mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-22 22:06:45 +00:00
Tighten up mongoid shim for reload override in 1.8.x and 1.9.x
This commit is contained in:
parent
8c1ba048df
commit
ff594d5873
@ -7,6 +7,8 @@ module ValidatesTimeliness
|
|||||||
# field value in Mongoid. Parser will return nil rather than error.
|
# field value in Mongoid. Parser will return nil rather than error.
|
||||||
|
|
||||||
module ClassMethods
|
module ClassMethods
|
||||||
|
public
|
||||||
|
|
||||||
# Mongoid has no bulk attribute method definition hook. It defines
|
# Mongoid has no bulk attribute method definition hook. It defines
|
||||||
# them with each field definition. So we likewise define them after
|
# them with each field definition. So we likewise define them after
|
||||||
# each validation is defined.
|
# each validation is defined.
|
||||||
@ -16,12 +18,6 @@ module ValidatesTimeliness
|
|||||||
attr_names.each { |attr_name| define_timeliness_write_method(attr_name) }
|
attr_names.each { |attr_name| define_timeliness_write_method(attr_name) }
|
||||||
end
|
end
|
||||||
|
|
||||||
def timeliness_type_cast_code(attr_name, var_name)
|
|
||||||
type = timeliness_attribute_type(attr_name)
|
|
||||||
|
|
||||||
"#{var_name} = Timeliness::Parser.parse(value, :#{type})"
|
|
||||||
end
|
|
||||||
|
|
||||||
def timeliness_attribute_type(attr_name)
|
def timeliness_attribute_type(attr_name)
|
||||||
{
|
{
|
||||||
Date => :date,
|
Date => :date,
|
||||||
@ -29,11 +25,22 @@ module ValidatesTimeliness
|
|||||||
DateTime => :datetime
|
DateTime => :datetime
|
||||||
}[fields[attr_name.to_s].type] || :datetime
|
}[fields[attr_name.to_s].type] || :datetime
|
||||||
end
|
end
|
||||||
|
|
||||||
|
protected
|
||||||
|
|
||||||
|
def timeliness_type_cast_code(attr_name, var_name)
|
||||||
|
type = timeliness_attribute_type(attr_name)
|
||||||
|
|
||||||
|
"#{var_name} = Timeliness::Parser.parse(value, :#{type})"
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def reload
|
module Reload
|
||||||
_clear_timeliness_cache
|
def reload(*args)
|
||||||
super
|
_clear_timeliness_cache
|
||||||
|
super
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -44,11 +51,13 @@ module Mongoid::Document
|
|||||||
include ValidatesTimeliness::ORM::Mongoid
|
include ValidatesTimeliness::ORM::Mongoid
|
||||||
|
|
||||||
# Pre-2.3 reload
|
# Pre-2.3 reload
|
||||||
if instance_methods.include?('reload')
|
if (instance_methods & ['reload', :reload]).present?
|
||||||
def reload_with_timeliness
|
def reload_with_timeliness
|
||||||
_clear_timeliness_cache
|
_clear_timeliness_cache
|
||||||
reload_without_timeliness
|
reload_without_timeliness
|
||||||
end
|
end
|
||||||
alias_method_chain :reload, :timeliness
|
alias_method_chain :reload, :timeliness
|
||||||
|
else
|
||||||
|
include ValidatesTimeliness::ORM::Mongoid::Reload
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user