Class: ActiveInteraction::Errors

Inherits:
ActiveModel::Errors
  • Object
show all
Defined in:
lib/active_interaction/errors.rb,
lib/active_interaction/backports.rb

Overview

An extension that provides symbolic error messages to make introspection

and testing easier.

Since:

  • 1.0.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeErrors

Returns a new instance of Errors.

See Also:

  • ActiveModel::Errors#initialize

Since:

  • 1.0.0



126
127
128
129
130
# File 'lib/active_interaction/errors.rb', line 126

def initialize(*)
  @symbolic = Hash.new([]).with_indifferent_access

  super
end

Instance Attribute Details

#symbolicHash{Symbol => Array<Symbol>} (readonly)

Maps attributes to arrays of symbolic messages.

Returns:

  • (Hash{Symbol => Array<Symbol>})

Since:

  • 1.0.0



98
99
100
# File 'lib/active_interaction/errors.rb', line 98

def symbolic
  @symbolic
end

Instance Method Details

#add_sym(attribute, symbol = :invalid, message = nil, options = {}) ⇒ Hash{Symbol => Array<Symbol>}

Adds a symbolic error message to an attribute.

Examples:

errors.add_sym(:attribute)
errors.symbolic
# => {:attribute=>[:invalid]}
errors.messages
# => {:attribute=>["is invalid"]}

Parameters:

  • attribute (Symbol)

    The attribute to add an error to.

  • symbol (Symbol, nil) (defaults to: :invalid)

    The symbolic error to add.

  • message (String, Symbol, Proc, nil) (defaults to: nil)

    The message to add.

  • options (Hash) (defaults to: {})

Returns:

  • (Hash{Symbol => Array<Symbol>})

See Also:

  • ActiveModel::Errors#add

Since:

  • 1.0.0



117
118
119
120
121
# File 'lib/active_interaction/errors.rb', line 117

def add_sym(attribute, symbol = :invalid, message = nil, options = {})
  add(attribute, message || symbol, options)

  symbolic[attribute] += [symbol]
end

#clearObject

See Also:

  • ActiveModel::Errors#clear

Since:

  • 1.0.0



144
145
146
147
148
# File 'lib/active_interaction/errors.rb', line 144

def clear
  symbolic.clear

  super
end

#merge!(other) ⇒ Errors

Merge other errors into this one.

Parameters:

Returns:

Since:

  • 1.0.0



155
156
157
158
159
# File 'lib/active_interaction/errors.rb', line 155

def merge!(other)
  merge_messages!(other)
  merge_symbolic!(other) if other.respond_to?(:symbolic)
  self
end