From b3ebd0246e83e8da034a5b8de1c349a8cefef391 Mon Sep 17 00:00:00 2001 From: Luis Hurtado Date: Sun, 21 Dec 2008 18:52:52 -0500 Subject: [PATCH] Creation of model instances support custom updated_at and updated_on attributes. --- activerecord/lib/active_record/timestamp.rb | 4 +- activerecord/test/cases/base_test.rb | 33 +++++++++++++++++++++++++++ activerecord/test/models/forum.rb | 2 + activerecord/test/schema/schema.rb | 8 ++++++ 4 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 activerecord/test/models/forum.rb diff --git a/activerecord/lib/active_record/timestamp.rb b/activerecord/lib/active_record/timestamp.rb index a9e0efa..8dbe80a 100644 --- a/activerecord/lib/active_record/timestamp.rb +++ b/activerecord/lib/active_record/timestamp.rb @@ -23,8 +23,8 @@ module ActiveRecord write_attribute('created_at', t) if respond_to?(:created_at) && created_at.nil? write_attribute('created_on', t) if respond_to?(:created_on) && created_on.nil? - write_attribute('updated_at', t) if respond_to?(:updated_at) - write_attribute('updated_on', t) if respond_to?(:updated_on) + write_attribute('updated_at', t) if respond_to?(:updated_at) && updated_at.nil? + write_attribute('updated_on', t) if respond_to?(:updated_on) && updated_on.nil? end create_without_timestamps end diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 5f54931..f935981 100755 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -16,6 +16,7 @@ require 'models/post' require 'models/comment' require 'models/minimalistic' require 'models/warehouse_thing' +require 'models/forum' require 'rexml/document' class Category < ActiveRecord::Base; end @@ -2071,6 +2072,38 @@ class BasicsTest < ActiveRecord::TestCase ActiveRecord::Base.logger = original_logger end + def test_create_with_custom_created_at + # create forum with custom updated_at + custom_created_at = 1.hour.ago.beginning_of_day + forum = Forum.create(:title => "The Colombian Forum", :created_at => custom_created_at) + + assert_equal custom_created_at, forum[:created_at] + end + + def test_create_with_custom_created_on + # create forum with custom updated_at + custom_created_on = 1.hour.ago.beginning_of_day + forum = Forum.create(:title => "The Colombian Forum", :created_on => custom_created_on) + + assert_equal custom_created_on, forum[:created_on] + end + + def test_create_with_custom_updated_at + # create forum with custom updated_at + custom_updated_at = 1.hour.ago.beginning_of_day + forum = Forum.create(:title => "The Colombian Forum", :updated_at => custom_updated_at) + + assert_equal custom_updated_at, forum[:updated_at] + end + + def test_create_with_custom_updated_on + # create forum with custom updated_at + custom_updated_on = 1.hour.ago.beginning_of_day + forum = Forum.create(:title => "The Colombian Forum", :updated_on => custom_updated_on) + + assert_equal custom_updated_on, forum[:updated_on] + end + private def with_kcode(kcode) if RUBY_VERSION < '1.9' diff --git a/activerecord/test/models/forum.rb b/activerecord/test/models/forum.rb new file mode 100644 index 0000000..3c571fd --- /dev/null +++ b/activerecord/test/models/forum.rb @@ -0,0 +1,2 @@ +class Forum < ActiveRecord::Base +end diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index fbacc69..a3781cb 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -154,6 +154,14 @@ ActiveRecord::Schema.define do t.string :name end + create_table :forums, :force => true do |t| + t.string :title, :null => false + t.datetime :created_at + t.datetime :created_on + t.datetime :updated_at + t.datetime :updated_on + end + create_table :items, :force => true do |t| t.column :name, :integer end -- 1.5.5.1