Class: SimpleParams::Errors

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

Instance Method Summary collapse

Constructor Details

#initialize(base, nested_attributes = []) ⇒ Errors

Returns a new instance of Errors.



6
7
8
9
# File 'lib/simple_params/errors.rb', line 6

def initialize(base, nested_attributes = [])
  super(base)
  @nested_attributes = symbolize_nested(nested_attributes)
end

Instance Method Details

#[](attribute) ⇒ Object



11
12
13
# File 'lib/simple_params/errors.rb', line 11

def [](attribute)
  get(attribute.to_sym) || reset_attribute(attribute.to_sym)
end

#[]=(attribute, error) ⇒ Object



15
16
17
# File 'lib/simple_params/errors.rb', line 15

def []=(attribute, error)
  add_error_to_attribute(attribute, error)
end

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



19
20
21
22
23
24
25
26
27
# File 'lib/simple_params/errors.rb', line 19

def add(attribute, message = :invalid, options = {})
  message = normalize_message(attribute, message, options)
  if exception = options[:strict]
    exception = ActiveModel::StrictValidationFailed if exception == true
    raise exception, full_message(attribute, message)
  end

  add_error_to_attribute(attribute, message)
end

#clearObject



29
30
31
32
33
34
35
36
# File 'lib/simple_params/errors.rb', line 29

def clear
  super
  @nested_attributes.each do |attribute|
    if fetch_nested_attribute(attribute).present?
      fetch_nested_attribute(attribute).errors.clear
    end
  end
end

#empty?Boolean Also known as: blank?

Returns:

  • (Boolean)


38
39
40
41
42
43
44
45
# File 'lib/simple_params/errors.rb', line 38

def empty?
  super &&
  @nested_attributes.all? do |attribute|
    if fetch_nested_attribute(attribute).present?
      fetch_nested_attribute(attribute).errors.empty?
    end
  end
end

#full_messagesObject



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/simple_params/errors.rb', line 67

def full_messages
  parent_messages = map { |attribute, message| full_message(attribute, message) }
  nested_messages = @nested_attributes.map do |attribute|
    if fetch_nested_attribute(attribute)
      messages = fetch_nested_attribute(attribute).errors.full_messages
      messages.map do |message|
        "#{attribute} " + message
      end
    end
  end
  (parent_messages + nested_messages).flatten
end

#include?(attribute) ⇒ Boolean Also known as: has_key?, key?

Returns:

  • (Boolean)


48
49
50
51
52
53
54
# File 'lib/simple_params/errors.rb', line 48

def include?(attribute)
  if fetch_nested_attribute(attribute)
    !fetch_nested_attribute(attribute).errors.empty?
  else
    messages[attribute].present?
  end
end

#to_hash(full_messages = false) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/simple_params/errors.rb', line 80

def to_hash(full_messages = false)
  messages = if full_messages
    msgs = {}
    self.messages.each do |attribute, array|
      msgs[attribute] = array.map { |message| full_message(attribute, message) }
    end
    msgs
  else
    self.messages.dup
  end

  @nested_attributes.map do |attribute|
    errors = nested_error_messages(attribute, full_messages)
    unless errors.empty?
      messages.merge!(attribute.to_sym => errors)
    end
  end
  messages
end

#to_s(full_messages = false) ⇒ Object



100
101
102
103
# File 'lib/simple_params/errors.rb', line 100

def to_s(full_messages = false)
  array = to_a
  array.join(', ')
end

#valuesObject



58
59
60
61
62
63
64
65
# File 'lib/simple_params/errors.rb', line 58

def values
  messages.values +
  @nested_attributes.map do |attribute|
    if fetch_nested_attribute(attribute).present?
      fetch_nested_attribute(attribute).errors.values
    end
  end
end