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