diff options
Diffstat (limited to 'activemodel/lib/active_model/dirty.rb')
-rw-r--r-- | activemodel/lib/active_model/dirty.rb | 23 |
1 files changed, 20 insertions, 3 deletions
diff --git a/activemodel/lib/active_model/dirty.rb b/activemodel/lib/active_model/dirty.rb index f57588b96d..dc6088a39a 100644 --- a/activemodel/lib/active_model/dirty.rb +++ b/activemodel/lib/active_model/dirty.rb @@ -17,6 +17,7 @@ module ActiveModel # * Call <tt>changes_applied</tt> after the changes are persisted. # * Call <tt>reset_changes</tt> when you want to reset the changes # information. + # * Call <tt>undo_changes</tt> when you want to restore previous data. # # A minimal implementation could be: # @@ -42,6 +43,10 @@ module ActiveModel # def reload! # reset_changes # end + # + # def rollback! + # undo_changes + # end # end # # A newly instantiated object is unchanged: @@ -72,6 +77,13 @@ module ActiveModel # person.reload! # person.previous_changes # => {} # + # Rollback the changes: + # + # person.name = "Uncle Bob" + # person.rollback! + # person.name # => "Bill" + # person.name_changed? # => false + # # Assigning the same value leaves the attribute unchanged: # # person.name = 'Bill' @@ -167,17 +179,22 @@ module ActiveModel private # Removes current changes and makes them accessible through +previous_changes+. - def changes_applied + def changes_applied # :doc: @previously_changed = changes @changed_attributes = ActiveSupport::HashWithIndifferentAccess.new end - # Removes all dirty data: current changes and previous changes - def reset_changes + # Removes all dirty data: current changes and previous changes. + def reset_changes # :doc: @previously_changed = ActiveSupport::HashWithIndifferentAccess.new @changed_attributes = ActiveSupport::HashWithIndifferentAccess.new end + # Restore all previous data. + def undo_changes # :doc: + changed_attributes.each_key { |attr| reset_attribute! attr } + end + # Handle <tt>*_change</tt> for +method_missing+. def attribute_change(attr) [changed_attributes[attr], __send__(attr)] if attribute_changed?(attr) |