aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/lib
diff options
context:
space:
mode:
authorTejas Dinkar <tejas@gja.in>2013-12-02 10:20:43 +0530
committerTejas Dinkar <tejas@gja.in>2013-12-15 12:13:31 +0530
commitda2b05bb6b713a702eb0420079394bf273f1203e (patch)
tree854ece5341e6b41f45af5842fef164544a35e3b2 /activemodel/lib
parentfc83efae516fc572551b3f1d67a12394800dd4e2 (diff)
downloadrails-da2b05bb6b713a702eb0420079394bf273f1203e.tar.gz
rails-da2b05bb6b713a702eb0420079394bf273f1203e.tar.bz2
rails-da2b05bb6b713a702eb0420079394bf273f1203e.zip
Allows you to check if an attribute has changed to a particular value
model.name_changed?(from: "Pete", to: "Ringo")
Diffstat (limited to 'activemodel/lib')
-rw-r--r--activemodel/lib/active_model/dirty.rb8
1 files changed, 6 insertions, 2 deletions
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+.