language_validator.rb 449 B

1234567891011121314151617181920212223
  1. # frozen_string_literal: true
  2. class LanguageValidator < ActiveModel::EachValidator
  3. include LanguagesHelper
  4. def validate_each(record, attribute, value)
  5. @value = value
  6. record.errors.add(attribute, :invalid) unless valid_locale_value?
  7. end
  8. private
  9. def valid_locale_value?
  10. if @value.nil?
  11. true
  12. elsif @value.is_a?(Array)
  13. @value.all? { |x| valid_locale?(x) }
  14. else
  15. valid_locale?(@value)
  16. end
  17. end
  18. end