From e6ec89f91d89b367a7312666c7f622c2a76b3dbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jannis=20H=C3=BCbl?= Date: Fri, 16 Feb 2018 15:29:46 +0100 Subject: [PATCH] allow to access included in relation sideload --- lib/sinja.rb | 9 +++++++++ lib/sinja/helpers/relationships.rb | 1 + 2 files changed, 10 insertions(+) diff --git a/lib/sinja.rb b/lib/sinja.rb index 5d20636..e77245e 100644 --- a/lib/sinja.rb +++ b/lib/sinja.rb @@ -157,6 +157,15 @@ def data end end + def included + @included ||= {} + @included[request.path] ||= begin + deserialize_request_body.fetch(:included, nil) + rescue NoMethodError, KeyError + raise BadRequestError, 'Malformed {json:api} request payload' + end + end + def normalize_filter_params return {} unless params[:filter]&.any? diff --git a/lib/sinja/helpers/relationships.rb b/lib/sinja/helpers/relationships.rb index e7579ca..90f9ef1 100644 --- a/lib/sinja/helpers/relationships.rb +++ b/lib/sinja/helpers/relationships.rb @@ -23,6 +23,7 @@ def dispatch_relationship_requests!(id, methods: {}, **opts) rels = data.fetch(:relationships, {}).to_a rels.each do |rel, body, rel_type=nil, count=0| rel_type ||= settings._resource_config[:has_one].key?(rel) ? :has_one : :has_many + body = body.merge(included: included) if included code, _, *json = dispatch_relationship_request id, rel, opts.merge(:body=>body, :method=>methods.fetch(rel_type, :patch))