Class: AgeValidator
- Inherits:
-
ActiveModel::EachValidator
- Object
- ActiveModel::EachValidator
- AgeValidator
- Defined in:
- lib/age_calculator/validator.rb
Constant Summary collapse
- COMPARATORS =
{ :over => :>=, :under => :<= }.freeze
Class Method Summary collapse
Instance Method Summary collapse
Class Method Details
.locales ⇒ Object
19 20 21 |
# File 'lib/age_calculator/validator.rb', line 19 def self.locales Dir[Pathname.new(__FILE__).join('../../../config/locales/*.yml')] end |
Instance Method Details
#validate_each(record, attribute, value) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/age_calculator/validator.rb', line 6 def validate_each(record, attribute, value) return record.errors.add(attribute, :blank) if value.blank? return record.errors.add(attribute, :not_date) unless value.is_a?(Date) age = AgeCalculator.new(value).age(asof: [:asof]) .slice(*COMPARATORS.keys).each do |option_key, option_value| unless age.send(COMPARATORS[option_key], option_value.to_i) record.errors.add(attribute, I18n.t("errors.messages.age_#{option_key}", age: option_value)) end end end |