Validator 4.x compat

This commit is contained in:
Adam Meehan 2015-12-29 14:57:56 +11:00
parent 24f2752c2e
commit feb508f171
2 changed files with 11 additions and 6 deletions

View File

@ -21,9 +21,7 @@ module ActiveModel
def timeliness_validation_for(attr_names, type=nil)
options = _merge_attributes(attr_names)
options.update(:type => type) if type
# Rails 4.0 and 4.1 compatibility for old #setup method with class as arg
options.update(:class => self) unless options.has_key?(:class)
validates_with TimelinessValidator, _merge_attributes(attr_names)
validates_with TimelinessValidator, options
end
end

View File

@ -45,14 +45,21 @@ module ValidatesTimeliness
super
model = options[:class]
setup_timeliness_validated_attributes(options[:class]) if options[:class]
end
def setup_timeliness_validated_attributes(model)
if model.respond_to?(:timeliness_validated_attributes)
model.timeliness_validated_attributes ||= []
model.timeliness_validated_attributes |= attributes
end
end
# Rails 4.0 compatibility for old #setup method with class as arg
if ActiveModel.version <= Gem::Version.new('4.1')
alias_method(:setup, :setup_timeliness_validated_attributes)
end
def validate_each(record, attr_name, value)
raw_value = attribute_raw_value(record, attr_name) || value
return if (@allow_nil && raw_value.nil?) || (@allow_blank && raw_value.blank?)
@ -94,8 +101,8 @@ module ValidatesTimeliness
end
def attribute_raw_value(record, attr_name)
record.respond_to?(:_timeliness_raw_value_for) &&
record._timeliness_raw_value_for(attr_name.to_s)
record.respond_to?(:read_timeliness_attribute_before_type_cast) &&
record.read_timeliness_attribute_before_type_cast(attr_name.to_s)
end
def timezone_aware?(record, attr_name)