mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-25 15:22:58 +00:00
Type caste to Date for date columns in AR after parsing string in attribute writer
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user