diff options
author | Sean Griffin <sean@thoughtbot.com> | 2014-08-22 11:24:00 -0700 |
---|---|---|
committer | Sean Griffin <sean@thoughtbot.com> | 2014-08-22 11:26:06 -0700 |
commit | 9384fa43cc7bbb32a2683821eaa9383344f03ebd (patch) | |
tree | 6f887568d3a82f8197804a25345aaccf8bc50b6a /activerecord/lib/active_record/attribute_methods | |
parent | b47d8dea25aa4c1b0d02603adc00cf63f2163f21 (diff) | |
download | rails-9384fa43cc7bbb32a2683821eaa9383344f03ebd.tar.gz rails-9384fa43cc7bbb32a2683821eaa9383344f03ebd.tar.bz2 rails-9384fa43cc7bbb32a2683821eaa9383344f03ebd.zip |
Cache the value of `changed_attributes` when calling `changes_applied`
`changes_applied` calles `changes`, which will call `changed_attributes`
multiple times in a loop. This method actually performs work now, so we
should cache the results while looping over it when we know it cannot
change.
Diffstat (limited to 'activerecord/lib/active_record/attribute_methods')
-rw-r--r-- | activerecord/lib/active_record/attribute_methods/dirty.rb | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/attribute_methods/dirty.rb b/activerecord/lib/active_record/attribute_methods/dirty.rb index 01f126f1b3..2f02738f6d 100644 --- a/activerecord/lib/active_record/attribute_methods/dirty.rb +++ b/activerecord/lib/active_record/attribute_methods/dirty.rb @@ -54,7 +54,19 @@ module ActiveRecord end def changed_attributes - super.reverse_merge(attributes_changed_in_place).freeze + # This should only be set by methods which will call changed_attributes + # multiple times when it is known that the computed value cannot change. + if defined?(@cached_changed_attributes) + @cached_changed_attributes + else + super.reverse_merge(attributes_changed_in_place).freeze + end + end + + def changes + cache_changed_attributes do + super + end end private @@ -157,6 +169,13 @@ module ActiveRecord store_original_raw_attribute(attr) end end + + def cache_changed_attributes + @cached_changed_attributes = changed_attributes + yield + ensure + remove_instance_variable(:@cached_changed_attributes) + end end end end |