aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/lib/active_model
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2014-06-30 15:37:06 -0300
committerRafael Mendonça França <rafaelmfranca@gmail.com>2014-06-30 15:37:06 -0300
commitbada1d3ed6a5d8879805ace8277711ae1efc18c8 (patch)
tree48d530c21a2ebf68021aedae68f0d407f082e5db /activemodel/lib/active_model
parent66d6c875baae1804d0a440b99705754143bfff99 (diff)
parent552d4d85f3e4d9332054bee7e18a5b44ae1ed3cf (diff)
downloadrails-bada1d3ed6a5d8879805ace8277711ae1efc18c8.tar.gz
rails-bada1d3ed6a5d8879805ace8277711ae1efc18c8.tar.bz2
rails-bada1d3ed6a5d8879805ace8277711ae1efc18c8.zip
Merge pull request #14861 from igor04/dirty-rollback
Added rollback method to ActiveModel::Dirty
Diffstat (limited to 'activemodel/lib/active_model')
-rw-r--r--activemodel/lib/active_model/dirty.rb17
1 files changed, 17 insertions, 0 deletions
diff --git a/activemodel/lib/active_model/dirty.rb b/activemodel/lib/active_model/dirty.rb
index f57588b96d..95a3c00d0b 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'
@@ -178,6 +190,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)