Class: StructuredParams::Errors

Inherits:
ActiveModel::Errors
  • Object
show all
Defined in:
lib/structured_params/errors.rb

Overview

Custom errors collection that handles nested attribute names

Instance Method Summary collapse

Instance Method Details

#as_json(options = nil) ⇒ Object

Override as_json to support structured option This maintains compatibility with ActiveModel::Errors while adding structured functionality : (?{ full_messages?: bool, structured?: bool }?) -> Hash[Symbol, untyped]



25
26
27
28
29
# File 'lib/structured_params/errors.rb', line 25

def as_json(options = nil)
  options ||= {}
  to_hash(options.fetch(:full_messages, false),
          structured: options.fetch(:structured, false))
end

#messages(structured: false) ⇒ Object

Override messages to support structured option This maintains compatibility with ActiveModel::Errors while adding structured functionality : (?structured: bool) -> Hash[Symbol, untyped]



34
35
36
37
38
39
# File 'lib/structured_params/errors.rb', line 34

def messages(structured: false)
  hash = to_hash(false, structured: structured)
  hash.default = [].freeze
  hash.freeze
  hash
end

#to_hash(full_messages = false, structured: false) ⇒ Object

Override to_hash to maintain compatibility with ActiveModel::Errors by default Add structured option to get nested structure for dot-notation attributes : (?bool, ?structured: false) -> Hash[Symbol, String] : (?bool, structured: bool) -> Hash[Symbol, untyped]



12
13
14
15
16
17
18
19
20
# File 'lib/structured_params/errors.rb', line 12

def to_hash(full_messages = false, structured: false)
  if structured
    attribute_messages_hash = build_attribute_messages_hash(full_messages)
    build_nested_hash({}, attribute_messages_hash)
  else
    # Use default ActiveModel::Errors behavior
    super(full_messages)
  end
end