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