From 552d4d85f3e4d9332054bee7e18a5b44ae1ed3cf Mon Sep 17 00:00:00 2001 From: igor04 Date: Thu, 24 Apr 2014 16:06:26 +0300 Subject: Added rollback method to ActiveModel::Dirty --- activemodel/lib/active_model/dirty.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'activemodel/lib') diff --git a/activemodel/lib/active_model/dirty.rb b/activemodel/lib/active_model/dirty.rb index 98ffffeb10..3e0d7f9115 100644 --- a/activemodel/lib/active_model/dirty.rb +++ b/activemodel/lib/active_model/dirty.rb @@ -17,6 +17,7 @@ module ActiveModel # * Call changes_applied after the changes are persisted. # * Call reset_changes when you want to reset the changes # information. + # * Call rollback_changes 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! + # rollback_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' @@ -176,6 +188,11 @@ module ActiveModel @changed_attributes = ActiveSupport::HashWithIndifferentAccess.new end + # Restore all previous data + def rollback_changes #:doc: + changed_attributes.each_key { |attr| reset_attribute! attr } + end + # Handle *_change for +method_missing+. def attribute_change(attr) [changed_attributes[attr], __send__(attr)] if attribute_changed?(attr) -- cgit v1.2.3