AR の動作を模倣したいのですが、コンテキストを変更してオブジェクトafter_save ..., if: -> { bar? }
に設定する方法がわかりません。self
module MyModule
extend ActiveSupport::Concern
module ClassMethods
def add_callback(options)
foo = options.fetch(:foo)
the_real_callback = -> do
puts foo.call(self).inspect
end
before_save(the_real_callback)
end
end
end
class MyClass < ActiveRecord::Base
include MyModule
add_callback foo: ->(instance) { instance.bar }
def bar
"bar"
end
end
o = MyClass.new
o.save
# displays "bar"
add_callback foo: ->(instance) { instance.bar }
と交換したいadd_callback foo -> { bar }