Permalink
Browse files

Prevent touch on belongs_to parent if marked for destruction

  • Loading branch information...
1 parent b65b220 commit a808977a6882fe27bbdd42733c6e46aa073ff4a3 @adzap committed Jan 13, 2013
Showing with 10 additions and 6 deletions.
  1. +10 −6 activerecord/lib/active_record/associations/builder/belongs_to.rb
@@ -45,18 +45,22 @@ def belongs_to_counter_cache_before_destroy_for_#{name}
def add_touch_callbacks(reflection)
mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1
- def belongs_to_touch_after_save_or_destroy_for_#{name}
+ def belongs_to_touch_after_save_for_#{name}
record = #{name}
+ record.touch(#{options[:touch].inspect if options[:touch] != true}) unless record.nil?
+ end
- unless record.nil?
- record.touch #{options[:touch].inspect if options[:touch] != true}
+ def belongs_to_touch_after_destroy_for_#{name}
+ unless marked_for_destruction?
+ record = #{name}
+ record.touch(#{options[:touch].inspect if options[:touch] != true}) unless record.nil?
end
end
CODE
- 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 "belongs_to_touch_after_save_for_#{name}"
+ model.after_touch "belongs_to_touch_after_save_for_#{name}"
+ model.after_destroy "belongs_to_touch_after_destroy_for_#{name}"
end
def valid_dependent_options

0 comments on commit a808977

Please sign in to comment.