From 701e48e4acef827522f5c53e1602edb1485ea21b Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 11 Jun 2013 13:40:39 -0700 Subject: stop adding a new method for touch callbacks --- .../associations/builder/belongs_to.rb | 50 +++++++++------------- 1 file changed, 21 insertions(+), 29 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/associations/builder/belongs_to.rb b/activerecord/lib/active_record/associations/builder/belongs_to.rb index f2b64fb6f4..d4e1a0dda1 100644 --- a/activerecord/lib/active_record/associations/builder/belongs_to.rb +++ b/activerecord/lib/active_record/associations/builder/belongs_to.rb @@ -91,47 +91,39 @@ module ActiveRecord::Associations::Builder klass.attr_readonly cache_column if klass && klass.respond_to?(:attr_readonly) end - def add_touch_methods(mixin) - return if mixin.method_defined? :belongs_to_touch_after_save_or_destroy - - mixin.class_eval do - def belongs_to_touch_after_save_or_destroy(foreign_key, name, touch) - old_foreign_id = attribute_was(foreign_key) - - if old_foreign_id - klass = association(name).klass - old_record = klass.find_by(klass.primary_key => old_foreign_id) - - if old_record - if touch != true - old_record.touch touch - else - old_record.touch - end - end + def self.touch_record(o, foreign_key, name, touch) # :nodoc: + old_foreign_id = o.attribute_was(foreign_key) + + if old_foreign_id + klass = o.association(name).klass + old_record = klass.find_by(klass.primary_key => old_foreign_id) + + if old_record + if touch != true + old_record.touch touch + else + old_record.touch end + end + end - record = send name - unless record.nil? || record.new_record? - if touch != true - record.touch touch - else - record.touch - end - end + record = o.send name + unless record.nil? || record.new_record? + if touch != true + record.touch touch + else + record.touch end end end def add_touch_callbacks(reflection) - add_touch_methods(mixin) - foreign_key = reflection.foreign_key n = name touch = options[:touch] callback = lambda { |record| - record.belongs_to_touch_after_save_or_destroy foreign_key, n, touch + BelongsTo.touch_record(record, foreign_key, n, touch) } model.after_save callback -- cgit v1.2.3