aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/lib/active_model/dirty.rb
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2014-07-15 18:45:15 -0300
committerRafael Mendonça França <rafaelmfranca@gmail.com>2014-07-15 18:45:15 -0300
commit93e09f52781993f9f6b36af5b93e7d8892252271 (patch)
tree141dc9de75148f2d3a358a87e2524785e7a3e357 /activemodel/lib/active_model/dirty.rb
parent2b2e04150680498f043720de34f82e6dc647c14a (diff)
parent41fb06fa47b0c11d6b943163ec1bc8ce9fd4d229 (diff)
downloadrails-93e09f52781993f9f6b36af5b93e7d8892252271.tar.gz
rails-93e09f52781993f9f6b36af5b93e7d8892252271.tar.bz2
rails-93e09f52781993f9f6b36af5b93e7d8892252271.zip
Merge pull request #16180 from rafaelfranca/rm-dirty
Improve Active Model Dirty API.
Diffstat (limited to 'activemodel/lib/active_model/dirty.rb')
-rw-r--r--activemodel/lib/active_model/dirty.rb32
1 files changed, 24 insertions, 8 deletions
diff --git a/activemodel/lib/active_model/dirty.rb b/activemodel/lib/active_model/dirty.rb
index dc6088a39a..24214187af 100644
--- a/activemodel/lib/active_model/dirty.rb
+++ b/activemodel/lib/active_model/dirty.rb
@@ -15,9 +15,9 @@ module ActiveModel
# * Call <tt>attr_name_will_change!</tt> before each change to the tracked
# attribute.
# * Call <tt>changes_applied</tt> after the changes are persisted.
- # * Call <tt>reset_changes</tt> when you want to reset the changes
+ # * 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:
#
@@ -37,15 +37,18 @@ 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!
- # undo_changes
+ # restore_attributes
# end
# end
#
@@ -113,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.
@@ -184,15 +188,20 @@ 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 }
+ def restore_attributes # :doc:
+ changed_attributes.each_key { |attr| restore_attribute! attr }
end
# Handle <tt>*_change</tt> for +method_missing+.
@@ -215,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)