diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2013-09-23 10:59:05 -0300 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2013-09-23 10:59:05 -0300 |
commit | 9aa1a3d85327fa0a3055b5b757a0be092ce582f7 (patch) | |
tree | 963c86f32493da238498a52798c0c6018addd144 /activemodel | |
parent | 782055674a975e54846bcc103eac023ac84b046b (diff) | |
download | rails-9aa1a3d85327fa0a3055b5b757a0be092ce582f7.tar.gz rails-9aa1a3d85327fa0a3055b5b757a0be092ce582f7.tar.bz2 rails-9aa1a3d85327fa0a3055b5b757a0be092ce582f7.zip |
Merge pull request #10816 from bogdan/less-dirty-dirty
Make AM::Dirty less dirty to plugin into AR or other library
Diffstat (limited to 'activemodel')
-rw-r--r-- | activemodel/CHANGELOG.md | 6 | ||||
-rw-r--r-- | activemodel/lib/active_model/dirty.rb | 26 | ||||
-rw-r--r-- | activemodel/test/cases/dirty_test.rb | 3 |
3 files changed, 23 insertions, 12 deletions
diff --git a/activemodel/CHANGELOG.md b/activemodel/CHANGELOG.md index 3d3c61ed1c..d9719afee3 100644 --- a/activemodel/CHANGELOG.md +++ b/activemodel/CHANGELOG.md @@ -1,3 +1,9 @@ +* Added new API methods `reset_changes` and `changed_applied` to AM::Dirty + that control changes state. Previsously you needed to update internal + instance variables, but now API methods are available. + + *Bogdan Gusiev* + * Fix has_secure_password. `password_confirmation` validations are triggered even if no `password_confirmation` is set. diff --git a/activemodel/lib/active_model/dirty.rb b/activemodel/lib/active_model/dirty.rb index ea5ddf71de..be2759b141 100644 --- a/activemodel/lib/active_model/dirty.rb +++ b/activemodel/lib/active_model/dirty.rb @@ -14,13 +14,7 @@ module ActiveModel # track. # * Call <tt>attr_name_will_change!</tt> before each change to the tracked # attribute. - # - # If you wish to also track previous changes on save or update, you need to - # add: - # - # @previously_changed = changes - # - # inside of your save or update method. + # * Call <tt>changes_applied</tt> after the changes are persisted. # # A minimal implementation could be: # @@ -39,8 +33,8 @@ module ActiveModel # end # # def save - # @previously_changed = changes - # @changed_attributes.clear + # # do persistence work + # changes_applied # end # end # @@ -129,7 +123,7 @@ module ActiveModel # person.save # person.previous_changes # => {"name" => ["bob", "robert"]} def previous_changes - @previously_changed + @previously_changed ||= {} end # Returns a hash of the attributes with unsaved changes indicating their original @@ -154,6 +148,18 @@ module ActiveModel private + # Removes current changes and makes them accessible through +previous_changes+. + def changes_applied + @previously_changed = changes + @changed_attributes = {} + end + + # Removes all dirty data: current changes and previous changes + def reset_changes + @previously_changed = {} + @changed_attributes = {} + end + # Handle <tt>*_change</tt> 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 ba45089cca..ed34ca8a6e 100644 --- a/activemodel/test/cases/dirty_test.rb +++ b/activemodel/test/cases/dirty_test.rb @@ -29,8 +29,7 @@ class DirtyTest < ActiveModel::TestCase end def save - @previously_changed = changes - @changed_attributes.clear + changes_applied end end |