I am creating a gem which create hooks like before_update_filter in ActiveRecord class .
For this I have create a module UpdateFilter as given below :
module UpdateFilter
def before_update_filter(*args)
puts self #in class scope
self.set_callback(:save, :before, args[0].to_sym)
end
end
And in intializers I am doing
ActiveRecord::Base.extend UpdateFilter
Ok. Now all above is working fine . But I want to set_callbacks only if some attributes of instance changed and I cannot access attributes in before_update_filter method because it is in class scope .
As a summary I want to implement hook like . I hope it clears what I am trying to do .
before_update_filter :instance_mthod_name, :attr_prams => [:name, :rating]
Now how can I implement this??
It might require some edits, but I think that should work:
According to the docs block passed to
set_callbackis executed in the context of the current object. So we have access to all instance methods inside this block. We check that all (replace it with ‘any’ or even some other conditions) listed attributes have changed and only then call required callback instance method.