aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/lib/active_model/dirty.rb
diff options
context:
space:
mode:
authorLihan Li <frankieteardrop@gmail.com>2016-04-11 17:46:34 -0400
committerLihan Li <frankieteardrop@gmail.com>2016-04-11 17:46:34 -0400
commit73e2dbe651566dbda20002c6184e334c471e1195 (patch)
treebbd463db382331e2bfa7fcc9fc12adb4348cfb22 /activemodel/lib/active_model/dirty.rb
parentfa8602656edbf879430fffefc5efa11d253da003 (diff)
downloadrails-73e2dbe651566dbda20002c6184e334c471e1195.tar.gz
rails-73e2dbe651566dbda20002c6184e334c471e1195.tar.bz2
rails-73e2dbe651566dbda20002c6184e334c471e1195.zip
set default parameter to nil to speed up attribute_changed?
Benchmark results: Warming up -------------------------------------- old code 32.176k i/100ms new code 34.837k i/100ms Calculating ------------------------------------- old code 1.595M (± 3.5%) i/s - 7.947M new code 1.942M (± 3.9%) i/s - 9.685M
Diffstat (limited to 'activemodel/lib/active_model/dirty.rb')
-rw-r--r--activemodel/lib/active_model/dirty.rb8
1 files changed, 5 insertions, 3 deletions
diff --git a/activemodel/lib/active_model/dirty.rb b/activemodel/lib/active_model/dirty.rb
index 6e2e5afd1b..fad4de9df5 100644
--- a/activemodel/lib/active_model/dirty.rb
+++ b/activemodel/lib/active_model/dirty.rb
@@ -174,10 +174,12 @@ module ActiveModel
end
# Handles <tt>*_changed?</tt> for +method_missing+.
- def attribute_changed?(attr, options = {}) #:nodoc:
+ def attribute_changed?(attr, options = nil) #:nodoc:
result = changes_include?(attr)
- result &&= options[:to] == __send__(attr) if options.key?(:to)
- result &&= options[:from] == changed_attributes[attr] if options.key?(:from)
+ if options
+ result &&= options[:to] == __send__(attr) if options.key?(:to)
+ result &&= options[:from] == changed_attributes[attr] if options.key?(:from)
+ end
result
end