From 66d0a0153578ce760d822580c5b8c0b726042ac2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Tue, 15 Jul 2014 16:00:50 -0300 Subject: Deprecate ActiveModel::Dirty#reset_changes in favor of #clear_changes_information This method name is causing confusion with the `reset_#{attribute}` methods. While `reset_name` set the value of the name attribute for the previous value the `reset_changes` only discard the changes and previous changes. --- activemodel/lib/active_model/dirty.rb | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'activemodel/lib') diff --git a/activemodel/lib/active_model/dirty.rb b/activemodel/lib/active_model/dirty.rb index dc6088a39a..e93ce05e40 100644 --- a/activemodel/lib/active_model/dirty.rb +++ b/activemodel/lib/active_model/dirty.rb @@ -15,7 +15,7 @@ module ActiveModel # * Call attr_name_will_change! before each change to the tracked # attribute. # * Call changes_applied after the changes are persisted. - # * Call reset_changes when you want to reset the changes + # * Call clear_changes_information when you want to reset the changes # information. # * Call undo_changes when you want to restore previous data. # @@ -37,11 +37,14 @@ module ActiveModel # # def save # # do persistence work + # # changes_applied # end # # def reload! - # reset_changes + # # get the values from the persistence layer + # + # clear_changes_information # end # # def rollback! @@ -184,12 +187,17 @@ module ActiveModel @changed_attributes = ActiveSupport::HashWithIndifferentAccess.new end - # Removes all dirty data: current changes and previous changes. - def reset_changes # :doc: + # Clear all dirty data: current changes and previous changes. + def clear_changes_information # :doc: @previously_changed = ActiveSupport::HashWithIndifferentAccess.new @changed_attributes = ActiveSupport::HashWithIndifferentAccess.new end + def reset_changes + ActiveSupport::Deprecation.warn "#reset_changes is deprecated and will be removed on Rails 5. Please use #clear_changes_information instead." + clear_changes_information + end + # Restore all previous data. def undo_changes # :doc: changed_attributes.each_key { |attr| reset_attribute! attr } -- cgit v1.2.3 From 41fb06fa47b0c11d6b943163ec1bc8ce9fd4d229 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Tue, 15 Jul 2014 16:12:23 -0300 Subject: Deprecate `reset_#{attribute}` in favor of `restore_#{attribute}`. These methods may cause confusion with the `reset_changes` that behaves differently of them. Also rename undo_changes to restore_changes to match this new set of methods. --- activemodel/lib/active_model/dirty.rb | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'activemodel/lib') diff --git a/activemodel/lib/active_model/dirty.rb b/activemodel/lib/active_model/dirty.rb index e93ce05e40..24214187af 100644 --- a/activemodel/lib/active_model/dirty.rb +++ b/activemodel/lib/active_model/dirty.rb @@ -17,7 +17,7 @@ module ActiveModel # * Call changes_applied after the changes are persisted. # * Call clear_changes_information when you want to reset the changes # information. - # * Call undo_changes when you want to restore previous data. + # * Call restore_attributes when you want to restore previous data. # # A minimal implementation could be: # @@ -48,7 +48,7 @@ module ActiveModel # end # # def rollback! - # undo_changes + # restore_attributes # end # end # @@ -116,6 +116,7 @@ module ActiveModel included do attribute_method_suffix '_changed?', '_change', '_will_change!', '_was' attribute_method_affix prefix: 'reset_', suffix: '!' + attribute_method_affix prefix: 'restore_', suffix: '!' end # Returns +true+ if any attribute have unsaved changes, +false+ otherwise. @@ -199,8 +200,8 @@ module ActiveModel end # Restore all previous data. - def undo_changes # :doc: - changed_attributes.each_key { |attr| reset_attribute! attr } + def restore_attributes # :doc: + changed_attributes.each_key { |attr| restore_attribute! attr } end # Handle *_change for +method_missing+. @@ -223,6 +224,13 @@ module ActiveModel # Handle reset_*! for +method_missing+. def reset_attribute!(attr) + ActiveSupport::Deprecation.warn "#reset_#{attr}! is deprecated and will be removed on Rails 5. Please use #restore_#{attr}! instead." + + restore_attribute!(attr) + end + + # Handle restore_*! for +method_missing+. + def restore_attribute!(attr) if attribute_changed?(attr) __send__("#{attr}=", changed_attributes[attr]) changed_attributes.delete(attr) -- cgit v1.2.3