Class: Caprese::Record::Errors
- Inherits:
-
ActiveModel::Errors
- Object
- ActiveModel::Errors
- Caprese::Record::Errors
- Defined in:
- lib/caprese/record/errors.rb
Instance Method Summary collapse
-
#add(attribute, code = :invalid, options = {}) ⇒ Object
Adds an error object for a field, with a code, to the messages hash.
- #as_json ⇒ Object
-
#empty? ⇒ Boolean
True if the model has no errors.
-
#full_messages ⇒ Hash
Returns the full error messages for each error in the model.
-
#full_messages_for(attribute) ⇒ Array
An array of full error messages for a given attribute of the model.
-
#to_a ⇒ Array
An array of all errors in the model.
-
#to_xml(options = {}) ⇒ Object
We have not and will not ever implement an XML rendering of these errors.
Instance Method Details
#add(attribute, code = :invalid, options = {}) ⇒ Object
Adds an error object for a field, with a code, to the messages hash
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, = {}) = .dup [:t] ||= {} [:t][:count] = [:count] [:t][:value] ||= [: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: [:t] ) if (exception = [:strict]) exception = ActiveModel::StrictValidationFailed if exception == true raise exception, e. end self[attribute] << e end |
#as_json ⇒ Object
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.
41 42 43 |
# File 'lib/caprese/record/errors.rb', line 41 def empty? all? { |k,v| !v } end |
#full_messages ⇒ Hash
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
48 49 50 |
# File 'lib/caprese/record/errors.rb', line 48 def map { |_, e| e. } end |
#full_messages_for(attribute) ⇒ Array
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.
55 56 57 |
# File 'lib/caprese/record/errors.rb', line 55 def (attribute) (get(attribute) || []).map { |e| e. } end |
#to_a ⇒ Array
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.
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
69 70 71 |
# File 'lib/caprese/record/errors.rb', line 69 def to_xml( = {}) raise NotImplementedError end |