language_validator.rb 403 B

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