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