Permalink
Browse files

Added proper handling of time fields that are turned into Time object…

…s 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
  • Loading branch information...
1 parent 50f333b commit 0daa29ece29191b288fe86d3616bea0357325419 @dhh dhh committed Dec 1, 2004
View
@@ -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]
@@ -182,6 +182,7 @@ def klass
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 @@ def type_cast(value)
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 @@ def string_to_time(string)
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 @@ def simplified_type(field_type)
: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
@@ -147,6 +147,11 @@ def test_preserving_date_objects
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 @@ def test_default_values
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 @@ def test_multiparameter_attributes_on_time_with_empty_seconds
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
@@ -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,
@@ -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,
@@ -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,
@@ -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
@@ -15,17 +15,17 @@ def setup
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

0 comments on commit 0daa29e

Please sign in to comment.