Class: CharacterValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/thingtank/validators.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, values) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/thingtank/validators.rb', line 61

def validate_each(record, attribute, values)
  case values
  when NilClass
    return nil
  when Array
    values.each do |value|
      validate_single_character(record, attribute, value, options)
    end
  else
    validate_single_character(record, attribute, values, options)
  end
end

#validate_single_character(record, attribute, values, options) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/thingtank/validators.rb', line 41

def validate_single_character(record, attribute, values, options)
  if options[:in] # validates :hosting_account, :character => [HostingAccount, ...]
    valid = false
    options[:in].each do |character|
      character = character.new(values)
      if character.valid?
        valid = true
      end
    end
    unless valid
      record.errors.add attribute, "invalid character is #{values.inspect}, should be one of #{options[:in].join(', ')}"
    end
  else
    character = options[:with].new(values)
    unless character.valid?
      record.errors.add attribute, character.errors.messages
    end
  end
end