mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-23 06:16:44 +00:00
Validator 4.x compat
This commit is contained in:
parent
24f2752c2e
commit
feb508f171
@ -21,9 +21,7 @@ module ActiveModel
|
|||||||
def timeliness_validation_for(attr_names, type=nil)
|
def timeliness_validation_for(attr_names, type=nil)
|
||||||
options = _merge_attributes(attr_names)
|
options = _merge_attributes(attr_names)
|
||||||
options.update(:type => type) if type
|
options.update(:type => type) if type
|
||||||
# Rails 4.0 and 4.1 compatibility for old #setup method with class as arg
|
validates_with TimelinessValidator, options
|
||||||
options.update(:class => self) unless options.has_key?(:class)
|
|
||||||
validates_with TimelinessValidator, _merge_attributes(attr_names)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -45,14 +45,21 @@ module ValidatesTimeliness
|
|||||||
|
|
||||||
super
|
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)
|
if model.respond_to?(:timeliness_validated_attributes)
|
||||||
model.timeliness_validated_attributes ||= []
|
model.timeliness_validated_attributes ||= []
|
||||||
model.timeliness_validated_attributes |= attributes
|
model.timeliness_validated_attributes |= attributes
|
||||||
end
|
end
|
||||||
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)
|
def validate_each(record, attr_name, value)
|
||||||
raw_value = attribute_raw_value(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?)
|
return if (@allow_nil && raw_value.nil?) || (@allow_blank && raw_value.blank?)
|
||||||
@ -94,8 +101,8 @@ module ValidatesTimeliness
|
|||||||
end
|
end
|
||||||
|
|
||||||
def attribute_raw_value(record, attr_name)
|
def attribute_raw_value(record, attr_name)
|
||||||
record.respond_to?(:_timeliness_raw_value_for) &&
|
record.respond_to?(:read_timeliness_attribute_before_type_cast) &&
|
||||||
record._timeliness_raw_value_for(attr_name.to_s)
|
record.read_timeliness_attribute_before_type_cast(attr_name.to_s)
|
||||||
end
|
end
|
||||||
|
|
||||||
def timezone_aware?(record, attr_name)
|
def timezone_aware?(record, attr_name)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user