Class: DateValidator
- Inherits:
-
ActiveModel::EachValidator
- Object
- ActiveModel::EachValidator
- DateValidator
- Defined in:
- lib/validators/date_validator.rb
Instance Method Summary collapse
- #date_for(record, value, option) ⇒ Object
- #validate_after_option(record, attribute, value, original_value) ⇒ Object
- #validate_before_option(record, attribute, value, original_value) ⇒ Object
- #validate_each(record, attribute, value) ⇒ Object
Instance Method Details
#date_for(record, value, option) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/validators/date_validator.rb', line 70 def date_for(record, value, option) date = case option when :today Date.today when :now Time.now + 60 # be lenient on now for server clocks when Time, Date, DateTime, ActiveSupport::TimeWithZone option when Proc option.call(record) else record.__send__(option) if record.respond_to?(option) end return unless date.present? if [:time] return [date.to_time, value.to_time] else return [date.to_date, value.to_date] end end |
#validate_after_option(record, attribute, value, original_value) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/validators/date_validator.rb', line 33 def validate_after_option(record, attribute, value, original_value) date, value = date_for(record, value, [:after]) return true unless date.present? return true if value.present? && date.present? && (value && date && value >= date) = ": #{original_value} is not a valid value." if [:after] == :now || [:after] == :today << " Date cannot be in the past" elsif [:after].respond_to?(:strftime) << " Date cannot be before #{[:after]}" elsif [:after].is_a? Proc << " Date cannot be before #{date}" elsif record.respond_to?([:after]) << " Date cannot be before #{[:after]}" end end |
#validate_before_option(record, attribute, value, original_value) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/validators/date_validator.rb', line 51 def validate_before_option(record, attribute, value, original_value) date, value = date_for(record, value, [:before]) return true unless date.present? return true if value.present? && date.present? && (value && date && value <= date) = ": #{original_value} is not a valid value." if [:before] == :now || [:before] == :today << " Date cannot be in the future" elsif [:before].respond_to?(:strftime) << " Date cannot be after #{[:before]}" elsif [:before].is_a? Proc << " Date cannot be after #{date}" elsif record.respond_to?([:before]) << " Date cannot be after #{[:before]}" end end |
#validate_each(record, attribute, value) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/validators/date_validator.rb', line 7 def validate_each(record, attribute, value) @error_hash = [:error_level].present? ? record.send([:error_level]) : record.errors original_value = value original_value = record.read_attribute_before_type_cast( attribute ) rescue nil # only in rails # dont display date format error unless date could not be parsed if value.nil? # if blank date was given it's still not a format issue, still show message if desired return if original_value.blank? && [:allow_blank] # display helpful date format validation message with original value = [:time] ? ": #{original_value} is not a valid value. Value must be a date in YYYY-MM-DD or YYYY-MM-DD HH:MM:SS format." : ": #{original_value} is not a valid value. Value must be a date in YYYY-MM-DD format." @error_hash.add(attribute, ([:message] || )) else # handle validation options on valid date instances return unless value.respond_to?(:strftime) if([:after] && ( = validate_after_option(record, attribute, value, original_value)) != true) @error_hash.add(attribute, ([:after_message] || "#{}.")) end if([:before] && ( = validate_before_option(record, attribute, value, original_value)) != true) @error_hash.add(attribute, ([:before_message] || "#{}.")) end end end |