|
4 | 4 | module JSONAPI
|
5 | 5 | module Rails
|
6 | 6 | class SuccessRenderer
|
7 |
| - def initialize(renderer = JSONAPI::Serializable::SuccessRenderer.new) |
| 7 | + def initialize(renderer = JSONAPI::Serializable::Renderer.new) |
8 | 8 | @renderer = renderer
|
9 | 9 |
|
10 | 10 | freeze
|
11 | 11 | end
|
12 | 12 |
|
13 | 13 | def render(resources, options, controller)
|
14 |
| - options = options.dup |
15 |
| - |
16 |
| - if (pagination_links = controller.jsonapi_pagination(resources)) |
17 |
| - (options[:links] ||= {}).merge!(pagination_links) |
18 |
| - end |
19 |
| - options[:expose] = |
20 |
| - controller.jsonapi_expose.merge!(options[:expose] || {}) |
21 |
| - options[:jsonapi] = |
22 |
| - options[:jsonapi_object] || controller.jsonapi_object |
| 14 | + options = default_options(options, controller, resources) |
23 | 15 |
|
24 | 16 | @renderer.render(resources, options)
|
25 | 17 | end
|
| 18 | + |
| 19 | + private |
| 20 | + |
| 21 | + # @api private |
| 22 | + def default_options(options, controller, resources) |
| 23 | + options.dup.tap do |options| |
| 24 | + if (pagination_links = controller.jsonapi_pagination(resources)) |
| 25 | + (options[:links] ||= {}).merge!(pagination_links) |
| 26 | + end |
| 27 | + options[:expose] = controller.jsonapi_expose |
| 28 | + .merge!(options[:expose] || {}) |
| 29 | + options[:jsonapi] = |
| 30 | + options[:jsonapi_object] || controller.jsonapi_object |
| 31 | + end |
| 32 | + end |
26 | 33 | end
|
27 | 34 |
|
28 | 35 | class ErrorsRenderer
|
29 |
| - def initialize(renderer = JSONAPI::Serializable::ErrorsRenderer.new) |
| 36 | + def initialize(renderer = JSONAPI::Serializable::Renderer.new) |
30 | 37 | @renderer = renderer
|
31 | 38 |
|
32 | 39 | freeze
|
33 | 40 | end
|
34 | 41 |
|
35 | 42 | def render(errors, options, controller)
|
36 |
| - options = options.merge(_jsonapi_pointers: controller.jsonapi_pointers) |
37 |
| - # TODO(beauby): SerializableError inference on AR validation errors. |
| 43 | + options = default_options(options, controller) |
38 | 44 |
|
39 | 45 | errors = [errors] unless errors.is_a?(Array)
|
40 | 46 | errors = errors.flat_map do |error|
|
41 |
| - if error.respond_to?(:as_jsonapi) |
42 |
| - error |
43 |
| - elsif error.is_a?(ActiveModel::Errors) |
44 |
| - ActiveModelErrors.new(error, options[:_jsonapi_pointers]).to_a |
45 |
| - elsif error.is_a?(Hash) |
46 |
| - JSONAPI::Serializable::Error.create(error) |
| 47 | + if error.is_a?(ActiveModel::Errors) |
| 48 | + klass = if options[:inferrer] && |
| 49 | + options[:inferrer].key?(:'ActiveModel::Errors') |
| 50 | + options[:inferrer][:'ActiveModel::Errors'] |
| 51 | + else |
| 52 | + JSONAPI::Rails::ActiveModelErrors |
| 53 | + end |
| 54 | + klass = klass.safe_constantize if klass.is_a?(String) |
| 55 | + klass.new(options[:expose].merge(object: error)).to_a |
47 | 56 | else
|
48 |
| - raise # TODO(lucas): Raise meaningful exception. |
| 57 | + error |
49 | 58 | end
|
50 | 59 | end
|
51 | 60 |
|
52 |
| - @renderer.render(errors, options) |
| 61 | + @renderer.render_errors(errors, options) |
| 62 | + end |
| 63 | + |
| 64 | + private |
| 65 | + |
| 66 | + # @api private |
| 67 | + def default_options(options, controller) |
| 68 | + options.dup.tap do |options| |
| 69 | + options[:expose] = |
| 70 | + controller.jsonapi_expose |
| 71 | + .merge!(options[:expose] || {}) |
| 72 | + .merge!(_jsonapi_pointers: controller.jsonapi_pointers) |
| 73 | + options[:jsonapi] = |
| 74 | + options[:jsonapi_object] || controller.jsonapi_object |
| 75 | + end |
53 | 76 | end
|
54 | 77 | end
|
55 | 78 | end
|
|
0 commit comments