Sha256: 3946c56727f0fb0dd8298ee1b5526ef559cf5aefd631ce7755e4868bb265dc5d
Contents?: true
Size: 1.82 KB
Versions: 3
Compression:
Stored size: 1.82 KB
Contents
module PrettyApi module Errors class NestedErrors def self.parsed_nested_errors(record, attrs) errors = record_only_errors(record) return errors if attrs.blank? parse_deep_nested_errors(record, attrs, errors) PrettyApi::Utils::Hash.deep_compact_blank(errors) end def self.parse_deep_nested_errors(record, attrs, result) case attrs when Hash attrs.each do |key, value| parse_association_errors(record, key, value, result) end when Array attrs.each { |value| parse_deep_nested_errors record, value, result } else parse_association_errors(record, attrs, nil, result) end end def self.parse_association_errors(record, attr, nested_attrs = nil, result) associations = record.send(attr) return if associations.blank? if associations.respond_to? :to_a parse_has_many_errors(associations, attr, nested_attrs, result) else parse_has_one_errors(associations, attr, nested_attrs, result) end end def self.parse_has_many_errors(associations, attr, nested_attrs = nil, result) result[attr] = {} associations.each_with_index do |association, i| result[attr][i] = record_only_errors(association) parse_deep_nested_errors association, nested_attrs, result[attr][i] if nested_attrs.present? end end def self.parse_has_one_errors(association, attr, nested_attrs = nil, result) result[attr] = record_only_errors(association) parse_deep_nested_errors association, nested_attrs, result[attr] if nested_attrs.present? end def self.record_only_errors(record) record.errors.as_json.reject { |k, _v| k.to_s.include?(".") } end end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
pretty-api-0.1.2 | lib/pretty_api/errors/nested_errors.rb |
pretty-api-0.1.1 | lib/pretty_api/errors/nested_errors.rb |
pretty-api-0.1.0 | lib/pretty_api/errors/nested_errors.rb |