From 1a300b674810b85e6f55e4106cb27c1b2dbef499 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Thu, 17 Jul 2014 14:53:41 -0300 Subject: Make restore_attributes public Also make it accept a list of attributes to be changed. This will make possible to restore only a subset of the changed attributes. Closes #16203 --- activemodel/lib/active_model/dirty.rb | 10 +++++----- activemodel/test/cases/dirty_test.rb | 20 +++++++++++++++----- 2 files changed, 20 insertions(+), 10 deletions(-) (limited to 'activemodel') diff --git a/activemodel/lib/active_model/dirty.rb b/activemodel/lib/active_model/dirty.rb index 24214187af..d11243c4c0 100644 --- a/activemodel/lib/active_model/dirty.rb +++ b/activemodel/lib/active_model/dirty.rb @@ -180,6 +180,11 @@ module ActiveModel attribute_changed?(attr) ? changed_attributes[attr] : __send__(attr) end + # Restore all previous data of the provided attributes. + def restore_attributes(attributes = changed) + attributes.each { |attr| restore_attribute! attr } + end + private # Removes current changes and makes them accessible through +previous_changes+. @@ -199,11 +204,6 @@ module ActiveModel clear_changes_information end - # Restore all previous data. - def restore_attributes # :doc: - changed_attributes.each_key { |attr| restore_attribute! attr } - end - # Handle *_change 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 6b8bb5216b..db2cd885e2 100644 --- a/activemodel/test/cases/dirty_test.rb +++ b/activemodel/test/cases/dirty_test.rb @@ -49,10 +49,6 @@ class DirtyTest < ActiveModel::TestCase def deprecated_reload reset_changes end - - def rollback - restore_attributes - end end setup do @@ -209,10 +205,24 @@ class DirtyTest < ActiveModel::TestCase @model.name = 'Bob' @model.color = 'White' - @model.rollback + @model.restore_attributes assert_not @model.changed? assert_equal 'Dmitry', @model.name assert_equal 'Red', @model.color end + + test "restore_attributes can restore only some attributes" do + @model.name = 'Dmitry' + @model.color = 'Red' + @model.save + @model.name = 'Bob' + @model.color = 'White' + + @model.restore_attributes(['name']) + + assert @model.changed? + assert_equal 'Dmitry', @model.name + assert_equal 'White', @model.color + end end -- cgit v1.2.3