2022-09-20 17:51:21 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class LanguageValidator < ActiveModel::EachValidator
|
|
|
|
include LanguagesHelper
|
|
|
|
|
|
|
|
def validate_each(record, attribute, value)
|
2023-07-28 17:12:09 -04:00
|
|
|
@value = value
|
|
|
|
|
|
|
|
record.errors.add(attribute, :invalid) unless valid_locale_value?
|
2022-09-20 17:51:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2023-07-28 17:12:09 -04:00
|
|
|
def valid_locale_value?
|
|
|
|
if @value.nil?
|
2022-09-20 17:51:21 -04:00
|
|
|
true
|
2023-07-28 17:12:09 -04:00
|
|
|
elsif @value.is_a?(Array)
|
|
|
|
@value.all? { |x| valid_locale?(x) }
|
2022-09-20 17:51:21 -04:00
|
|
|
else
|
2023-07-28 17:12:09 -04:00
|
|
|
valid_locale?(@value)
|
2022-09-20 17:51:21 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|