aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel
diff options
context:
space:
mode:
authorGodfrey Chan <godfreykfc@gmail.com>2013-12-30 22:05:56 -0800
committerGodfrey Chan <godfreykfc@gmail.com>2013-12-30 22:05:56 -0800
commitf3a8be3b8be496cd5de14e9c29de3748432d5da0 (patch)
tree4579d98d98223e87c958368f5cf16146144a136e /activemodel
parent969a0778cd2fd809a7b4f60c8477bb6d0ac9faf3 (diff)
parentda2b05bb6b713a702eb0420079394bf273f1203e (diff)
downloadrails-f3a8be3b8be496cd5de14e9c29de3748432d5da0.tar.gz
rails-f3a8be3b8be496cd5de14e9c29de3748432d5da0.tar.bz2
rails-f3a8be3b8be496cd5de14e9c29de3748432d5da0.zip
Merge pull request #13131 from gja/changed-accepts-values
Allows you to check if a field has changed to a particular value
Diffstat (limited to 'activemodel')
-rw-r--r--activemodel/CHANGELOG.md6
-rw-r--r--activemodel/lib/active_model/dirty.rb8
-rw-r--r--activemodel/test/cases/dirty_test.rb10
3 files changed, 22 insertions, 2 deletions
diff --git a/activemodel/CHANGELOG.md b/activemodel/CHANGELOG.md
index 77d1252f1f..42cf58a870 100644
--- a/activemodel/CHANGELOG.md
+++ b/activemodel/CHANGELOG.md
@@ -1,3 +1,9 @@
+* `attribute_changed?` now accepts parameters which check the old and new value of the attribute
+
+ `model.name_changed?(from: "Pete", to: "Ringo")`
+
+ *Tejas Dinkar*
+
* Fix `has_secure_password` to honor bcrypt-ruby's cost attribute.
*T.J. Schuck*
diff --git a/activemodel/lib/active_model/dirty.rb b/activemodel/lib/active_model/dirty.rb
index 8ccb25c613..58d87e6f7f 100644
--- a/activemodel/lib/active_model/dirty.rb
+++ b/activemodel/lib/active_model/dirty.rb
@@ -54,6 +54,7 @@ module ActiveModel
# person.name = 'Bob'
# person.changed? # => true
# person.name_changed? # => true
+ # person.name_changed?(from: "Uncle Bob", to: "Bob") # => true
# person.name_was # => "Uncle Bob"
# person.name_change # => ["Uncle Bob", "Bob"]
# person.name = 'Bill'
@@ -149,8 +150,11 @@ module ActiveModel
end
# Handle <tt>*_changed?</tt> for +method_missing+.
- def attribute_changed?(attr) # :nodoc:
- changed_attributes.include?(attr)
+ def attribute_changed?(attr, options = {}) #:nodoc:
+ result = changed_attributes.include?(attr)
+ result &&= options[:to] == __send__(attr) if options.key?(:to)
+ result &&= options[:from] == changed_attributes[attr] if options.key?(:from)
+ result
end
# Handle <tt>*_was</tt> for +method_missing+.
diff --git a/activemodel/test/cases/dirty_test.rb b/activemodel/test/cases/dirty_test.rb
index a90d0b1299..54427a1513 100644
--- a/activemodel/test/cases/dirty_test.rb
+++ b/activemodel/test/cases/dirty_test.rb
@@ -67,6 +67,16 @@ class DirtyTest < ActiveModel::TestCase
assert_equal [nil, "John"], @model.changes['name']
end
+ test "checking if an attribute has changed to a particular value" do
+ @model.name = "Ringo"
+ assert @model.name_changed?(from: nil, to: "Ringo")
+ assert_not @model.name_changed?(from: "Pete", to: "Ringo")
+ assert @model.name_changed?(to: "Ringo")
+ assert_not @model.name_changed?(to: "Pete")
+ assert @model.name_changed?(from: nil)
+ assert_not @model.name_changed?(from: "Pete")
+ end
+
test "changes accessible through both strings and symbols" do
@model.name = "David"
assert_not_nil @model.changes[:name]