Skip to content

Commit

Permalink
overriding the ActiveModel::Dirty#attribute_change method, instead of…
Browse files Browse the repository at this point in the history
… extracting the code from it
  • Loading branch information
Derick Bailey committed Jul 5, 2011
1 parent b9d43f0 commit c467795
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion lib/mongoid/dirty.rb
Expand Up @@ -20,7 +20,7 @@ module Dirty #:nodoc:
def changes
{}.tap do |hash|
changed.each do |name|
change = [changed_attributes[name], attributes[name]] if attribute_changed?(name)
change = attribute_change(name)
hash[name] = change if change[0] != change[1]
end
end
Expand Down Expand Up @@ -72,5 +72,23 @@ def setters
end
end
end

private

# Get the current value for the specified attribute, if the attribute has changed.
#
# @note This is overriding the AM::Dirty implementation to read from the mongoid
# attributes hash, which may contain a serialized version of the attributes data. It is
# necessary to read the serialized version as the changed value, to allow updates to
# the MongoDB document to persist correctly. For example, if a DateTime field is updated
# it must be persisted as a UTC Time.
#
# @return [ Object ] The current value of the field, or nil if no change made.
#
# @since 2.1.0
def attribute_change(attr)
[changed_attributes[attr], attributes[attr]] if attribute_changed?(attr)
end

end
end

0 comments on commit c467795

Please sign in to comment.