Class: SimpleParams::Errors
- Inherits:
-
ActiveModel::Errors
- Object
- ActiveModel::Errors
- SimpleParams::Errors
- Defined in:
- lib/simple_params/errors.rb
Instance Method Summary collapse
- #[](attribute) ⇒ Object
- #[]=(attribute, error) ⇒ Object
- #add(attribute, message = :invalid, options = {}) ⇒ Object
- #clear ⇒ Object
- #empty? ⇒ Boolean (also: #blank?)
- #full_messages ⇒ Object
- #include?(attribute) ⇒ Boolean (also: #has_key?, #key?)
-
#initialize(base, nested_attributes = []) ⇒ Errors
constructor
A new instance of Errors.
- #to_hash(full_messages = false) ⇒ Object
- #to_s(full_messages = false) ⇒ Object
- #values ⇒ Object
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, = :invalid, = {}) = (attribute, , ) if exception = [:strict] exception = ActiveModel::StrictValidationFailed if exception == true raise exception, (attribute, ) end add_error_to_attribute(attribute, ) end |
#clear ⇒ Object
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?
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_messages ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/simple_params/errors.rb', line 67 def = map { |attribute, | (attribute, ) } = @nested_attributes.map do |attribute| if fetch_nested_attribute(attribute) = fetch_nested_attribute(attribute).errors. .map do || "#{attribute} " + end end end ( + ).flatten end |
#include?(attribute) ⇒ Boolean Also known as: has_key?, key?
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 [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( = false) = if msgs = {} self..each do |attribute, array| msgs[attribute] = array.map { || (attribute, ) } end msgs else self..dup end @nested_attributes.map do |attribute| errors = (attribute, ) unless errors.empty? .merge!(attribute.to_sym => errors) end end end |
#to_s(full_messages = false) ⇒ Object
100 101 102 103 |
# File 'lib/simple_params/errors.rb', line 100 def to_s( = false) array = to_a array.join(', ') end |
#values ⇒ Object
58 59 60 61 62 63 64 65 |
# File 'lib/simple_params/errors.rb', line 58 def values .values + @nested_attributes.map do |attribute| if fetch_nested_attribute(attribute).present? fetch_nested_attribute(attribute).errors.values end end end |