aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2013-06-11 11:43:04 -0700
committerAaron Patterson <aaron.patterson@gmail.com>2013-06-11 11:43:04 -0700
commitebd3aedbb477f7e5f3416e781fc82ead7e27f872 (patch)
tree65487be13edcc37f753740df8426ef3f41368825 /activerecord
parent0d3589ea12d4b564f61659ad47d78bcfbc623a91 (diff)
downloadrails-ebd3aedbb477f7e5f3416e781fc82ead7e27f872.tar.gz
rails-ebd3aedbb477f7e5f3416e781fc82ead7e27f872.tar.bz2
rails-ebd3aedbb477f7e5f3416e781fc82ead7e27f872.zip
remove evaled belongs_to counter cache method
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/associations/builder/belongs_to.rb30
1 files changed, 22 insertions, 8 deletions
diff --git a/activerecord/lib/active_record/associations/builder/belongs_to.rb b/activerecord/lib/active_record/associations/builder/belongs_to.rb
index f2b6a8dfb5..2483ea3db3 100644
--- a/activerecord/lib/active_record/associations/builder/belongs_to.rb
+++ b/activerecord/lib/active_record/associations/builder/belongs_to.rb
@@ -25,18 +25,27 @@ module ActiveRecord::Associations::Builder
private
+ def add_counter_cache_methods(mixin)
+ return if mixin.method_defined? :belongs_to_counter_cache_after_create
+
+ mixin.class_eval do
+ def belongs_to_counter_cache_after_create(association, reflection)
+ if record = send(association.name)
+ cache_column = reflection.counter_cache_column
+ record.class.increment_counter(cache_column, record.id)
+ @_after_create_counter_called = true
+ end
+ end
+ end
+ end
+
def add_counter_cache_callbacks(reflection)
cache_column = reflection.counter_cache_column
foreign_key = reflection.foreign_key
- mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1
- def belongs_to_counter_cache_after_create_for_#{name}
- if record = #{name}
- record.class.increment_counter(:#{cache_column}, record.id)
- @_after_create_counter_called = true
- end
- end
+ 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}
@@ -64,7 +73,12 @@ module ActiveRecord::Associations::Builder
end
CODE
- model.after_create "belongs_to_counter_cache_after_create_for_#{name}"
+ association = self
+
+ model.after_create lambda { |o|
+ o.belongs_to_counter_cache_after_create(association, reflection)
+ }
+
model.before_destroy "belongs_to_counter_cache_before_destroy_for_#{name}"
model.after_update "belongs_to_counter_cache_after_update_for_#{name}"