2011-06-06 14:17:44 -04:00
|
|
|
require 'cases/helper'
|
2009-04-16 17:48:07 -04:00
|
|
|
require 'models/developer'
|
2009-04-16 18:25:55 -04:00
|
|
|
require 'models/owner'
|
|
|
|
require 'models/pet'
|
2010-08-02 10:16:02 -04:00
|
|
|
require 'models/toy'
|
2010-08-24 09:56:26 -04:00
|
|
|
require 'models/car'
|
2010-09-24 13:35:35 -04:00
|
|
|
require 'models/task'
|
2009-04-16 17:48:07 -04:00
|
|
|
|
|
|
|
class TimestampTest < ActiveRecord::TestCase
|
2010-09-24 13:35:35 -04:00
|
|
|
fixtures :developers, :owners, :pets, :toys, :cars, :tasks
|
2009-04-16 17:48:07 -04:00
|
|
|
|
|
|
|
def setup
|
|
|
|
@developer = Developer.first
|
2013-11-05 07:44:16 -05:00
|
|
|
@owner = Owner.first
|
2012-07-24 19:20:03 -04:00
|
|
|
@developer.update_columns(updated_at: Time.now.prev_month)
|
2009-04-16 17:48:07 -04:00
|
|
|
@previously_updated_at = @developer.updated_at
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_saving_a_changed_record_updates_its_timestamp
|
|
|
|
@developer.name = "Jack Bauer"
|
|
|
|
@developer.save!
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2010-05-19 15:14:51 -04:00
|
|
|
assert_not_equal @previously_updated_at, @developer.updated_at
|
2009-04-16 17:48:07 -04:00
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2009-04-16 17:48:07 -04:00
|
|
|
def test_saving_a_unchanged_record_doesnt_update_its_timestamp
|
|
|
|
@developer.save!
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2010-05-19 15:14:51 -04:00
|
|
|
assert_equal @previously_updated_at, @developer.updated_at
|
2009-04-16 17:48:07 -04:00
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2009-04-16 17:48:07 -04:00
|
|
|
def test_touching_a_record_updates_its_timestamp
|
2010-07-09 14:11:51 -04:00
|
|
|
previous_salary = @developer.salary
|
|
|
|
@developer.salary = previous_salary + 10000
|
2009-04-16 17:48:07 -04:00
|
|
|
@developer.touch
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2010-05-19 15:14:51 -04:00
|
|
|
assert_not_equal @previously_updated_at, @developer.updated_at
|
2010-07-09 14:11:51 -04:00
|
|
|
assert_equal previous_salary + 10000, @developer.salary
|
|
|
|
assert @developer.salary_changed?, 'developer salary should have changed'
|
|
|
|
assert @developer.changed?, 'developer should be marked as changed'
|
|
|
|
@developer.reload
|
|
|
|
assert_equal previous_salary, @developer.salary
|
2009-04-16 17:48:07 -04:00
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2011-06-07 17:21:05 -04:00
|
|
|
def test_touching_a_record_with_default_scope_that_excludes_it_updates_its_timestamp
|
2011-06-06 03:30:05 -04:00
|
|
|
developer = @developer.becomes(DeveloperCalledJamis)
|
|
|
|
|
|
|
|
developer.touch
|
|
|
|
assert_not_equal @previously_updated_at, developer.updated_at
|
|
|
|
developer.reload
|
|
|
|
assert_not_equal @previously_updated_at, developer.updated_at
|
|
|
|
end
|
|
|
|
|
2010-08-24 09:20:02 -04:00
|
|
|
def test_saving_when_record_timestamps_is_false_doesnt_update_its_timestamp
|
|
|
|
Developer.record_timestamps = false
|
|
|
|
@developer.name = "John Smith"
|
|
|
|
@developer.save!
|
|
|
|
|
|
|
|
assert_equal @previously_updated_at, @developer.updated_at
|
|
|
|
ensure
|
|
|
|
Developer.record_timestamps = true
|
|
|
|
end
|
|
|
|
|
2011-10-26 13:20:12 -04:00
|
|
|
def test_saving_when_instance_record_timestamps_is_false_doesnt_update_its_timestamp
|
|
|
|
@developer.record_timestamps = false
|
|
|
|
assert Developer.record_timestamps
|
|
|
|
|
|
|
|
@developer.name = "John Smith"
|
|
|
|
@developer.save!
|
|
|
|
|
|
|
|
assert_equal @previously_updated_at, @developer.updated_at
|
|
|
|
end
|
|
|
|
|
2010-09-24 13:35:35 -04:00
|
|
|
def test_touching_an_attribute_updates_timestamp
|
2009-04-16 18:25:55 -04:00
|
|
|
previously_created_at = @developer.created_at
|
|
|
|
@developer.touch(:created_at)
|
|
|
|
|
2010-07-09 14:11:51 -04:00
|
|
|
assert !@developer.created_at_changed? , 'created_at should not be changed'
|
|
|
|
assert !@developer.changed?, 'record should not be changed'
|
2010-05-19 15:14:51 -04:00
|
|
|
assert_not_equal previously_created_at, @developer.created_at
|
2010-07-09 14:11:51 -04:00
|
|
|
assert_not_equal @previously_updated_at, @developer.updated_at
|
2009-04-16 18:25:55 -04:00
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2010-09-24 13:35:35 -04:00
|
|
|
def test_touching_an_attribute_updates_it
|
|
|
|
task = Task.first
|
|
|
|
previous_value = task.ending
|
|
|
|
task.touch(:ending)
|
|
|
|
assert_not_equal previous_value, task.ending
|
|
|
|
assert_in_delta Time.now, task.ending, 1
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_touching_a_record_without_timestamps_is_unexceptional
|
|
|
|
assert_nothing_raised { Car.first.touch }
|
2010-08-24 09:56:26 -04:00
|
|
|
end
|
|
|
|
|
2013-11-05 07:44:16 -05:00
|
|
|
def test_touching_a_no_touching_object
|
|
|
|
Developer.no_touching do
|
|
|
|
assert @developer.no_touching?
|
|
|
|
assert !@owner.no_touching?
|
|
|
|
@developer.touch
|
|
|
|
end
|
|
|
|
|
|
|
|
assert !@developer.no_touching?
|
|
|
|
assert !@owner.no_touching?
|
|
|
|
assert_equal @previously_updated_at, @developer.updated_at
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_touching_related_objects
|
|
|
|
@owner = Owner.first
|
|
|
|
@previously_updated_at = @owner.updated_at
|
|
|
|
|
|
|
|
Owner.no_touching do
|
|
|
|
@owner.pets.first.touch
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal @previously_updated_at, @owner.updated_at
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_global_no_touching
|
|
|
|
ActiveRecord::Base.no_touching do
|
|
|
|
assert @developer.no_touching?
|
|
|
|
assert @owner.no_touching?
|
|
|
|
@developer.touch
|
|
|
|
end
|
|
|
|
|
|
|
|
assert !@developer.no_touching?
|
|
|
|
assert !@owner.no_touching?
|
|
|
|
assert_equal @previously_updated_at, @developer.updated_at
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_no_touching_threadsafe
|
|
|
|
Thread.new do
|
|
|
|
Developer.no_touching do
|
|
|
|
assert @developer.no_touching?
|
|
|
|
|
|
|
|
sleep(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert !@developer.no_touching?
|
|
|
|
end
|
|
|
|
|
2009-04-16 18:25:55 -04:00
|
|
|
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
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2009-04-16 18:25:55 -04:00
|
|
|
pet.name = "Fluffy the Third"
|
|
|
|
pet.save
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2010-05-19 15:14:51 -04:00
|
|
|
assert_not_equal previously_owner_updated_at, pet.owner.updated_at
|
2009-04-16 18:25:55 -04:00
|
|
|
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
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2009-04-16 18:25:55 -04:00
|
|
|
pet.destroy
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2010-05-19 15:14:51 -04:00
|
|
|
assert_not_equal previously_owner_updated_at, pet.owner.updated_at
|
2009-04-16 18:25:55 -04:00
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2013-02-26 10:55:18 -05:00
|
|
|
def test_saving_a_new_record_belonging_to_invalid_parent_with_touch_should_not_raise_exception
|
|
|
|
klass = Class.new(Owner) do
|
|
|
|
def self.name; 'Owner'; end
|
|
|
|
validate { errors.add(:base, :invalid) }
|
|
|
|
end
|
|
|
|
|
|
|
|
pet = Pet.new(owner: klass.new)
|
|
|
|
pet.save!
|
|
|
|
|
|
|
|
assert pet.owner.new_record?
|
|
|
|
end
|
|
|
|
|
2009-04-16 18:25:55 -04:00
|
|
|
def test_saving_a_record_with_a_belongs_to_that_specifies_touching_a_specific_attribute_the_parent_should_update_that_attribute
|
2012-08-10 12:42:48 -04:00
|
|
|
klass = Class.new(ActiveRecord::Base) do
|
|
|
|
def self.name; 'Pet'; end
|
|
|
|
belongs_to :owner, :touch => :happy_at
|
|
|
|
end
|
2009-04-16 18:25:55 -04:00
|
|
|
|
2012-08-10 12:42:48 -04:00
|
|
|
pet = klass.first
|
2009-04-16 18:25:55 -04:00
|
|
|
owner = pet.owner
|
|
|
|
previously_owner_happy_at = owner.happy_at
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2009-04-16 18:25:55 -04:00
|
|
|
pet.name = "Fluffy the Third"
|
|
|
|
pet.save
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2010-05-19 15:14:51 -04:00
|
|
|
assert_not_equal previously_owner_happy_at, pet.owner.happy_at
|
2009-04-16 18:25:55 -04:00
|
|
|
end
|
2010-08-02 05:07:43 -04:00
|
|
|
|
2010-08-12 12:43:15 -04:00
|
|
|
def test_touching_a_record_with_a_belongs_to_that_uses_a_counter_cache_should_update_the_parent
|
2012-08-10 12:42:48 -04:00
|
|
|
klass = Class.new(ActiveRecord::Base) do
|
|
|
|
def self.name; 'Pet'; end
|
|
|
|
belongs_to :owner, :counter_cache => :use_count, :touch => true
|
|
|
|
end
|
2010-08-12 12:43:15 -04:00
|
|
|
|
2012-08-10 12:42:48 -04:00
|
|
|
pet = klass.first
|
2010-08-12 12:43:15 -04:00
|
|
|
owner = pet.owner
|
2012-07-24 19:20:03 -04:00
|
|
|
owner.update_columns(happy_at: 3.days.ago)
|
2010-08-12 12:43:15 -04:00
|
|
|
previously_owner_updated_at = owner.updated_at
|
|
|
|
|
|
|
|
pet.name = "I'm a parrot"
|
|
|
|
pet.save
|
|
|
|
|
|
|
|
assert_not_equal previously_owner_updated_at, pet.owner.updated_at
|
|
|
|
end
|
|
|
|
|
2010-08-02 05:07:43 -04:00
|
|
|
def test_touching_a_record_touches_parent_record_and_grandparent_record
|
2012-08-10 12:42:48 -04:00
|
|
|
klass = Class.new(ActiveRecord::Base) do
|
|
|
|
def self.name; 'Toy'; end
|
|
|
|
belongs_to :pet, :touch => true
|
|
|
|
end
|
2010-08-02 05:07:43 -04:00
|
|
|
|
2012-08-10 12:42:48 -04:00
|
|
|
toy = klass.first
|
2010-08-02 05:07:43 -04:00
|
|
|
pet = toy.pet
|
|
|
|
owner = pet.owner
|
2011-03-27 18:12:28 -04:00
|
|
|
time = 3.days.ago
|
2010-08-02 05:07:43 -04:00
|
|
|
|
2012-07-24 19:20:03 -04:00
|
|
|
owner.update_columns(updated_at: time)
|
2010-08-02 05:07:43 -04:00
|
|
|
toy.touch
|
2010-08-02 18:10:34 -04:00
|
|
|
owner.reload
|
2010-08-02 05:07:43 -04:00
|
|
|
|
2010-08-02 10:16:02 -04:00
|
|
|
assert_not_equal time, owner.updated_at
|
2010-08-02 05:07:43 -04:00
|
|
|
end
|
2011-01-25 17:20:57 -05:00
|
|
|
|
2013-04-22 21:08:44 -04:00
|
|
|
def test_touching_a_record_touches_polymorphic_record
|
|
|
|
klass = Class.new(ActiveRecord::Base) do
|
|
|
|
def self.name; 'Toy'; end
|
|
|
|
end
|
|
|
|
|
|
|
|
wheel_klass = Class.new(ActiveRecord::Base) do
|
|
|
|
def self.name; 'Wheel'; end
|
|
|
|
belongs_to :wheelable, :polymorphic => true, :touch => true
|
|
|
|
end
|
|
|
|
|
|
|
|
toy = klass.first
|
|
|
|
time = 3.days.ago
|
|
|
|
toy.update_columns(updated_at: time)
|
|
|
|
|
|
|
|
wheel = wheel_klass.new
|
|
|
|
wheel.wheelable = toy
|
|
|
|
wheel.save
|
|
|
|
wheel.touch
|
|
|
|
|
|
|
|
assert_not_equal time, toy.updated_at
|
|
|
|
end
|
|
|
|
|
2013-04-24 08:37:33 -04:00
|
|
|
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
|
|
|
|
|
2013-12-05 09:57:05 -05:00
|
|
|
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
|
2013-04-22 21:08:44 -04:00
|
|
|
end
|
|
|
|
|
2013-12-05 09:57:05 -05:00
|
|
|
wheel_class = Class.new(ActiveRecord::Base) do
|
2013-04-22 21:08:44 -04:00
|
|
|
def self.name; 'Wheel'; end
|
|
|
|
belongs_to :wheelable, :polymorphic => true, :touch => true
|
|
|
|
end
|
|
|
|
|
2013-12-05 09:57:05 -05:00
|
|
|
car1 = car_class.find(1)
|
|
|
|
car2 = car_class.find(2)
|
2013-04-22 21:08:44 -04:00
|
|
|
|
2013-12-05 09:57:05 -05:00
|
|
|
wheel = wheel_class.create!(wheelable: car1)
|
2013-04-22 21:08:44 -04:00
|
|
|
|
|
|
|
time = 3.days.ago.at_beginning_of_hour
|
|
|
|
|
2013-12-05 09:57:05 -05:00
|
|
|
car1.update_columns(updated_at: time)
|
|
|
|
car2.update_columns(updated_at: time)
|
2013-04-22 21:08:44 -04:00
|
|
|
|
2013-12-05 09:57:05 -05:00
|
|
|
wheel.wheelable = car2
|
2013-04-22 21:08:44 -04:00
|
|
|
wheel.save!
|
|
|
|
|
2013-12-05 09:57:05 -05:00
|
|
|
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!
|
2013-04-22 21:08:44 -04:00
|
|
|
|
2013-12-05 09:57:05 -05:00
|
|
|
assert_not_equal time, car.reload.updated_at
|
|
|
|
assert_not_equal time, toy.reload.updated_at
|
2013-04-22 21:08:44 -04:00
|
|
|
end
|
|
|
|
|
2013-04-24 08:37:33 -04:00
|
|
|
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
|
|
|
|
|
2011-01-25 17:20:57 -05:00
|
|
|
def test_timestamp_attributes_for_create
|
|
|
|
toy = Toy.first
|
2013-11-20 07:58:09 -05:00
|
|
|
assert_equal [:created_at, :created_on], toy.send(:timestamp_attributes_for_create)
|
2011-01-25 17:20:57 -05:00
|
|
|
end
|
2011-01-25 17:21:33 -05:00
|
|
|
|
|
|
|
def test_timestamp_attributes_for_update
|
|
|
|
toy = Toy.first
|
2013-11-20 07:58:09 -05:00
|
|
|
assert_equal [:updated_at, :updated_on], toy.send(:timestamp_attributes_for_update)
|
2011-01-25 17:21:33 -05:00
|
|
|
end
|
2011-01-25 17:23:19 -05:00
|
|
|
|
|
|
|
def test_all_timestamp_attributes
|
|
|
|
toy = Toy.first
|
2013-11-20 07:58:09 -05:00
|
|
|
assert_equal [:created_at, :created_on, :updated_at, :updated_on], toy.send(:all_timestamp_attributes)
|
2011-01-25 17:23:19 -05:00
|
|
|
end
|
2011-01-25 17:27:27 -05:00
|
|
|
|
|
|
|
def test_timestamp_attributes_for_create_in_model
|
|
|
|
toy = Toy.first
|
2013-11-20 07:58:09 -05:00
|
|
|
assert_equal [:created_at], toy.send(:timestamp_attributes_for_create_in_model)
|
2011-01-25 17:27:27 -05:00
|
|
|
end
|
2011-01-25 17:28:31 -05:00
|
|
|
|
|
|
|
def test_timestamp_attributes_for_update_in_model
|
|
|
|
toy = Toy.first
|
2013-11-20 07:58:09 -05:00
|
|
|
assert_equal [:updated_at], toy.send(:timestamp_attributes_for_update_in_model)
|
2011-01-25 17:28:31 -05:00
|
|
|
end
|
2011-01-25 17:33:55 -05:00
|
|
|
|
|
|
|
def test_all_timestamp_attributes_in_model
|
|
|
|
toy = Toy.first
|
2013-11-20 07:58:09 -05:00
|
|
|
assert_equal [:created_at, :updated_at], toy.send(:all_timestamp_attributes_in_model)
|
2011-01-25 17:33:55 -05:00
|
|
|
end
|
2010-05-19 15:14:51 -04:00
|
|
|
end
|