Mark Grealish

the life and code of a crazy cat man


Skip model callbacks through factory_girl in Rails 5

in code

The kind of callback we skip are expensive calls to third-party services. These are all tested separately within our suite. In Rails 4, we used this pattern to skip certain callbacks:

FactoryGirl.define do
  factory :fremen do
    name 'Liet Kynes'
    fierceness :high

    after :build do
      Fremen.skip_callback :create, :after, :bribe_spacing_guild
    end

    trait :with_bribe do
      Fremen.set_callback :create, :after, :bribe_spacing_guild
    end
  end
end

Per this issue, Rails 5 raises an ArgumentError if skip_callback is called on an already-skipped callback, so I had to amend it:

FactoryGirl.define do
  factory :fremen do
    name 'Liet Kynes'
    fierceness :high

    Fremen.skip_callback :create, :after, :bribe_spacing_guild

    trait :with_bribe do
      after :build do
        Fremen.set_callback :create, :after, :bribe_spacing_guild
      end

      after :create do
        Fremen.skip_callback :create, :after, :bribe_spacing_guild
      end
    end
  end
end

This is not optimal (it’ll wig out on create_list :fremen, 3, :with_bribe), but it works.


Always Grouchy

in me

Taken at Blackrock, Dublin.

Me at dawn at Blackrock

Ballycorus Leadmines

in ireland

Photographed on my hike today. I was surprised when this photograph came out, let alone perfectly, because the wind blew me flat on my ass as I finished the capture.

Chimney at Ballycorus Leadmines

Hell of a week

in code

I couldn’t get my head straight on Tuesday so I went to look at the sea instead.

At least it went back up?

Perfect and Blue and Empty

in ireland

The day was too much, so I climbed a hill and decided to be for a while.

The Irish Sea from Killiney Hill, Dublin

Pretty and red at Blackrock

in friends

Eadaoin on the beach at Blackrock, February 18 2017