aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/lib
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2013-09-23 10:59:05 -0300
committerRafael Mendonça França <rafaelmfranca@gmail.com>2013-09-23 10:59:05 -0300
commit9aa1a3d85327fa0a3055b5b757a0be092ce582f7 (patch)
tree963c86f32493da238498a52798c0c6018addd144 /activemodel/lib
parent782055674a975e54846bcc103eac023ac84b046b (diff)
downloadrails-9aa1a3d85327fa0a3055b5b757a0be092ce582f7.tar.gz
rails-9aa1a3d85327fa0a3055b5b757a0be092ce582f7.tar.bz2
rails-9aa1a3d85327fa0a3055b5b757a0be092ce582f7.zip
Merge pull request #10816 from bogdan/less-dirty-dirty
Make AM::Dirty less dirty to plugin into AR or other library
Diffstat (limited to 'activemodel/lib')
-rw-r--r--activemodel/lib/active_model/dirty.rb26
1 files changed, 16 insertions, 10 deletions
diff --git a/activemodel/lib/active_model/dirty.rb b/activemodel/lib/active_model/dirty.rb
index ea5ddf71de..be2759b141 100644
--- a/activemodel/lib/active_model/dirty.rb
+++ b/activemodel/lib/active_model/dirty.rb
@@ -14,13 +14,7 @@ module ActiveModel
# track.
# * Call <tt>attr_name_will_change!</tt> before each change to the tracked
# attribute.
- #
- # If you wish to also track previous changes on save or update, you need to
- # add:
- #
- # @previously_changed = changes
- #
- # inside of your save or update method.
+ # * Call <tt>changes_applied</tt> after the changes are persisted.
#
# A minimal implementation could be:
#
@@ -39,8 +33,8 @@ module ActiveModel
# end
#
# def save
- # @previously_changed = changes
- # @changed_attributes.clear
+ # # do persistence work
+ # changes_applied
# end
# end
#
@@ -129,7 +123,7 @@ module ActiveModel
# person.save
# person.previous_changes # => {"name" => ["bob", "robert"]}
def previous_changes
- @previously_changed
+ @previously_changed ||= {}
end
# Returns a hash of the attributes with unsaved changes indicating their original
@@ -154,6 +148,18 @@ module ActiveModel
private
+ # Removes current changes and makes them accessible through +previous_changes+.
+ def changes_applied
+ @previously_changed = changes
+ @changed_attributes = {}
+ end
+
+ # Removes all dirty data: current changes and previous changes
+ def reset_changes
+ @previously_changed = {}
+ @changed_attributes = {}
+ end
+
# Handle <tt>*_change</tt> for +method_missing+.
def attribute_change(attr)
[changed_attributes[attr], __send__(attr)] if attribute_changed?(attr)