You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
22 lines
403 B
22 lines
403 B
2 years ago
|
# frozen_string_literal: true
|
||
|
|
||
|
class LanguageValidator < ActiveModel::EachValidator
|
||
|
include LanguagesHelper
|
||
|
|
||
|
def validate_each(record, attribute, value)
|
||
|
record.errors.add(attribute, :invalid) unless valid?(value)
|
||
|
end
|
||
|
|
||
|
private
|
||
|
|
||
|
def valid?(str)
|
||
|
if str.nil?
|
||
|
true
|
||
|
elsif str.is_a?(Array)
|
||
|
str.all? { |x| valid_locale?(x) }
|
||
|
else
|
||
|
valid_locale?(str)
|
||
|
end
|
||
|
end
|
||
|
end
|