From c7a939cbcb8b93c1b692614a79e31591a2afdb39 Mon Sep 17 00:00:00 2001 From: Michael Deutsch Date: Thu, 22 Jun 2017 15:12:23 -0400 Subject: [PATCH 1/3] Add jsonapi_pointers method to ActionController. --- lib/jsonapi/rails/action_controller.rb | 8 ++++++++ lib/jsonapi/rails/railtie.rb | 7 +++---- spec/action_controller_spec.rb | 19 +++++++++++++++++++ 3 files changed, 30 insertions(+), 4 deletions(-) create mode 100644 spec/action_controller_spec.rb diff --git a/lib/jsonapi/rails/action_controller.rb b/lib/jsonapi/rails/action_controller.rb index 8f8ffbc..9d7c87e 100644 --- a/lib/jsonapi/rails/action_controller.rb +++ b/lib/jsonapi/rails/action_controller.rb @@ -4,6 +4,8 @@ module JSONAPI module Rails module ActionController + extend ActiveSupport::Concern + REVERSE_MAPPING_KEY = 'jsonapi_deserializable.reverse_mapping'.freeze module ClassMethods @@ -30,6 +32,12 @@ def _deserializable(key, options, fallback, &block) end end end + + private + + def jsonapi_pointers + request.env[REVERSE_MAPPING_KEY] + end end end end diff --git a/lib/jsonapi/rails/railtie.rb b/lib/jsonapi/rails/railtie.rb index 2ac26e4..b37d523 100644 --- a/lib/jsonapi/rails/railtie.rb +++ b/lib/jsonapi/rails/railtie.rb @@ -17,7 +17,7 @@ class Railtie < ::Rails::Railtie initializer 'jsonapi-rails.action_controller' do ActiveSupport.on_load(:action_controller) do require 'jsonapi/rails/action_controller' - extend ::JSONAPI::Rails::ActionController::ClassMethods + include ::JSONAPI::Rails::ActionController Mime::Type.register MEDIA_TYPE, :jsonapi @@ -35,9 +35,8 @@ class Railtie < ::Rails::Railtie ::ActionController::Renderers.add(:jsonapi_error) do |errors, options| # Renderer proc is evaluated in the controller context, so it - # has access to the request object. - reverse_mapping = request.env[ActionController::REVERSE_MAPPING_KEY] - options = options.merge(_reverse_mapping: reverse_mapping) + # has access to the jsonapi_pointers method. + options = options.merge(_jsonapi_pointers: jsonapi_pointers) self.content_type ||= Mime[:jsonapi] RENDERERS[:jsonapi_error].render(errors, options).to_json diff --git a/spec/action_controller_spec.rb b/spec/action_controller_spec.rb new file mode 100644 index 0000000..9aa9705 --- /dev/null +++ b/spec/action_controller_spec.rb @@ -0,0 +1,19 @@ +require 'rails_helper' + +RSpec.describe JSONAPI::Rails::ActionController do + class TestController < ActionController::Base + deserializable_resource "things" + end + + let(:controller) { TestController.new } + + context 'source pointers' do + it 'should fetch the mapping created during deserialization' do + reverse_mapping = {id: "/data/id", type: "/data/type"} + allow(controller).to receive(:request) do + OpenStruct.new(env: {'jsonapi_deserializable.reverse_mapping' => reverse_mapping}) + end + expect(controller.send(:jsonapi_pointers)).to equal reverse_mapping + end + end +end From 5dbf28da60f0ce4924238b5418edf9a6f44ada73 Mon Sep 17 00:00:00 2001 From: Lucas Date: Thu, 13 Jul 2017 18:46:06 +0200 Subject: [PATCH 2/3] Rename reverse_mapping key to jsonapi_pointers. --- lib/jsonapi/rails/action_controller.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/jsonapi/rails/action_controller.rb b/lib/jsonapi/rails/action_controller.rb index 9d7c87e..d3b1aab 100644 --- a/lib/jsonapi/rails/action_controller.rb +++ b/lib/jsonapi/rails/action_controller.rb @@ -6,9 +6,9 @@ module Rails module ActionController extend ActiveSupport::Concern - REVERSE_MAPPING_KEY = 'jsonapi_deserializable.reverse_mapping'.freeze + JSONAPI_POINTERS_KEY = 'jsonapi_deserializable.jsonapi_pointers'.freeze - module ClassMethods + class_methods do def deserializable_resource(key, options = {}, &block) _deserializable(key, options, JSONAPI::Deserializable::Resource, &block) @@ -26,7 +26,7 @@ def _deserializable(key, options, fallback, &block) before_action(options) do |controller| resource = klass.new(controller.params[:_jsonapi].to_unsafe_hash) - controller.request.env[REVERSE_MAPPING_KEY] = + controller.request.env[JSONAPI_POINTERS_KEY] = resource.reverse_mapping controller.params[key.to_sym] = resource.to_hash end @@ -36,7 +36,7 @@ def _deserializable(key, options, fallback, &block) private def jsonapi_pointers - request.env[REVERSE_MAPPING_KEY] + request.env[JSONAPI_POINTERS_KEY] end end end From 2c91b912e3a7c5e4da3afabbd3d79cbd9e71a3b8 Mon Sep 17 00:00:00 2001 From: Lucas Date: Thu, 13 Jul 2017 18:52:29 +0200 Subject: [PATCH 3/3] Clean up spec. --- spec/action_controller_spec.rb | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/spec/action_controller_spec.rb b/spec/action_controller_spec.rb index 9aa9705..bd608cd 100644 --- a/spec/action_controller_spec.rb +++ b/spec/action_controller_spec.rb @@ -1,19 +1,17 @@ require 'rails_helper' -RSpec.describe JSONAPI::Rails::ActionController do - class TestController < ActionController::Base - deserializable_resource "things" - end - - let(:controller) { TestController.new } +RSpec.describe ActionController::Base do + it 'exposes the deserialization mapping via the jsonapi_pointers method' do + pointers = { id: '/data/id', type: '/data/type' } - context 'source pointers' do - it 'should fetch the mapping created during deserialization' do - reverse_mapping = {id: "/data/id", type: "/data/type"} - allow(controller).to receive(:request) do - OpenStruct.new(env: {'jsonapi_deserializable.reverse_mapping' => reverse_mapping}) - end - expect(controller.send(:jsonapi_pointers)).to equal reverse_mapping + allow(subject).to receive(:request) do + OpenStruct.new( + env: { + JSONAPI::Rails::ActionController::JSONAPI_POINTERS_KEY => pointers + } + ) end + + expect(subject.send(:jsonapi_pointers)).to equal pointers end end