mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-22 22:06:45 +00:00
Refactor AR shim for v3.1 checks
This commit is contained in:
parent
5be45b00db
commit
84316bc73a
@ -41,7 +41,6 @@ module ValidatesTimeliness
|
|||||||
original_value = value
|
original_value = value
|
||||||
@timeliness_cache ||= {}
|
@timeliness_cache ||= {}
|
||||||
@timeliness_cache["#{attr_name}"] = original_value
|
@timeliness_cache["#{attr_name}"] = original_value
|
||||||
|
|
||||||
#{ "if value.is_a?(String)\n#{timeliness_type_cast_code(attr_name, 'value')}\nend" if ValidatesTimeliness.use_plugin_parser }
|
#{ "if value.is_a?(String)\n#{timeliness_type_cast_code(attr_name, 'value')}\nend" if ValidatesTimeliness.use_plugin_parser }
|
||||||
|
|
||||||
super(value)
|
super(value)
|
||||||
|
|||||||
@ -8,7 +8,9 @@ module ValidatesTimeliness
|
|||||||
end
|
end
|
||||||
|
|
||||||
included do
|
included do
|
||||||
unless ValidatesTimeliness::ORM::ActiveRecord.use_plugin_cache?
|
if ValidatesTimeliness::ORM::ActiveRecord.use_plugin_cache?
|
||||||
|
include Reload
|
||||||
|
else
|
||||||
# Just use the built-in before_type_cast retrieval
|
# Just use the built-in before_type_cast retrieval
|
||||||
alias_method :_timeliness_raw_value_for, :read_attribute_before_type_cast
|
alias_method :_timeliness_raw_value_for, :read_attribute_before_type_cast
|
||||||
end
|
end
|
||||||
@ -18,26 +20,33 @@ module ValidatesTimeliness
|
|||||||
def define_attribute_methods
|
def define_attribute_methods
|
||||||
super
|
super
|
||||||
use_before_type_cast = ValidatesTimeliness::ORM::ActiveRecord.use_plugin_cache?
|
use_before_type_cast = ValidatesTimeliness::ORM::ActiveRecord.use_plugin_cache?
|
||||||
|
define_timeliness_methods use_before_type_cast
|
||||||
|
end
|
||||||
|
|
||||||
if use_before_type_cast || ValidatesTimeliness.use_plugin_parser
|
def define_timeliness_methods(before_type_cast=false)
|
||||||
define_timeliness_methods(use_before_type_cast)
|
return if timeliness_validated_attributes.blank?
|
||||||
|
|
||||||
|
timeliness_validated_attributes.each do |attr_name|
|
||||||
|
if before_type_cast
|
||||||
|
define_timeliness_write_method(attr_name)
|
||||||
|
define_timeliness_before_type_cast_method(attr_name)
|
||||||
|
elsif ValidatesTimeliness.use_plugin_parser
|
||||||
|
define_timeliness_write_method_without_cache(attr_name)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# ActiveRecord >= 3.1.x has correct before_type_cast implementation to support plugin, except parser
|
def define_timeliness_write_method_without_cache(attr_name)
|
||||||
unless ValidatesTimeliness::ORM::ActiveRecord.use_plugin_cache?
|
|
||||||
def define_timeliness_write_method(attr_name)
|
|
||||||
method_body, line = <<-EOV, __LINE__ + 1
|
method_body, line = <<-EOV, __LINE__ + 1
|
||||||
def #{attr_name}=(value)
|
def #{attr_name}=(value)
|
||||||
original_value = value
|
original_value = value
|
||||||
if original_value.is_a?(String)\n#{timeliness_type_cast_code(attr_name, 'value')}\nend
|
if value.is_a?(String)\n#{timeliness_type_cast_code(attr_name, 'value')}\nend
|
||||||
super(value)
|
super(value)
|
||||||
@attributes['#{attr_name}'] = original_value
|
@attributes['#{attr_name}'] = original_value
|
||||||
end
|
end
|
||||||
EOV
|
EOV
|
||||||
generated_timeliness_methods.module_eval(method_body, __FILE__, line)
|
generated_timeliness_methods.module_eval(method_body, __FILE__, line)
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
def timeliness_attribute_timezone_aware?(attr_name)
|
def timeliness_attribute_timezone_aware?(attr_name)
|
||||||
attr_name = attr_name.to_s
|
attr_name = attr_name.to_s
|
||||||
@ -51,15 +60,13 @@ module ValidatesTimeliness
|
|||||||
def timeliness_type_cast_code(attr_name, var_name)
|
def timeliness_type_cast_code(attr_name, var_name)
|
||||||
type = timeliness_attribute_type(attr_name)
|
type = timeliness_attribute_type(attr_name)
|
||||||
|
|
||||||
<<-END
|
method_body = super
|
||||||
#{super}
|
method_body << "\n#{var_name} = #{var_name}.to_date if #{var_name}" if type == :date
|
||||||
#{var_name} = #{var_name}.to_date if #{var_name} && :#{type} == :date
|
method_body
|
||||||
END
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# ActiveRecord >= 3.1.x needs no cached cleared
|
module Reload
|
||||||
if use_plugin_cache?
|
|
||||||
def reload(*args)
|
def reload(*args)
|
||||||
_clear_timeliness_cache
|
_clear_timeliness_cache
|
||||||
super
|
super
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user