Type caste to Date for date columns in AR after parsing string in attribute writer

This commit is contained in:
Adam Meehan
2011-04-27 07:37:26 +10:00
parent e3dea5aebd
commit ea7c9ec7be
5 changed files with 65 additions and 18 deletions

View File

@@ -60,11 +60,6 @@ describe ValidatesTimeliness::AttributeMethods do
r.birth_date = '2010-01-01'
end
it 'should parse string as current timezone' do
r = PersonWithParser.new
r.birth_datetime = '2010-01-01 12:00'
r.birth_datetime.zone == Time.zone.name
end
end
end

View File

@@ -47,10 +47,32 @@ describe ValidatesTimeliness, 'ActiveRecord' do
r.birth_date = '2010-01-01'
end
it 'should parse string as current timezone' do
r = EmployeeWithParser.new
r.birth_datetime = '2010-06-01 12:00'
r.birth_datetime.utc_offset.should == 10.hours
context "for a date column" do
it 'should store a date value after parsing string' do
r = EmployeeWithParser.new
r.birth_date = '2010-01-01'
r.birth_date.should be_kind_of(Date)
r.birth_date.should == Date.new(2010, 1, 1)
end
end
context "for a datetime column" do
with_config(:default_timezone, 'Australia/Melbourne')
it 'should parse string into Time value' do
r = EmployeeWithParser.new
r.birth_datetime = '2010-01-01 12:00'
r.birth_datetime.should be_kind_of(Time)
end
it 'should parse string as current timezone' do
r = EmployeeWithParser.new
r.birth_datetime = '2010-06-01 12:00'
r.birth_datetime.utc_offset.should == Time.zone.utc_offset
end
end
end
end

View File

@@ -60,10 +60,24 @@ describe ValidatesTimeliness, 'Mongoid' do
r.publish_date = '2010-01-01'
end
it 'should parse string into Time value' do
r = Article.new
r.publish_datetime = '2010-01-01 12:00'
r.publish_datetime.should == Time.utc(2010,1,1,12,0)
context "for a date column" do
it 'should store a date value after parsing string' do
r = Article.new
r.publish_date = '2010-01-01'
r.publish_date.should be_kind_of(Date)
r.publish_date.should == Date.new(2010, 1, 1)
end
end
context "for a datetime column" do
it 'should parse string into Time value' do
r = Article.new
r.publish_datetime = '2010-01-01 12:00'
r.publish_datetime.should be_kind_of(Time)
r.publish_datetime.should == Time.utc(2010,1,1,12,0)
end
end
end
end
@@ -79,7 +93,7 @@ describe ValidatesTimeliness, 'Mongoid' do
context "before_type_cast method" do
it 'should not be defined if ORM does not support it' do
Article.new.should_not respond_to(:birth_datetime_before_type_cast)
Article.new.should_not respond_to(:publish_datetime_before_type_cast)
end
end
end