Skip to content

Instantly share code, notes, and snippets.

@mgraham
Created April 23, 2015 21:31
Show Gist options
  • Save mgraham/84214a0d46653d74ca2e to your computer and use it in GitHub Desktop.
Save mgraham/84214a0d46653d74ca2e to your computer and use it in GitHub Desktop.
Allow tracking whether ActiveType attributes have changed
module TrackAttributeChanges
extend ActiveSupport::Concern
include ActiveModel::Dirty
module ClassMethods
def track_attribute_changes(attr, type=nil)
self.module_eval <<-BODY, __FILE__, __LINE__ + 1
def #{attr}=(value)
changed = false
if "#{type}" == "datetime"
changed = (#{attr}.to_s != value.to_s)
else
changed = (#{attr} != value)
end
attribute_will_change!('#{attr}') if changed
super
end
def #{attr}_changed?
changed.include?('#{attr}')
end
BODY
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment