diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2013-06-11 13:40:39 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2013-06-11 13:40:39 -0700 |
commit | 701e48e4acef827522f5c53e1602edb1485ea21b (patch) | |
tree | c8477e6306bdb70c0bfc6fd859d03434ff5ff252 | |
parent | 47617ecdf7b4c6ee8dd0a5092b82c792fc1cad4a (diff) | |
download | rails-701e48e4acef827522f5c53e1602edb1485ea21b.tar.gz rails-701e48e4acef827522f5c53e1602edb1485ea21b.tar.bz2 rails-701e48e4acef827522f5c53e1602edb1485ea21b.zip |
stop adding a new method for touch callbacks
-rw-r--r-- | activerecord/lib/active_record/associations/builder/belongs_to.rb | 50 |
1 files changed, 21 insertions, 29 deletions
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 |