aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2013-06-11 13:29:59 -0700
committerAaron Patterson <aaron.patterson@gmail.com>2013-06-11 13:29:59 -0700
commit4b824b052046f3ec0cea6cad2adc3ce0bcd5c9fe (patch)
tree9e619a8d710dd21c5bb73f2fdfdb594a06f3e8ab /activerecord/lib/active_record/associations
parent3680074b4de40e52a3d304268b83bb9abb76fb1e (diff)
downloadrails-4b824b052046f3ec0cea6cad2adc3ce0bcd5c9fe.tar.gz
rails-4b824b052046f3ec0cea6cad2adc3ce0bcd5c9fe.tar.bz2
rails-4b824b052046f3ec0cea6cad2adc3ce0bcd5c9fe.zip
push the touch method outside the eval
Diffstat (limited to 'activerecord/lib/active_record/associations')
-rw-r--r--activerecord/lib/active_record/associations/builder/belongs_to.rb47
1 files changed, 34 insertions, 13 deletions
diff --git a/activerecord/lib/active_record/associations/builder/belongs_to.rb b/activerecord/lib/active_record/associations/builder/belongs_to.rb
index 6b364cdb7f..f2b64fb6f4 100644
--- a/activerecord/lib/active_record/associations/builder/belongs_to.rb
+++ b/activerecord/lib/active_record/associations/builder/belongs_to.rb
@@ -91,31 +91,52 @@ module ActiveRecord::Associations::Builder
klass.attr_readonly cache_column if klass && klass.respond_to?(:attr_readonly)
end
- def add_touch_callbacks(reflection)
- mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1
- def belongs_to_touch_after_save_or_destroy_for_#{name}
- foreign_key_field = #{reflection.foreign_key.inspect}
- old_foreign_id = attribute_was(foreign_key_field)
+ 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.inspect}).klass
+ klass = association(name).klass
old_record = klass.find_by(klass.primary_key => old_foreign_id)
if old_record
- old_record.touch #{options[:touch].inspect if options[:touch] != true}
+ if touch != true
+ old_record.touch touch
+ else
+ old_record.touch
+ end
end
end
- record = #{name}
+ record = send name
unless record.nil? || record.new_record?
- record.touch #{options[:touch].inspect if options[:touch] != true}
+ if touch != true
+ record.touch touch
+ else
+ record.touch
+ end
end
end
- CODE
+ 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
+ }
- model.after_save "belongs_to_touch_after_save_or_destroy_for_#{name}"
- model.after_touch "belongs_to_touch_after_save_or_destroy_for_#{name}"
- model.after_destroy "belongs_to_touch_after_save_or_destroy_for_#{name}"
+ model.after_save callback
+ model.after_touch callback
+ model.after_destroy callback
end
end
end