From 0daa29ece29191b288fe86d3616bea0357325419 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Wed, 1 Dec 2004 13:18:51 +0000 Subject: Added proper handling of time fields that are turned into Time objects with the dummy date of 2000/1/1 [HariSeldon] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@40 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/CHANGELOG | 2 ++ .../active_record/connection_adapters/abstract_adapter.rb | 14 +++++++++++++- activerecord/test/base_test.rb | 15 +++++++++++++++ activerecord/test/fixtures/db_definitions/mysql.sql | 1 + activerecord/test/fixtures/db_definitions/postgresql.sql | 1 + activerecord/test/fixtures/db_definitions/sqlite.sql | 1 + activerecord/test/fixtures/topics/first | 1 + activerecord/test/reflection_test.rb | 6 +++--- 8 files changed, 37 insertions(+), 4 deletions(-) diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index db41ef69bf..d4e681af1d 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *CVS* +* Added proper handling of time fields that are turned into Time objects with the dummy date of 2000/1/1 [HariSeldon] + * Added reverse order of deleting fixtures, so referential keys can be maintained #247 [Tim Bates] * Added relative path search for sqlite dbfiles in database.yml (if RAILS_ROOT is defined) #233 [bitsweat] diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb index 4689ac3ac3..301ee208a3 100755 --- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb @@ -182,6 +182,7 @@ module ActiveRecord when :float then Float when :datetime then Time when :date then Date + when :time then Time when :text, :string then String when :boolean then Object end @@ -195,6 +196,7 @@ module ActiveRecord when :integer then value.to_i when :float then value.to_f when :datetime then string_to_time(value) + when :time then string_to_dummy_time(value) when :date then string_to_date(value) when :boolean then (value == "t" or value == true ? true : false) else value @@ -220,6 +222,14 @@ module ActiveRecord Time.local(*time_array) rescue nil end + def string_to_dummy_time(string) + return string if Time === string + time_array = ParseDate.parsedate(string) + # pad the resulting array with dummy date information + time_array[0] = 2000; time_array[1] = 1; time_array[2] = 1; + Time.local(*time_array) rescue nil + end + def extract_limit(sql_type) $1.to_i if sql_type =~ /\((.*)\)/ end @@ -230,8 +240,10 @@ module ActiveRecord :integer when /float|double|decimal|numeric/i :float - when /time/i + when /datetime/i :datetime + when /time/i + :time when /date/i :date when /(c|b)lob/i, /text/i diff --git a/activerecord/test/base_test.rb b/activerecord/test/base_test.rb index 0d2278eb58..c362fd7139 100755 --- a/activerecord/test/base_test.rb +++ b/activerecord/test/base_test.rb @@ -147,6 +147,11 @@ class BasicsTest < Test::Unit::TestCase Date, Topic.find(1).last_read, "The last_read attribute should be of the Date class" ) + + assert_kind_of( + Time, Topic.find(1).bonus_time, + "The bonus_time attribute should be of the Time class" + ) end def test_preserving_time_objects @@ -311,6 +316,7 @@ class BasicsTest < Test::Unit::TestCase topic = Topic.new assert_equal 1, topic.approved assert_nil topic.written_on + assert_nil topic.bonus_time assert_nil topic.last_read topic.save @@ -426,6 +432,15 @@ class BasicsTest < Test::Unit::TestCase assert_equal Time.local(2004, 6, 24, 16, 24, 0), topic.written_on end + def test_attributes_on_dummy_time + attributes = { + "bonus_time" => "5:42:00AM" + } + topic = Topic.find(1) + topic.attributes = attributes + assert_equal Time.local(2000, 1, 1, 5, 42, 0), topic.bonus_time + end + def test_boolean b_false = Booleantest.create({ "value" => false }) false_id = b_false.id diff --git a/activerecord/test/fixtures/db_definitions/mysql.sql b/activerecord/test/fixtures/db_definitions/mysql.sql index 766c0ec71f..0b96c49dd3 100755 --- a/activerecord/test/fixtures/db_definitions/mysql.sql +++ b/activerecord/test/fixtures/db_definitions/mysql.sql @@ -23,6 +23,7 @@ CREATE TABLE `topics` ( `author_name` varchar(255) default NULL, `author_email_address` varchar(255) default NULL, `written_on` datetime default NULL, + `bonus_time` time default NULL, `last_read` date default NULL, `content` text, `approved` tinyint(1) default 1, diff --git a/activerecord/test/fixtures/db_definitions/postgresql.sql b/activerecord/test/fixtures/db_definitions/postgresql.sql index e83356627b..87db0b7f3f 100644 --- a/activerecord/test/fixtures/db_definitions/postgresql.sql +++ b/activerecord/test/fixtures/db_definitions/postgresql.sql @@ -46,6 +46,7 @@ CREATE TABLE topics ( author_name character varying(255), author_email_address character varying(255), written_on timestamp without time zone, + bonus_time time, last_read date, content text, replies_count integer default 0, diff --git a/activerecord/test/fixtures/db_definitions/sqlite.sql b/activerecord/test/fixtures/db_definitions/sqlite.sql index cb617305dc..91ca172789 100644 --- a/activerecord/test/fixtures/db_definitions/sqlite.sql +++ b/activerecord/test/fixtures/db_definitions/sqlite.sql @@ -21,6 +21,7 @@ CREATE TABLE 'topics' ( 'author_name' VARCHAR(255) DEFAULT NULL, 'author_email_address' VARCHAR(255) DEFAULT NULL, 'written_on' DATETIME DEFAULT NULL, + 'bonus_time' TIME DEFAULT NULL, 'last_read' DATE DEFAULT NULL, 'content' TEXT, 'approved' INTEGER DEFAULT 1, diff --git a/activerecord/test/fixtures/topics/first b/activerecord/test/fixtures/topics/first index 9972a578c8..2e1a88563b 100755 --- a/activerecord/test/fixtures/topics/first +++ b/activerecord/test/fixtures/topics/first @@ -3,6 +3,7 @@ title => The First Topic author_name => David author_email_address => david@loudthinking.com written_on => 2003-07-16 15:28 +bonus_time => 12:13:14 last_read => 2004-04-15 content => Have a nice day approved => 0 diff --git a/activerecord/test/reflection_test.rb b/activerecord/test/reflection_test.rb index 5d7e9d1197..33e147be77 100644 --- a/activerecord/test/reflection_test.rb +++ b/activerecord/test/reflection_test.rb @@ -15,17 +15,17 @@ class ReflectionTest < Test::Unit::TestCase def test_read_attribute_names assert_equal( - %w( id title author_name author_email_address written_on last_read content approved replies_count parent_id type ).sort, + %w( id title author_name author_email_address bonus_time written_on last_read content approved replies_count parent_id type ).sort, @first.attribute_names ) end def test_columns - assert_equal 11, Topic.columns.length + assert_equal 12, Topic.columns.length end def test_content_columns - assert_equal 7, Topic.content_columns.length + assert_equal 8, Topic.content_columns.length end def test_column_string_type_and_limit -- cgit v1.2.3