From 9aa1a3d85327fa0a3055b5b757a0be092ce582f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Mon, 23 Sep 2013 10:59:05 -0300 Subject: Merge pull request #10816 from bogdan/less-dirty-dirty Make AM::Dirty less dirty to plugin into AR or other library --- activemodel/CHANGELOG.md | 6 ++++++ activemodel/lib/active_model/dirty.rb | 26 ++++++++++++++++---------- activemodel/test/cases/dirty_test.rb | 3 +-- 3 files changed, 23 insertions(+), 12 deletions(-) (limited to 'activemodel') 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 attr_name_will_change! 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 changes_applied 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 *_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 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 -- cgit v1.2.3 From 089e1b6426719ad5132153323d23798094d271fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Mon, 23 Sep 2013 11:00:41 -0300 Subject: Document reset_changes since it is part of public API [ci skip] --- activemodel/lib/active_model/dirty.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'activemodel') diff --git a/activemodel/lib/active_model/dirty.rb b/activemodel/lib/active_model/dirty.rb index be2759b141..0b17443219 100644 --- a/activemodel/lib/active_model/dirty.rb +++ b/activemodel/lib/active_model/dirty.rb @@ -15,6 +15,8 @@ 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 + # information. # # A minimal implementation could be: # @@ -36,6 +38,10 @@ module ActiveModel # # do persistence work # changes_applied # end + # + # def reload! + # reset_changes + # end # end # # A newly instantiated object is unchanged: @@ -59,6 +65,12 @@ module ActiveModel # person.changed? # => false # person.name_changed? # => false # + # Reset the changes: + # + # person.previous_changes # => {"name" => ["Uncle Bob", "Bill"]} + # person.reload + # person.previous_changes # => {} + # # Assigning the same value leaves the attribute unchanged: # # person.name = 'Bill' -- cgit v1.2.3 From 7ef29140eb67a8f1475ebde24c524d7ff35a2154 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Mon, 23 Sep 2013 11:01:43 -0300 Subject: No need to abbreviate ActiveModel [ci skip] --- activemodel/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activemodel') diff --git a/activemodel/CHANGELOG.md b/activemodel/CHANGELOG.md index d9719afee3..91b25d5cda 100644 --- a/activemodel/CHANGELOG.md +++ b/activemodel/CHANGELOG.md @@ -1,4 +1,4 @@ -* Added new API methods `reset_changes` and `changed_applied` to AM::Dirty +* Added new API methods `reset_changes` and `changed_applied` to `ActiveModel::Dirty` that control changes state. Previsously you needed to update internal instance variables, but now API methods are available. -- cgit v1.2.3 From ed0b080cb37a420194b56a7029017ce4a1e8b408 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Mon, 23 Sep 2013 11:25:43 -0300 Subject: Fix the documentation method. It is reload! in the class definition. [ci skip] --- activemodel/lib/active_model/dirty.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activemodel') diff --git a/activemodel/lib/active_model/dirty.rb b/activemodel/lib/active_model/dirty.rb index 0b17443219..98e7d84608 100644 --- a/activemodel/lib/active_model/dirty.rb +++ b/activemodel/lib/active_model/dirty.rb @@ -68,7 +68,7 @@ module ActiveModel # Reset the changes: # # person.previous_changes # => {"name" => ["Uncle Bob", "Bill"]} - # person.reload + # person.reload! # person.previous_changes # => {} # # Assigning the same value leaves the attribute unchanged: -- cgit v1.2.3 From 4a99e1019931481011c621fe89f715fe6dac9dd3 Mon Sep 17 00:00:00 2001 From: "T.J. Schuck" Date: Mon, 23 Sep 2013 14:28:26 -0400 Subject: bcrypt-ruby v3.1.2 supports Ruby 2.0 on Windows --- activemodel/lib/active_model/secure_password.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'activemodel') diff --git a/activemodel/lib/active_model/secure_password.rb b/activemodel/lib/active_model/secure_password.rb index 8b9ac97bbb..17fafe4be9 100644 --- a/activemodel/lib/active_model/secure_password.rb +++ b/activemodel/lib/active_model/secure_password.rb @@ -20,9 +20,9 @@ module ActiveModel # value to the password_confirmation attribute and the validation # will not be triggered. # - # You need to add bcrypt-ruby (~> 3.1.0) to Gemfile to use #has_secure_password: + # You need to add bcrypt-ruby (~> 3.1.2) to Gemfile to use #has_secure_password: # - # gem 'bcrypt-ruby', '~> 3.1.0' + # gem 'bcrypt-ruby', '~> 3.1.2' # # Example using Active Record (which automatically includes ActiveModel::SecurePassword): # @@ -46,7 +46,7 @@ module ActiveModel # This is to avoid ActiveModel (and by extension the entire framework) # being dependent on a binary library. begin - gem 'bcrypt-ruby', '~> 3.1.0' + gem 'bcrypt-ruby', '~> 3.1.2' require 'bcrypt' rescue LoadError $stderr.puts "You don't have bcrypt-ruby installed in your application. Please add it to your Gemfile and run bundle install" -- cgit v1.2.3