views:

600

answers:

3

I am aware of ActiveRecord::Dirty and the related methods, but I don't see a means by which I can subscribe to an attribute changed event. Something like:

class Person < ActiveRecord::Base
  def attribute_changed(attribute_name, old_value, new_value)
  end

  #or

  attribute_changed do |attribute_name, old_value, new_value|
  end
end

Is there a Rails standard or plugin for this? I feel that it must be there somewhere and I'm just missing it.

A: 

try:

def attribute_changed(attribute_name, old_value, new_value)
end

def attribute=(attribute_name, value)
  returning(super) do
    attribute_changed(attribute_name, attribute_was(attribute_name), attribute(attribute_name))
  end
end

Just invented this now, but it should work.

cwninja
Thanks for setting me down the right path.
Mario
+2  A: 

cwninja's answer should do the trick, but there is a little bit more to it.

First of all, the base attribute handling is done with the write_attribute method so you should be tapping into this.

Rails also does have a built in callback structure which could be nice to tap into though it doesn't allow for passing arguments which is a bit of an annoyance.

Using custom callbacks you could do it like this:

class Person < ActiveRecord::Base

  def write_attribute(attr_name, value)
    returning(super) do
      attribute_changed(attr_name, read_attribute(attr_value), new_value)
    end
  end

  private

    def attribute_changed(attr, old_val, new_val)
      logger.info "Attribute Changed: #{attr} from #{old_val} to #{new_val}"
    end

 end

If you wanted to try to use Rails callbacks (especially useful if you might have multiple callbacks and/or subclassing) you could do something like this:

class Person < ActiveRecord::Base
  define_callbacks :attribute_changed

  attribute_changed :notify_of_attribute_change

  def write_attribute(attr_name, value)
    returning(super) do
      @last_changed_attr = attr_name
      run_callbacks(:attribute_changed)
    end
  end

  private

    def notify_of_attribute_change
      attr = @last_changed_attr
      old_val, new_val = send("#{attr}_change")
      logger.info "Attribute Changed: #{attr} from #{old_val} to #{new_val}"
    end

end
Peter Wagenet
Peter, that is beautiful, going to save me a TON of "write_attribute" i have littered all over my code. Thanks!
BushyMark
A: 

You could always access the private method changed_attributes and check the keys there using a before_save and do with it what you will.

Ryan Bigg