提交 0daa29ec 编写于 作者: D David Heinemeier Hansson

Added proper handling of time fields that are turned into Time objects with...

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
上级 50f333b2
*CVS* *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 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] * Added relative path search for sqlite dbfiles in database.yml (if RAILS_ROOT is defined) #233 [bitsweat]
......
...@@ -182,6 +182,7 @@ def klass ...@@ -182,6 +182,7 @@ def klass
when :float then Float when :float then Float
when :datetime then Time when :datetime then Time
when :date then Date when :date then Date
when :time then Time
when :text, :string then String when :text, :string then String
when :boolean then Object when :boolean then Object
end end
...@@ -195,6 +196,7 @@ def type_cast(value) ...@@ -195,6 +196,7 @@ def type_cast(value)
when :integer then value.to_i when :integer then value.to_i
when :float then value.to_f when :float then value.to_f
when :datetime then string_to_time(value) when :datetime then string_to_time(value)
when :time then string_to_dummy_time(value)
when :date then string_to_date(value) when :date then string_to_date(value)
when :boolean then (value == "t" or value == true ? true : false) when :boolean then (value == "t" or value == true ? true : false)
else value else value
...@@ -220,6 +222,14 @@ def string_to_time(string) ...@@ -220,6 +222,14 @@ def string_to_time(string)
Time.local(*time_array) rescue nil Time.local(*time_array) rescue nil
end 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) def extract_limit(sql_type)
$1.to_i if sql_type =~ /\((.*)\)/ $1.to_i if sql_type =~ /\((.*)\)/
end end
...@@ -230,8 +240,10 @@ def simplified_type(field_type) ...@@ -230,8 +240,10 @@ def simplified_type(field_type)
:integer :integer
when /float|double|decimal|numeric/i when /float|double|decimal|numeric/i
:float :float
when /time/i when /datetime/i
:datetime :datetime
when /time/i
:time
when /date/i when /date/i
:date :date
when /(c|b)lob/i, /text/i when /(c|b)lob/i, /text/i
......
...@@ -147,6 +147,11 @@ def test_preserving_date_objects ...@@ -147,6 +147,11 @@ def test_preserving_date_objects
Date, Topic.find(1).last_read, Date, Topic.find(1).last_read,
"The last_read attribute should be of the Date class" "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 end
def test_preserving_time_objects def test_preserving_time_objects
...@@ -311,6 +316,7 @@ def test_default_values ...@@ -311,6 +316,7 @@ def test_default_values
topic = Topic.new topic = Topic.new
assert_equal 1, topic.approved assert_equal 1, topic.approved
assert_nil topic.written_on assert_nil topic.written_on
assert_nil topic.bonus_time
assert_nil topic.last_read assert_nil topic.last_read
topic.save topic.save
...@@ -426,6 +432,15 @@ def test_multiparameter_attributes_on_time_with_empty_seconds ...@@ -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 assert_equal Time.local(2004, 6, 24, 16, 24, 0), topic.written_on
end 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 def test_boolean
b_false = Booleantest.create({ "value" => false }) b_false = Booleantest.create({ "value" => false })
false_id = b_false.id false_id = b_false.id
......
...@@ -23,6 +23,7 @@ CREATE TABLE `topics` ( ...@@ -23,6 +23,7 @@ CREATE TABLE `topics` (
`author_name` varchar(255) default NULL, `author_name` varchar(255) default NULL,
`author_email_address` varchar(255) default NULL, `author_email_address` varchar(255) default NULL,
`written_on` datetime default NULL, `written_on` datetime default NULL,
`bonus_time` time default NULL,
`last_read` date default NULL, `last_read` date default NULL,
`content` text, `content` text,
`approved` tinyint(1) default 1, `approved` tinyint(1) default 1,
......
...@@ -46,6 +46,7 @@ CREATE TABLE topics ( ...@@ -46,6 +46,7 @@ CREATE TABLE topics (
author_name character varying(255), author_name character varying(255),
author_email_address character varying(255), author_email_address character varying(255),
written_on timestamp without time zone, written_on timestamp without time zone,
bonus_time time,
last_read date, last_read date,
content text, content text,
replies_count integer default 0, replies_count integer default 0,
......
...@@ -21,6 +21,7 @@ CREATE TABLE 'topics' ( ...@@ -21,6 +21,7 @@ CREATE TABLE 'topics' (
'author_name' VARCHAR(255) DEFAULT NULL, 'author_name' VARCHAR(255) DEFAULT NULL,
'author_email_address' VARCHAR(255) DEFAULT NULL, 'author_email_address' VARCHAR(255) DEFAULT NULL,
'written_on' DATETIME DEFAULT NULL, 'written_on' DATETIME DEFAULT NULL,
'bonus_time' TIME DEFAULT NULL,
'last_read' DATE DEFAULT NULL, 'last_read' DATE DEFAULT NULL,
'content' TEXT, 'content' TEXT,
'approved' INTEGER DEFAULT 1, 'approved' INTEGER DEFAULT 1,
......
...@@ -3,6 +3,7 @@ title => The First Topic ...@@ -3,6 +3,7 @@ title => The First Topic
author_name => David author_name => David
author_email_address => david@loudthinking.com author_email_address => david@loudthinking.com
written_on => 2003-07-16 15:28 written_on => 2003-07-16 15:28
bonus_time => 12:13:14
last_read => 2004-04-15 last_read => 2004-04-15
content => Have a nice day content => Have a nice day
approved => 0 approved => 0
......
...@@ -15,17 +15,17 @@ def setup ...@@ -15,17 +15,17 @@ def setup
def test_read_attribute_names def test_read_attribute_names
assert_equal( 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 @first.attribute_names
) )
end end
def test_columns def test_columns
assert_equal 11, Topic.columns.length assert_equal 12, Topic.columns.length
end end
def test_content_columns def test_content_columns
assert_equal 7, Topic.content_columns.length assert_equal 8, Topic.content_columns.length
end end
def test_column_string_type_and_limit def test_column_string_type_and_limit
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册