Class: ActiveModel::Errors

Inherits:
Object
  • Object
show all
Defined in:
lib/releaf/rails_ext/validation_error_codes.rb

Instance Method Summary collapse

Instance Method Details

#add(attribute, message = nil, options = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/releaf/rails_ext/validation_error_codes.rb', line 13

def add(attribute, message = nil, options = {})
  # build error code from message symbol
  error_code = normalize_error_code(attribute, message, options)
  message = normalize_message(attribute, message, options)
  if exception = options[:strict]
    exception = ActiveModel::StrictValidationFailed if exception == true
    raise exception, full_message(attribute, message)
  end

  # use customized String subclass with "error_code" attribute
  self[attribute] << ErrorMessage.new(message, error_code, options[:data])
end

#normalize_error_code(_attribute, message, options) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/releaf/rails_ext/validation_error_codes.rb', line 26

def normalize_error_code(_attribute, message, options)
  if !options[:error_code].blank?
    options[:error_code]
  elsif message.class == Symbol
    message
  else
    :invalid
  end
end