This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
2022-09-20 14:51:21 -07:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class LanguageValidator < ActiveModel::EachValidator
|
|
|
|
include LanguagesHelper
|
|
|
|
|
|
|
|
def validate_each(record, attribute, value)
|
2023-07-28 14:12:09 -07:00
|
|
|
@value = value
|
|
|
|
|
|
|
|
record.errors.add(attribute, :invalid) unless valid_locale_value?
|
2022-09-20 14:51:21 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2023-07-28 14:12:09 -07:00
|
|
|
def valid_locale_value?
|
|
|
|
if @value.nil?
|
2022-09-20 14:51:21 -07:00
|
|
|
true
|
2023-07-28 14:12:09 -07:00
|
|
|
elsif @value.is_a?(Array)
|
|
|
|
@value.all? { |x| valid_locale?(x) }
|
2022-09-20 14:51:21 -07:00
|
|
|
else
|
2023-07-28 14:12:09 -07:00
|
|
|
valid_locale?(@value)
|
2022-09-20 14:51:21 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|