Class: Caprese::Serializer::ErrorSerializer
- Inherits:
-
ActiveModel::Serializer::ErrorSerializer
- Object
- ActiveModel::Serializer::ErrorSerializer
- Caprese::Serializer::ErrorSerializer
- Defined in:
- lib/caprese/serializer/error_serializer.rb
Instance Method Summary collapse
-
#as_json ⇒ Object
Applies aliases to fields of RecordInvalid record’s errors if aliases have been applied Otherwise returns normal error fields as_json hash.
- #document_errors? ⇒ Boolean
- #resource_errors? ⇒ Boolean
Instance Method Details
#as_json ⇒ Object
Applies aliases to fields of RecordInvalid record’s errors if aliases have been applied Otherwise returns normal error fields as_json hash
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/caprese/serializer/error_serializer.rb', line 17 def as_json json = object.as_json record = object.try(:record) aliases = object.try(:aliases) if record.present? && aliases.try(:any?) aliased_json = {} json.each do |k, v| # Iterate over engaged_field_aliases object and see if each segment of the name split by '.' is in it (meaning # that segment should be aliased for the error output since that is what the user is expecting) klass_iterator = record.class alias_iterator = aliases field_iteratee = k.to_s.split('.') new_error_field_name = field_iteratee.map do |field| field_alias = klass_iterator.caprese_alias_field(field) if i = alias_iterator.try(:[], field_alias) alias_iterator = i # If != true, will be an object (relationship) to traverse, find the relationship klass so we can use its aliases # for the next segment of alias_iterator if alias_iterator != true && (ref = klass_iterator.reflect_on_association(field)).present? klass_iterator = ref.klass end field_alias elsif i = alias_iterator.try(:[], field) alias_iterator = i # If != true, will be an object (relationship) to traverse, find the relationship klass so we can use its aliases # for the next segment of alias_iterator if alias_iterator != true && (ref = klass_iterator.reflect_on_association(field)).present? klass_iterator = ref.klass end field else alias_iterator = {} field end end aliased_json[new_error_field_name.join('.')] = v end aliased_json else json end end |
#document_errors? ⇒ Boolean
10 11 12 |
# File 'lib/caprese/serializer/error_serializer.rb', line 10 def document_errors? object.try(:document).present? end |
#resource_errors? ⇒ Boolean
6 7 8 |
# File 'lib/caprese/serializer/error_serializer.rb', line 6 def resource_errors? object.try(:record).present? end |