Class: ActiveModel::Validations::RolesValidator

Inherits:
EachValidator
  • Object
show all
Defined in:
lib/active_model/validations/roles_validator.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ RolesValidator

Returns a new instance of RolesValidator.



10
11
12
13
# File 'lib/active_model/validations/roles_validator.rb', line 10

def initialize(options)
  @roles = options[:roles]
  super 
end

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



14
15
16
17
18
# File 'lib/active_model/validations/roles_validator.rb', line 14

def validate_each(record, attribute, value) 
  unless value.split("_").all? {|x| @roles.include?(x) }
    record.errors[attribute] << (options[:message] || "contains an invalid role")
  end
end