Skip to content

Commit

Permalink
Merge branch 'master' into rails_5
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklandgrebe committed Dec 8, 2017
2 parents c022524 + fec413e commit 81cae50
Show file tree
Hide file tree
Showing 20 changed files with 754 additions and 193 deletions.
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,14 @@

## 0.4.1

* Allows `:self` link to be overridden in serializers that subclass Caprese::Serializer
* Allows `:self` link to be overridden in serializers that subclass Caprese::Serializer

# Master

* Add `relationship_scope(relationship_name, scope) => scope` method for all relationships in serializers, allowing override for custom scoping
* Refactor `assign_record_attributes` to `assign_changes_from_document`, which splits into multiple modular methods that handle relationship data errors with more source pointer detail
* Adds `ResourceDocumentInvalidError` for errors pertaining to the document instead of record assignment (`RecordInvalidError`)
* Allows `PATCH` requests to primary endpoints that update autosaving collection relationships to propagate the nested errors on attributes/relationships up to the primary data so error source pointers are just as detailed as they would be under `POST` requests already
* Fields are now assigned in a specific order: attributes, singular relationships, collection relationships
* Fix issue regarding `:base` field titles interpolated into error messages
* Add more detailed error responses to `update_relationship_definition` endpoints
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
Caprese is a Rails library for creating RESTful APIs in as few words as possible. It handles all CRUD operations on resources and their associations for you, and you can customize how these operations
are carried out, allowing for infinite possibilities while focusing on work that matters to you, instead of writing repetitive code for each action of each resource in your application.

For now, the only format that is supported by Caprese is the [JSON API schema](http://jsonapi.org/format/). In the future, Caprese will support a more straightforward (but less powerful) JSON format as well, for simpler use cases.
For now, the only format that is supported by Caprese is the [JSON API schema.](http://jsonapi.org/format/)

[![Coverage Status](https://coveralls.io/repos/github/nicklandgrebe/caprese/badge.svg)](https://coveralls.io/github/nicklandgrebe/caprese)

## Installation

Expand Down
2 changes: 2 additions & 0 deletions lib/caprese/adapter/json_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ def failure_document
hash[:errors] =
if serializer.resource_errors?
Error.resource_errors(serializer, instance_options)
elsif serializer.document_errors?
Error.document_errors(serializer, instance_options)
else
Error.param_errors(serializer, instance_options)
end
Expand Down
19 changes: 17 additions & 2 deletions lib/caprese/adapter/json_api/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ def self.param_errors(error_serializer, options)
]
end

def self.document_errors(error_serializer, options)
error_attributes = error_serializer.as_json
[
{
code: error_attributes[:code],
detail: error_attributes[:message],
source: error_source(:pointer, nil, error_attributes[:field])
}
]
end

# Builds a JSON API Errors Object
# {http://jsonapi.org/format/#errors JSON API Errors}
#
Expand Down Expand Up @@ -89,12 +100,16 @@ def self.attribute_error_objects(record, attribute_name, attribute_errors)
# parameter: 'pres'
# }
# end
RESERVED_ATTRIBUTES = %w(type)
RESERVED_ATTRIBUTES = %w(id type)
def self.error_source(source_type, record, attribute_name)
case source_type
when :pointer
if attribute_name == :base
{
pointer: JsonApi::JsonPointer.new(:base, record, attribute_name)
}
# [type ...] and other primary data variables
if RESERVED_ATTRIBUTES.include?(attribute_name.to_s)
elsif RESERVED_ATTRIBUTES.include?(attribute_name.to_s)
{
pointer: JsonApi::JsonPointer.new(:primary_data, record, attribute_name)
}
Expand Down
5 changes: 3 additions & 2 deletions lib/caprese/adapter/json_api/json_pointer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ module JsonPointer
relationship_attribute: '/data/relationships/%s'.freeze,
relationship_base: '/data/relationships/%s/data'.freeze,
relationship_primary_data: '/data/relationships/%s/data/%s'.freeze,
primary_data: '/data/%s'.freeze
primary_data: '/data/%s'.freeze,
base: '/data'.freeze
}.freeze

# Iterates over the field of an error and converts it to a pointer in JSON API format
Expand Down Expand Up @@ -50,7 +51,7 @@ def new(pointer_type, record, value)
end
end
else
format(POINTERS[pointer_type], *[value].flatten)
format(POINTERS[pointer_type], *Array.wrap(value))
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/caprese/controller/concerns/aliasing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Caprese
module Aliasing
extend ActiveSupport::Concern

# Records all of the field aliases engaged by the API request (called in `assign_record_attributes` using comparison)
# Records all of the field aliases engaged by the API request (called in `assign_changes_from_document` using comparison)
# so that when the response is returned, the appropriate alias is used in reference to fields
#
# Success: @todo
Expand Down

0 comments on commit 81cae50

Please sign in to comment.