diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2014-07-15 16:12:23 -0300 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2014-07-15 18:09:38 -0300 |
commit | 41fb06fa47b0c11d6b943163ec1bc8ce9fd4d229 (patch) | |
tree | 8273991bd00e5b3b47d0994630b9d6875f5abdc6 /activemodel | |
parent | 66d0a0153578ce760d822580c5b8c0b726042ac2 (diff) | |
download | rails-41fb06fa47b0c11d6b943163ec1bc8ce9fd4d229.tar.gz rails-41fb06fa47b0c11d6b943163ec1bc8ce9fd4d229.tar.bz2 rails-41fb06fa47b0c11d6b943163ec1bc8ce9fd4d229.zip |
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.
Diffstat (limited to 'activemodel')
-rw-r--r-- | activemodel/CHANGELOG.md | 9 | ||||
-rw-r--r-- | activemodel/lib/active_model/dirty.rb | 16 | ||||
-rw-r--r-- | activemodel/test/cases/dirty_test.rb | 6 |
3 files changed, 22 insertions, 9 deletions
diff --git a/activemodel/CHANGELOG.md b/activemodel/CHANGELOG.md index 86f803d130..555cd259d2 100644 --- a/activemodel/CHANGELOG.md +++ b/activemodel/CHANGELOG.md @@ -1,11 +1,16 @@ +* Deprecate `reset_#{attribute}` in favor of `restore_#{attribute}`. + + These methods may cause confusion with the `reset_changes` that behaves differently + of them. + * 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 `reset_changes` only discard the changes and previous - changes. + changes. -* Added `undo_changes` method to `ActiveModel::Dirty` API to restore all the +* Added `restore_attributes` method to `ActiveModel::Dirty` API to restore all the changed values to the previous data. *Igor G.* 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 <tt>changes_applied</tt> after the changes are persisted. # * Call <tt>clear_changes_information</tt> when you want to reset the changes # information. - # * Call <tt>undo_changes</tt> when you want to restore previous data. + # * Call <tt>restore_attributes</tt> 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 <tt>*_change</tt> for +method_missing+. @@ -223,6 +224,13 @@ module ActiveModel # Handle <tt>reset_*!</tt> 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 <tt>restore_*!</tt> for +method_missing+. + def restore_attribute!(attr) if attribute_changed?(attr) __send__("#{attr}=", changed_attributes[attr]) changed_attributes.delete(attr) diff --git a/activemodel/test/cases/dirty_test.rb b/activemodel/test/cases/dirty_test.rb index 09f26c319d..6b8bb5216b 100644 --- a/activemodel/test/cases/dirty_test.rb +++ b/activemodel/test/cases/dirty_test.rb @@ -51,7 +51,7 @@ class DirtyTest < ActiveModel::TestCase end def rollback - undo_changes + restore_attributes end end @@ -115,7 +115,7 @@ class DirtyTest < ActiveModel::TestCase test "resetting attribute" do @model.name = "Bob" - @model.reset_name! + @model.restore_name! assert_nil @model.name assert !@model.name_changed? end @@ -202,7 +202,7 @@ class DirtyTest < ActiveModel::TestCase assert_equal ActiveSupport::HashWithIndifferentAccess.new, @model.changed_attributes end - test "undo_changes should restore all previous data" do + test "restore_attributes should restore all previous data" do @model.name = 'Dmitry' @model.color = 'Red' @model.save |