aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel
diff options
context:
space:
mode:
authorigor04 <igor.s04@mail.ru>2014-04-24 16:06:26 +0300
committerigor04 <igor.s04g@gmail.com>2014-06-23 23:19:43 +0300
commit552d4d85f3e4d9332054bee7e18a5b44ae1ed3cf (patch)
tree4ee803552b859eaa60ee53c56c9ad03a74c05014 /activemodel
parent43101ab3afe2a008d50cd78069e3bd94736da07d (diff)
downloadrails-552d4d85f3e4d9332054bee7e18a5b44ae1ed3cf.tar.gz
rails-552d4d85f3e4d9332054bee7e18a5b44ae1ed3cf.tar.bz2
rails-552d4d85f3e4d9332054bee7e18a5b44ae1ed3cf.zip
Added rollback method to ActiveModel::Dirty
Diffstat (limited to 'activemodel')
-rw-r--r--activemodel/lib/active_model/dirty.rb17
-rw-r--r--activemodel/test/cases/dirty_test.rb18
2 files changed, 35 insertions, 0 deletions
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 <tt>changes_applied</tt> after the changes are persisted.
# * Call <tt>reset_changes</tt> when you want to reset the changes
# information.
+ # * Call <tt>rollback_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!
+ # 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 <tt>*_change</tt> for +method_missing+.
def attribute_change(attr)
[changed_attributes[attr], __send__(attr)] if attribute_changed?(attr)
diff --git a/activemodel/test/cases/dirty_test.rb b/activemodel/test/cases/dirty_test.rb
index 2853476c91..d39484a7b9 100644
--- a/activemodel/test/cases/dirty_test.rb
+++ b/activemodel/test/cases/dirty_test.rb
@@ -45,6 +45,10 @@ class DirtyTest < ActiveModel::TestCase
def reload
reset_changes
end
+
+ def rollback
+ rollback_changes
+ end
end
setup do
@@ -176,4 +180,18 @@ class DirtyTest < ActiveModel::TestCase
assert_equal ActiveSupport::HashWithIndifferentAccess.new, @model.previous_changes
assert_equal ActiveSupport::HashWithIndifferentAccess.new, @model.changed_attributes
end
+
+ test "rollback should restore all previous data" do
+ @model.name = 'Dmitry'
+ @model.color = 'Red'
+ @model.save
+ @model.name = 'Bob'
+ @model.color = 'White'
+
+ @model.rollback
+
+ assert_not @model.changed?
+ assert_equal 'Dmitry', @model.name
+ assert_equal 'Red', @model.color
+ end
end