lib/validates_timeliness/validator.rb in adzap-validates_timeliness-2.1.0 vs lib/validates_timeliness/validator.rb in adzap-validates_timeliness-2.2.0
- old
+ new
@@ -30,26 +30,18 @@
def call(record, attr_name, value)
raw_value = raw_value(record, attr_name) || value
if value.is_a?(String) || configuration[:format]
- strict = !configuration[:format].nil?
- value = ValidatesTimeliness::Parser.parse(raw_value, type, :strict => strict, :format => configuration[:format])
+ value = ValidatesTimeliness::Parser.parse(raw_value, type, :strict => false, :format => configuration[:format])
end
return if (raw_value.nil? && configuration[:allow_nil]) || (raw_value.blank? && configuration[:allow_blank])
- if raw_value.blank?
- add_error(record, attr_name, :blank)
- return
- end
+ return add_error(record, attr_name, :blank) if raw_value.blank?
+ return add_error(record, attr_name, "invalid_#{type}".to_sym) if value.nil?
- if value.nil?
- add_error(record, attr_name, "invalid_#{type}".to_sym)
- return
- end
-
validate_restrictions(record, attr_name, value)
end
def error_messages
@error_messages ||= self.class.default_error_messages.merge(custom_error_messages)
@@ -60,25 +52,24 @@
def raw_value(record, attr_name)
record.send("#{attr_name}_before_type_cast") rescue nil
end
def validate_restrictions(record, attr_name, value)
- value = if configuration[:with_time] || configuration[:with_date]
- restriction_type = :datetime
- combine_date_and_time(value, record)
- else
- restriction_type = type
- self.class.type_cast_value(value, type, configuration[:ignore_usec])
+ if configuration[:with_time] || configuration[:with_date]
+ value = combine_date_and_time(value, record)
end
+
+ value = self.class.type_cast_value(value, implied_type, configuration[:ignore_usec])
+
return if value.nil?
RESTRICTION_METHODS.each do |option, method|
next unless restriction = configuration[option]
begin
- restriction = self.class.evaluate_option_value(restriction, restriction_type, record)
+ restriction = self.class.evaluate_option_value(restriction, implied_type, record)
next if restriction.nil?
- restriction = self.class.type_cast_value(restriction, restriction_type, configuration[:ignore_usec])
+ restriction = self.class.type_cast_value(restriction, implied_type, configuration[:ignore_usec])
unless evaluate_restriction(restriction, value, method)
add_error(record, attr_name, option, interpolation_values(option, restriction))
end
rescue
@@ -153,10 +144,14 @@
invalid_for_type << :with_date unless type == :time
invalid_for_type << :with_time unless type == :date
options.assert_valid_keys(VALID_OPTIONS - invalid_for_type)
end
+ def implied_type
+ @implied_type ||= configuration[:with_date] || configuration[:with_time] ? :datetime : type
+ end
+
# class methods
class << self
def default_error_messages
if defined?(I18n)
@@ -183,10 +178,11 @@
when Time, Date
value
when Symbol
evaluate_option_value(record.send(value), type, record)
when Proc
- evaluate_option_value(value.call(record), type, record)
+ result = value.arity > 0 ? value.call(record) : value.call
+ evaluate_option_value(result, type, record)
when Array
value.map {|r| evaluate_option_value(r, type, record) }.sort
when Range
evaluate_option_value([value.first, value.last], type, record)
else