Permalink
Browse files

remove evaled belongs_to counter cache method

  • Loading branch information...
1 parent 0d3589e commit ebd3aedbb477f7e5f3416e781fc82ead7e27f872 @tenderlove tenderlove committed Jun 11, 2013
Showing with 22 additions and 8 deletions.
  1. +22 −8 activerecord/lib/active_record/associations/builder/belongs_to.rb
@@ -25,18 +25,27 @@ def valid_dependent_options
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 @@ def belongs_to_counter_cache_after_update_for_#{name}
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}"

0 comments on commit ebd3aed

Please sign in to comment.