diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2009-04-16 17:25:55 -0500 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2009-04-16 17:25:55 -0500 |
commit | abb899c54e8777428b7a607774370ba29a5573bd (patch) | |
tree | 7fe740e4df2db5f96900e1d536db733da8edd42f /activerecord/test | |
parent | fdb61f02c54bda0ad5ff6d0259209113202b9307 (diff) | |
download | rails-abb899c54e8777428b7a607774370ba29a5573bd.tar.gz rails-abb899c54e8777428b7a607774370ba29a5573bd.tar.bz2 rails-abb899c54e8777428b7a607774370ba29a5573bd.zip |
Added :touch option to belongs_to associations that will touch the parent record when the current record is saved or destroyed [DHH]
Diffstat (limited to 'activerecord/test')
-rw-r--r-- | activerecord/test/cases/timestamp_test.rb | 47 | ||||
-rw-r--r-- | activerecord/test/models/pet.rb | 2 | ||||
-rw-r--r-- | activerecord/test/schema/schema.rb | 2 |
3 files changed, 49 insertions, 2 deletions
diff --git a/activerecord/test/cases/timestamp_test.rb b/activerecord/test/cases/timestamp_test.rb index e5f8fb9c1d..24b237a72b 100644 --- a/activerecord/test/cases/timestamp_test.rb +++ b/activerecord/test/cases/timestamp_test.rb @@ -1,8 +1,10 @@ require 'cases/helper' require 'models/developer' +require 'models/owner' +require 'models/pet' class TimestampTest < ActiveRecord::TestCase - fixtures :developers + fixtures :developers, :owners, :pets def setup @developer = Developer.first @@ -27,4 +29,47 @@ class TimestampTest < ActiveRecord::TestCase assert @previously_updated_at != @developer.updated_at end + + def test_touching_a_different_attribute + previously_created_at = @developer.created_at + @developer.touch(:created_at) + + assert previously_created_at != @developer.created_at + end + + def test_saving_a_record_with_a_belongs_to_that_specifies_touching_the_parent_should_update_the_parent_updated_at + pet = Pet.first + owner = pet.owner + previously_owner_updated_at = owner.updated_at + + pet.name = "Fluffy the Third" + pet.save + + assert previously_owner_updated_at != pet.owner.updated_at + end + + def test_destroying_a_record_with_a_belongs_to_that_specifies_touching_the_parent_should_update_the_parent_updated_at + pet = Pet.first + owner = pet.owner + previously_owner_updated_at = owner.updated_at + + pet.destroy + + assert previously_owner_updated_at != pet.owner.updated_at + end + + def test_saving_a_record_with_a_belongs_to_that_specifies_touching_a_specific_attribute_the_parent_should_update_that_attribute + Pet.belongs_to :owner, :touch => :happy_at + + pet = Pet.first + owner = pet.owner + previously_owner_happy_at = owner.happy_at + + pet.name = "Fluffy the Third" + pet.save + + assert previously_owner_happy_at != pet.owner.happy_at + ensure + Pet.belongs_to :owner, :touch => true + end end
\ No newline at end of file diff --git a/activerecord/test/models/pet.rb b/activerecord/test/models/pet.rb index dc1a3c5e94..a8bf94dd86 100644 --- a/activerecord/test/models/pet.rb +++ b/activerecord/test/models/pet.rb @@ -1,5 +1,5 @@ class Pet < ActiveRecord::Base set_primary_key :pet_id - belongs_to :owner + belongs_to :owner, :touch => true has_many :toys end diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index ea848a2940..5640510c96 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -281,6 +281,8 @@ ActiveRecord::Schema.define do create_table :owners, :primary_key => :owner_id ,:force => true do |t| t.string :name + t.column :updated_at, :datetime + t.column :happy_at, :datetime end |