Tighten up mongoid shim for reload override in 1.8.x and 1.9.x

This commit is contained in:
Adam Meehan 2012-02-06 22:48:29 +11:00
parent 8c1ba048df
commit ff594d5873

View File

@ -7,6 +7,8 @@ module ValidatesTimeliness
# field value in Mongoid. Parser will return nil rather than error.
module ClassMethods
public
# Mongoid has no bulk attribute method definition hook. It defines
# them with each field definition. So we likewise define them after
# each validation is defined.
@ -16,12 +18,6 @@ module ValidatesTimeliness
attr_names.each { |attr_name| define_timeliness_write_method(attr_name) }
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)
{
Date => :date,
@ -29,11 +25,22 @@ module ValidatesTimeliness
DateTime => :datetime
}[fields[attr_name.to_s].type] || :datetime
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
def reload
_clear_timeliness_cache
super
module Reload
def reload(*args)
_clear_timeliness_cache
super
end
end
end
end
@ -44,11 +51,13 @@ module Mongoid::Document
include ValidatesTimeliness::ORM::Mongoid
# Pre-2.3 reload
if instance_methods.include?('reload')
if (instance_methods & ['reload', :reload]).present?
def reload_with_timeliness
_clear_timeliness_cache
reload_without_timeliness
end
alias_method_chain :reload, :timeliness
else
include ValidatesTimeliness::ORM::Mongoid::Reload
end
end