diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2013-06-11 11:47:24 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2013-06-11 11:47:24 -0700 |
commit | c9d6c1b4afcaf39bf0e83fcf32b13093c94c1247 (patch) | |
tree | a194573dd5129afac608accdec5026ebe1974678 /activerecord | |
parent | ebd3aedbb477f7e5f3416e781fc82ead7e27f872 (diff) | |
download | rails-c9d6c1b4afcaf39bf0e83fcf32b13093c94c1247.tar.gz rails-c9d6c1b4afcaf39bf0e83fcf32b13093c94c1247.tar.bz2 rails-c9d6c1b4afcaf39bf0e83fcf32b13093c94c1247.zip |
push before_destroy counter cache method to a single method
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/associations/builder/belongs_to.rb | 29 |
1 files changed, 17 insertions, 12 deletions
diff --git a/activerecord/lib/active_record/associations/builder/belongs_to.rb b/activerecord/lib/active_record/associations/builder/belongs_to.rb index 2483ea3db3..51d24c1db8 100644 --- a/activerecord/lib/active_record/associations/builder/belongs_to.rb +++ b/activerecord/lib/active_record/associations/builder/belongs_to.rb @@ -36,6 +36,17 @@ module ActiveRecord::Associations::Builder @_after_create_counter_called = true end end + + def belongs_to_counter_cache_before_destroy(association, reflection) + foreign_key = reflection.foreign_key.to_sym + unless destroyed_by_association && destroyed_by_association.foreign_key.to_sym == foreign_key + record = send association.name + if record && !self.destroyed? + cache_column = reflection.counter_cache_column + record.class.decrement_counter(cache_column, record.id) + end + end + end end end @@ -46,15 +57,6 @@ module ActiveRecord::Associations::Builder add_counter_cache_methods mixin mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1 - def belongs_to_counter_cache_before_destroy_for_#{name} - unless destroyed_by_association && destroyed_by_association.foreign_key.to_sym == #{foreign_key.to_sym.inspect} - record = #{name} - if record && !self.destroyed? - record.class.decrement_counter(:#{cache_column}, record.id) - end - end - end - def belongs_to_counter_cache_after_update_for_#{name} if (@_after_create_counter_called ||= false) @_after_create_counter_called = false @@ -75,11 +77,14 @@ module ActiveRecord::Associations::Builder association = self - model.after_create lambda { |o| - o.belongs_to_counter_cache_after_create(association, reflection) + model.after_create lambda { |record| + record.belongs_to_counter_cache_after_create(association, reflection) + } + + model.before_destroy lambda { |record| + record.belongs_to_counter_cache_before_destroy(association, reflection) } - model.before_destroy "belongs_to_counter_cache_before_destroy_for_#{name}" model.after_update "belongs_to_counter_cache_after_update_for_#{name}" klass = reflection.class_name.safe_constantize |