Created
April 23, 2015 21:31
-
-
Save mgraham/84214a0d46653d74ca2e to your computer and use it in GitHub Desktop.
Allow tracking whether ActiveType attributes have changed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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