mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-23 14:27:52 +00:00
add new generalised method to get raw value without dependence on before_type_cast which may not be supported in ORMs call super to define full ORM write method default
58 lines
1.4 KiB
Ruby
58 lines
1.4 KiB
Ruby
module ValidatesTimeliness
|
|
module AttributeMethods
|
|
extend ActiveSupport::Concern
|
|
|
|
included do
|
|
if attribute_method_matchers.any? {|m| m.suffix == "_before_type_cast" && m.prefix.blank? }
|
|
extend BeforeTypeCastMethods
|
|
end
|
|
end
|
|
|
|
module ClassMethods
|
|
|
|
protected
|
|
|
|
def define_method_attribute=(attr_name)
|
|
if timeliness_validated_attributes.include?(attr_name)
|
|
method_body, line = <<-EOV, __LINE__ + 1
|
|
def #{attr_name}=(value)
|
|
@attributes_cache ||= {}
|
|
@attributes_cache["_#{attr_name}_before_type_cast"] = value
|
|
super
|
|
end
|
|
EOV
|
|
class_eval(method_body, __FILE__, line)
|
|
end
|
|
super rescue(NoMethodError)
|
|
end
|
|
|
|
end
|
|
|
|
module InstanceMethods
|
|
|
|
def _timeliness_raw_value_for(attr_name)
|
|
@attributes_cache && @attributes_cache["_#{attr_name}_before_type_cast"]
|
|
end
|
|
|
|
end
|
|
|
|
module BeforeTypeCastMethods
|
|
|
|
def define_method_attribute_before_type_cast(attr_name)
|
|
if timeliness_validated_attributes.include?(attr_name)
|
|
method_body, line = <<-EOV, __LINE__ + 1
|
|
def #{attr_name}_before_type_cast
|
|
_timeliness_raw_value_for('#{attr_name}')
|
|
end
|
|
EOV
|
|
class_eval(method_body, __FILE__, line)
|
|
else
|
|
super rescue(NoMethodError)
|
|
end
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
end
|