From 3395efb781f3199673dcac38703109bf20e12bf5 Mon Sep 17 00:00:00 2001 From: Franck Verrot Date: Sat, 15 May 2010 08:18:10 +0200 Subject: [PATCH] n AR::Base clone timestamps (ie: (created|updated)(at|on)) are now cleared. [#4538 state:resolved] --- activerecord/lib/active_record/base.rb | 3 +++ activerecord/test/cases/base_test.rb | 12 +++++++++++- activerecord/test/cases/fixtures_test.rb | 2 ++ activerecord/test/cases/reflection_test.rb | 10 +++++----- activerecord/test/fixtures/topics.yml | 2 ++ activerecord/test/schema/schema.rb | 2 ++ 6 files changed, 25 insertions(+), 6 deletions(-) diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 650a91b..8a015e8 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1458,6 +1458,9 @@ module ActiveRecord #:nodoc: callback(:after_initialize) if respond_to_without_attributes?(:after_initialize) cloned_attributes = other.clone_attributes(:read_attribute_before_type_cast) cloned_attributes.delete(self.class.primary_key) + %w(created_at created_on updated_at updated_on).each do |attribute_name| + cloned_attributes[attribute_name] = nil if other.has_attribute?(attribute_name) + end @attributes = cloned_attributes clear_aggregation_cache @attributes_cache = {} diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index bbc4e54..abd95ce 100755 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -1424,6 +1424,16 @@ class BasicsTest < ActiveRecord::TestCase # test if attributes set as part of after_initialize are cloned correctly assert_equal topic.author_email_address, cloned_topic.author_email_address + # assert the well-known timestamps updated_at and created_at are nil + assert_nil cloned_topic.updated_at + assert_nil cloned_topic.created_at + + # make sure we did not create any extra attribute during the clone + # (this model has only _at attributes) + [:created_on, :updated_on].each do |key| + assert_equal topic.has_attribute?(key), cloned_topic.has_attribute?(key) + end + # test if saved clone object differs from original cloned_topic.save assert !cloned_topic.new_record? @@ -2225,7 +2235,7 @@ class BasicsTest < ActiveRecord::TestCase def test_inspect_instance topic = topics(:first) - assert_equal %(#), topic.inspect + assert_equal %(#), topic.inspect end def test_inspect_new_instance diff --git a/activerecord/test/cases/fixtures_test.rb b/activerecord/test/cases/fixtures_test.rb index 3ce2320..b56756f 100644 --- a/activerecord/test/cases/fixtures_test.rb +++ b/activerecord/test/cases/fixtures_test.rb @@ -78,6 +78,8 @@ class FixturesTest < ActiveRecord::TestCase t.column :written_on, :datetime t.column :bonus_time, :time t.column :last_read, :date + t.column :created_at, :datetime + t.column :updated_at, :datetime t.column :content, :string t.column :approved, :boolean, :default => true t.column :replies_count, :integer, :default => 0 diff --git a/activerecord/test/cases/reflection_test.rb b/activerecord/test/cases/reflection_test.rb index 6781862..86aea20 100644 --- a/activerecord/test/cases/reflection_test.rb +++ b/activerecord/test/cases/reflection_test.rb @@ -24,25 +24,25 @@ class ReflectionTest < ActiveRecord::TestCase def test_read_attribute_names assert_equal( - %w( id title author_name author_email_address bonus_time written_on last_read content approved replies_count parent_id parent_title type ).sort, + %w( id title author_name author_email_address bonus_time written_on last_read updated_at created_at content approved replies_count parent_id parent_title type ).sort, @first.attribute_names ) end def test_columns - assert_equal 13, Topic.columns.length + assert_equal 15, Topic.columns.length end def test_columns_are_returned_in_the_order_they_were_declared column_names = Topic.columns.map { |column| column.name } - assert_equal %w(id title author_name author_email_address written_on bonus_time last_read content approved replies_count parent_id parent_title type), column_names + assert_equal %w(id title author_name author_email_address written_on bonus_time last_read created_at updated_at content approved replies_count parent_id parent_title type), column_names end def test_content_columns content_columns = Topic.content_columns content_column_names = content_columns.map {|column| column.name} - assert_equal 9, content_columns.length - assert_equal %w(title author_name author_email_address written_on bonus_time last_read content approved parent_title).sort, content_column_names.sort + assert_equal 11, content_columns.length + assert_equal %w(title author_name author_email_address written_on bonus_time last_read content approved parent_title updated_at created_at).sort, content_column_names.sort end def test_column_string_type_and_limit diff --git a/activerecord/test/fixtures/topics.yml b/activerecord/test/fixtures/topics.yml index 1769152..6d84165 100644 --- a/activerecord/test/fixtures/topics.yml +++ b/activerecord/test/fixtures/topics.yml @@ -9,6 +9,8 @@ first: content: Have a nice day approved: false replies_count: 1 + created_at: 2010-05-08t15:28:01.00+01:00 + updated_at: 2010-05-08t15:28:02.00+01:00 second: id: 2 diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index f5fba2f..04678f7 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -473,6 +473,8 @@ ActiveRecord::Schema.define do t.datetime :written_on t.time :bonus_time t.date :last_read + t.datetime :created_at + t.datetime :updated_at # use VARCHAR2(4000) instead of CLOB datatype as CLOB data type has many limitations in # Oracle SELECT WHERE clause which causes many unit test failures if current_adapter?(:OracleAdapter) -- 1.7.0.4