Class: Caprese::Record::Errors

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

Instance Method Summary collapse

Instance Method Details

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

Adds an error object for a field, with a code, to the messages hash

Parameters:

  • attribute (Symbol)

    the attribute of the model that this error applies to

  • code (Symbol) (defaults to: :invalid)

    the error code for the attribute

  • [Exception,Boolean] (Hash)

    a customizable set of options

  • [Hash] (Hash)

    a customizable set of options



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/caprese/record/errors.rb', line 14

def add(attribute, code = :invalid, options = {})
  options = options.dup
  options[:t] ||= {}
  options[:t][:count] = options[:count]
  options[:t][:value] ||= options[:value] ||
    if attribute != :base && @base.respond_to?(attribute)
      @base.send(:read_attribute_for_validation, attribute)
    else
      nil
    end

  e = Error.new(
    model: @base.class.name.underscore.downcase,
    field: attribute == :base ? nil : attribute,
    code: code,
    t: options[:t]
  )

  if (exception = options[:strict])
    exception = ActiveModel::StrictValidationFailed if exception == true
    raise exception, e.full_message
  end

  self[attribute] << e
end

#as_jsonObject



73
74
75
76
77
78
79
# File 'lib/caprese/record/errors.rb', line 73

def as_json
  Hash[
    map do |field, errors|
      [field, Array.wrap(errors).map { |e| e.as_json }]
    end
  ]
end

#empty?Boolean

Returns true if the model has no errors.

Returns:

  • (Boolean)

    true if the model has no errors



41
42
43
# File 'lib/caprese/record/errors.rb', line 41

def empty?
  all? { |k,v| !v }
end

#full_messagesHash

Note:

Overriden because original renders full_messages using internal helpers of self instead of error

Returns the full error messages for each error in the model

Returns:

  • (Hash)

    a hash mapped by attribute, with each value being an array of full messages for that attribute of the model



48
49
50
# File 'lib/caprese/record/errors.rb', line 48

def full_messages
  map { |_, e| e.full_message }
end

#full_messages_for(attribute) ⇒ Array

Note:

Overriden because original renders full_messages using internal helpers of self instead of error

Returns an array of full error messages for a given attribute of the model.

Parameters:

  • attribute (Symbol)

    an attribute in the model

Returns:

  • (Array)

    an array of full error messages for a given attribute of the model



55
56
57
# File 'lib/caprese/record/errors.rb', line 55

def full_messages_for(attribute)
  (get(attribute) || []).map { |e| e.full_message }
end

#to_aArray

Note:

Overridden because traditionally to_a is an alias for ‘full_messages`, because in Rails standard there is no difference between an error and a full message, an error is that full message. With our API, an error is a model that can render full messages, but it is still a distinct model. `to_a` thus has a different meaning here than in Rails standard.

Returns an array of all errors in the model.

Returns:

  • (Array)

    an array of all errors in the model



64
65
66
# File 'lib/caprese/record/errors.rb', line 64

def to_a
  values.flatten
end

#to_xml(options = {}) ⇒ Object

We have not and will not ever implement an XML rendering of these errors

Raises:

  • (NotImplementedError)


69
70
71
# File 'lib/caprese/record/errors.rb', line 69

def to_xml(options = {})
  raise NotImplementedError
end