mirror of
https://git.kescher.at/CatCatNya/catstodon.git
synced 2024-11-27 15:01:36 +01:00
22 lines
403 B
Ruby
22 lines
403 B
Ruby
|
# 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
|