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

@ -31,14 +31,11 @@ module ValidatesTimeliness
end
def define_timeliness_write_method(attr_name)
type = timeliness_attribute_type(attr_name)
timezone_aware = timeliness_attribute_timezone_aware?(attr_name)
method_body, line = <<-EOV, __LINE__ + 1
def #{attr_name}=(value)
@timeliness_cache ||= {}
@timeliness_cache["#{attr_name}"] = value
#{ "value = Timeliness::Parser.parse(value, :#{type}, :zone => (:current if #{timezone_aware})) if value.is_a?(String)" if ValidatesTimeliness.use_plugin_parser }
#{ timeliness_type_cast_code(attr_name) if ValidatesTimeliness.use_plugin_parser }
super
end
EOV
@ -54,6 +51,13 @@ module ValidatesTimeliness
generated_timeliness_methods.module_eval(method_body, __FILE__, line)
end
def timeliness_type_cast_code(attr_name)
type = timeliness_attribute_type(attr_name)
timezone_aware = timeliness_attribute_timezone_aware?(attr_name)
"value = Timeliness::Parser.parse(value, :#{type}, :zone => (:current if #{timezone_aware})) if value.is_a?(String)"
end
def generated_timeliness_methods
@generated_timeliness_methods ||= Module.new.tap { |m| include(m) }
end

View File

@ -18,6 +18,18 @@ module ValidatesTimeliness
def timeliness_attribute_type(attr_name)
columns_hash[attr_name.to_s].type
end
def timeliness_type_cast_code(attr_name)
type = timeliness_attribute_type(attr_name)
timezone_aware = timeliness_attribute_timezone_aware?(attr_name)
<<-END
if value.is_a?(String)
value = Timeliness::Parser.parse(value, :#{type}, :zone => (:current if #{timezone_aware}))
value = value.to_date if value && :#{type} == :date
end
END
end
end
module InstanceMethods

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