123456789_123456789_123456789_123456789_123456789_

Class: ActiveModel::Error

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Subclasses:
Inherits: Object
Defined in: activemodel/lib/active_model/error.rb

Overview

Represents one single error

Constant Summary

Class Attribute Summary

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(base, attribute, type = :invalid, **options) ⇒ Error

[ GitHub ]

  
# File 'activemodel/lib/active_model/error.rb', line 103

def initialize(base, attribute, type = :invalid, **options)
  @base = base
  @attribute = attribute
  @raw_type = type
  @type = type || :invalid
  @options = options
end

Class Attribute Details

.i18n_customize_full_message (rw)

[ GitHub ]

  
# File 'activemodel/lib/active_model/error.rb', line 13

class_attribute :i18n_customize_full_message, default: false

.i18n_customize_full_message?Boolean (rw)

[ GitHub ]

  
# File 'activemodel/lib/active_model/error.rb', line 13

class_attribute :i18n_customize_full_message, default: false

Instance Attribute Details

#attribute (readonly)

The attribute of #base which the error belongs to

[ GitHub ]

  
# File 'activemodel/lib/active_model/error.rb', line 121

attr_reader :attribute

#base (readonly)

The object which the error belongs to

[ GitHub ]

  
# File 'activemodel/lib/active_model/error.rb', line 119

attr_reader :base

#i18n_customize_full_message (rw)

[ GitHub ]

  
# File 'activemodel/lib/active_model/error.rb', line 13

class_attribute :i18n_customize_full_message, default: false

#i18n_customize_full_message?Boolean (rw)

[ GitHub ]

  
# File 'activemodel/lib/active_model/error.rb', line 13

class_attribute :i18n_customize_full_message, default: false

#options (readonly)

The options provided when calling errors#add

[ GitHub ]

  
# File 'activemodel/lib/active_model/error.rb', line 127

attr_reader :options

#raw_type (readonly)

The raw value provided as the second parameter when calling errors#add

[ GitHub ]

  
# File 'activemodel/lib/active_model/error.rb', line 125

attr_reader :raw_type

#type (readonly)

The type of error, defaults to :invalid unless specified

[ GitHub ]

  
# File 'activemodel/lib/active_model/error.rb', line 123

attr_reader :type

Instance Method Details

#detail

Alias for #details.

[ GitHub ]

  
# File 'activemodel/lib/active_model/error.rb', line 151

alias_method :detail, :details

#details Also known as: #detail

Returns the error details.

error = ActiveModel::Error.new(person, :name, :too_short, count: 5)
error.details
# => { error: :too_short, count: 5 }
[ GitHub ]

  
# File 'activemodel/lib/active_model/error.rb', line 148

def details
  { error: raw_type }.merge(options.except(*CALLBACKS_OPTIONS + MESSAGE_OPTIONS))
end

#eql?(other)

Alias for #==.

[ GitHub ]

  
# File 'activemodel/lib/active_model/error.rb', line 192

alias eql? ==

#full_message

Returns the full error message.

error = ActiveModel::Error.new(person, :name, :too_short, count: 5)
error.full_message
# => "Name is too short (minimum is 5 characters)"
[ GitHub ]

  
# File 'activemodel/lib/active_model/error.rb', line 158

def full_message
  self.class.full_message(attribute, message, @base)
end

#match?(attribute, type = nil, **options) ⇒ Boolean

See if error matches provided #attribute, #type and #options.

Omitted params are not checked for a match.

[ GitHub ]

  
# File 'activemodel/lib/active_model/error.rb', line 165

def match?(attribute, type = nil, **options)
  if @attribute != attribute || (type && @type != type)
    return false
  end

  options.each do |key, value|
    if @options[key] != value
      return false
    end
  end

  true
end

#message

Returns the error message.

error = ActiveModel::Error.new(person, :name, :too_short, count: 5)
error.message
# => "is too short (minimum is 5 characters)"
[ GitHub ]

  
# File 'activemodel/lib/active_model/error.rb', line 134

def message
  case raw_type
  when Symbol
    self.class.generate_message(attribute, raw_type, @base, options.except(*CALLBACKS_OPTIONS))
  else
    raw_type
  end
end

#strict_match?(attribute, type, **options) ⇒ Boolean

See if error matches provided #attribute, #type and #options exactly.

All params must be equal to Error’s own attributes to be considered a strict match.

[ GitHub ]

  
# File 'activemodel/lib/active_model/error.rb', line 183

def strict_match?(attribute, type, **options)
  return false unless match?(attribute, type)

  options == @options.except(*CALLBACKS_OPTIONS + MESSAGE_OPTIONS)
end