Add coverage for LanguageValidator
(#25593)
This commit is contained in:
parent
ad81be6c8e
commit
6602edf064
2 changed files with 68 additions and 6 deletions
|
@ -4,18 +4,20 @@ class LanguageValidator < ActiveModel::EachValidator
|
|||
include LanguagesHelper
|
||||
|
||||
def validate_each(record, attribute, value)
|
||||
record.errors.add(attribute, :invalid) unless valid?(value)
|
||||
@value = value
|
||||
|
||||
record.errors.add(attribute, :invalid) unless valid_locale_value?
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def valid?(str)
|
||||
if str.nil?
|
||||
def valid_locale_value?
|
||||
if @value.nil?
|
||||
true
|
||||
elsif str.is_a?(Array)
|
||||
str.all? { |x| valid_locale?(x) }
|
||||
elsif @value.is_a?(Array)
|
||||
@value.all? { |x| valid_locale?(x) }
|
||||
else
|
||||
valid_locale?(str)
|
||||
valid_locale?(@value)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue