aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2013-09-25 11:25:36 -0700
committerAaron Patterson <aaron.patterson@gmail.com>2013-09-25 11:25:36 -0700
commite2fd64fe469037644711a65a00270bea15ee2955 (patch)
treea7374d295e2f8e137593171ae99d2d6102d868bc /activemodel
parent70e48a6ac34615ba5ed3b807917c3b099256d67c (diff)
parentf3982858359008ec8fe43b8fa2c75aad8788cd89 (diff)
downloadrails-e2fd64fe469037644711a65a00270bea15ee2955.tar.gz
rails-e2fd64fe469037644711a65a00270bea15ee2955.tar.bz2
rails-e2fd64fe469037644711a65a00270bea15ee2955.zip
Merge branch 'master' into preload
* master: (62 commits) Getting Started Guide: Hello Rails! -> Hello, Rails! and wrap code tag Add CHANGELOG entry for #12344 Add regression test to #12343 Fix typo in number_to_human docs: you -> your [Documentation] Add a missing validation to I18n docs Use the given name in html_options for the hidden field in collection_check_boxes assign_attributes should return if argument is blank. No need the else clause Use join to concat the both side of the AST Add a CHANGELOG entry about Web Console inclusion added column type to example in section 2.3 Include web-console in new projects Gemfile ActiveRecord::ConnectionAdapters::Column.string_to_time method respects string with timezone. Closes #12278. add test_scoped_root_as_name Getting Started Guide: update RubyGems Guides link [ci skip] Deprecate unused quoted_locking_column method. Update references to wycats/thor to erikhuda/thor. bcrypt-ruby v3.1.2 supports Ruby 2.0 on Windows Fix the model name in the association basics guides We shouldn't override PostgreSQLAdapter's superclass inheritance while monkeypatching ...
Diffstat (limited to 'activemodel')
-rw-r--r--activemodel/CHANGELOG.md6
-rw-r--r--activemodel/lib/active_model/dirty.rb38
-rw-r--r--activemodel/lib/active_model/secure_password.rb6
-rw-r--r--activemodel/test/cases/dirty_test.rb3
4 files changed, 38 insertions, 15 deletions
diff --git a/activemodel/CHANGELOG.md b/activemodel/CHANGELOG.md
index 3d3c61ed1c..91b25d5cda 100644
--- a/activemodel/CHANGELOG.md
+++ b/activemodel/CHANGELOG.md
@@ -1,3 +1,9 @@
+* 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.
+
+ *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..98e7d84608 100644
--- a/activemodel/lib/active_model/dirty.rb
+++ b/activemodel/lib/active_model/dirty.rb
@@ -14,13 +14,9 @@ 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.
+ # * Call <tt>reset_changes</tt> when you want to reset the changes
+ # information.
#
# A minimal implementation could be:
#
@@ -39,8 +35,12 @@ module ActiveModel
# end
#
# def save
- # @previously_changed = changes
- # @changed_attributes.clear
+ # # do persistence work
+ # changes_applied
+ # end
+ #
+ # def reload!
+ # reset_changes
# end
# end
#
@@ -65,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'
@@ -129,7 +135,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 +160,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/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"
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