diff options
author | Yves Senn <yves.senn@gmail.com> | 2013-12-05 07:08:51 -0800 |
---|---|---|
committer | Yves Senn <yves.senn@gmail.com> | 2013-12-05 07:08:51 -0800 |
commit | 23d555a0abcbe5bb143928d33aaf66b38f63338b (patch) | |
tree | 9701a5c8a35828fd619a3af91042e2cde432f6f4 /activerecord/test | |
parent | f0c58621f104492aa1c2b6cb13ecb2d6c60541fa (diff) | |
parent | f1a646fa74290db32fb52bb373791891ab73d693 (diff) | |
download | rails-23d555a0abcbe5bb143928d33aaf66b38f63338b.tar.gz rails-23d555a0abcbe5bb143928d33aaf66b38f63338b.tar.bz2 rails-23d555a0abcbe5bb143928d33aaf66b38f63338b.zip |
Merge pull request #12646 from severin/polymorphic_belongs_to_touch
polymorphic belongs_to association with touch: true updates old record correctly
Diffstat (limited to 'activerecord/test')
-rw-r--r-- | activerecord/test/cases/timestamp_test.rb | 58 |
1 files changed, 42 insertions, 16 deletions
diff --git a/activerecord/test/cases/timestamp_test.rb b/activerecord/test/cases/timestamp_test.rb index 2953b2b2be..717e0e1866 100644 --- a/activerecord/test/cases/timestamp_test.rb +++ b/activerecord/test/cases/timestamp_test.rb @@ -272,36 +272,62 @@ class TimestampTest < ActiveRecord::TestCase assert_not_equal time, old_pet.updated_at end - def test_changing_parent_of_a_record_touches_both_new_and_old_polymorphic_parent_record - klass = Class.new(ActiveRecord::Base) do - def self.name; 'Toy'; end + def test_changing_parent_of_a_record_touches_both_new_and_old_polymorphic_parent_record_changes_within_same_class + car_class = Class.new(ActiveRecord::Base) do + def self.name; 'Car'; end end - wheel_klass = Class.new(ActiveRecord::Base) do + wheel_class = Class.new(ActiveRecord::Base) do def self.name; 'Wheel'; end belongs_to :wheelable, :polymorphic => true, :touch => true end - toy1 = klass.find(1) - toy2 = klass.find(2) + car1 = car_class.find(1) + car2 = car_class.find(2) - wheel = wheel_klass.new - wheel.wheelable = toy1 - wheel.save! + wheel = wheel_class.create!(wheelable: car1) time = 3.days.ago.at_beginning_of_hour - toy1.update_columns(updated_at: time) - toy2.update_columns(updated_at: time) + car1.update_columns(updated_at: time) + car2.update_columns(updated_at: time) - wheel.wheelable = toy2 + wheel.wheelable = car2 wheel.save! - toy1.reload - toy2.reload + assert_not_equal time, car1.reload.updated_at + assert_not_equal time, car2.reload.updated_at + end + + def test_changing_parent_of_a_record_touches_both_new_and_old_polymorphic_parent_record_changes_with_other_class + car_class = Class.new(ActiveRecord::Base) do + def self.name; 'Car'; end + end + + toy_class = Class.new(ActiveRecord::Base) do + def self.name; 'Toy'; end + end + + wheel_class = Class.new(ActiveRecord::Base) do + def self.name; 'Wheel'; end + belongs_to :wheelable, :polymorphic => true, :touch => true + end + + car = car_class.find(1) + toy = toy_class.find(3) + + wheel = wheel_class.create!(wheelable: car) + + time = 3.days.ago.at_beginning_of_hour + + car.update_columns(updated_at: time) + toy.update_columns(updated_at: time) + + wheel.wheelable = toy + wheel.save! - assert_not_equal time, toy1.updated_at - assert_not_equal time, toy2.updated_at + assert_not_equal time, car.reload.updated_at + assert_not_equal time, toy.reload.updated_at end def test_clearing_association_touches_the_old_record |