Skip to content

Commit

Permalink
Revert "Revert "belongs_to :touch behavior now touches old associat…
Browse files Browse the repository at this point in the history
…ion when transitioning to new association" until a proper fix is found for #10197"

This reverts commit 7389df1.

Conflicts:
	activerecord/test/cases/timestamp_test.rb
  • Loading branch information
pixeltrix committed Apr 24, 2013
1 parent ed5caf0 commit 1a30cfe
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
20 changes: 20 additions & 0 deletions activerecord/CHANGELOG.md
Expand Up @@ -110,6 +110,26 @@

*Neeraj Singh*

* `belongs_to :touch` behavior now touches old association when
transitioning to new association.

class Passenger < ActiveRecord::Base
belongs_to :car, touch: true
end

car_1 = Car.create
car_2 = Car.create

passenger = Passenger.create car: car_1

passenger.car = car_2
passenger.save

Previously only car_2 would be touched. Now both car_1 and car_2
will be touched.

*Adam Gamble*

* Extract and deprecate Firebird / Sqlserver / Oracle database tasks, because
These tasks should be supported by 3rd-party adapter.

Expand Down
Expand Up @@ -67,8 +67,19 @@ def belongs_to_counter_cache_after_update_for_#{name}
def add_touch_callbacks(reflection)
mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1
def belongs_to_touch_after_save_or_destroy_for_#{name}
record = #{name}
foreign_key_field = #{reflection.foreign_key.inspect}
old_foreign_id = attribute_was(foreign_key_field)
if old_foreign_id
reflection_klass = #{reflection.klass}
old_record = reflection_klass.find_by(reflection_klass.primary_key => old_foreign_id)
if old_record
old_record.touch #{options[:touch].inspect if options[:touch] != true}
end
end
record = #{name}
unless record.nil? || record.new_record?
record.touch #{options[:touch].inspect if options[:touch] != true}
end
Expand Down
46 changes: 46 additions & 0 deletions activerecord/test/cases/timestamp_test.rb
Expand Up @@ -176,6 +176,52 @@ def self.name; 'Toy'; end
assert_not_equal time, owner.updated_at
end

def test_changing_parent_of_a_record_touches_both_new_and_old_parent_record
klass = Class.new(ActiveRecord::Base) do
def self.name; 'Toy'; end
belongs_to :pet, touch: true
end

toy1 = klass.find(1)
old_pet = toy1.pet

toy2 = klass.find(2)
new_pet = toy2.pet
time = 3.days.ago.at_beginning_of_hour

old_pet.update_columns(updated_at: time)
new_pet.update_columns(updated_at: time)

toy1.pet = new_pet
toy1.save!

old_pet.reload
new_pet.reload

assert_not_equal time, new_pet.updated_at
assert_not_equal time, old_pet.updated_at
end

def test_clearing_association_touches_the_old_record
klass = Class.new(ActiveRecord::Base) do
def self.name; 'Toy'; end
belongs_to :pet, touch: true
end

toy = klass.find(1)
pet = toy.pet
time = 3.days.ago.at_beginning_of_hour

pet.update_columns(updated_at: time)

toy.pet = nil
toy.save!

pet.reload

assert_not_equal time, pet.updated_at
end

def test_timestamp_attributes_for_create
toy = Toy.first
assert_equal toy.send(:timestamp_attributes_for_create), [:created_at, :created_on]
Expand Down

0 comments on commit 1a30cfe

Please sign in to comment.