Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions lib/jsonapi/rails/action_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
module JSONAPI
module Rails
module ActionController
REVERSE_MAPPING_KEY = 'jsonapi_deserializable.reverse_mapping'.freeze
extend ActiveSupport::Concern

module ClassMethods
JSONAPI_POINTERS_KEY = 'jsonapi_deserializable.jsonapi_pointers'.freeze

class_methods do
def deserializable_resource(key, options = {}, &block)
_deserializable(key, options,
JSONAPI::Deserializable::Resource, &block)
Expand All @@ -24,12 +26,18 @@ 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
end
end

private

def jsonapi_pointers
request.env[JSONAPI_POINTERS_KEY]
end
end
end
end
7 changes: 3 additions & 4 deletions lib/jsonapi/rails/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
17 changes: 17 additions & 0 deletions spec/action_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'rails_helper'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing magic comment # frozen_string_literal: true.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing magic comment # frozen_string_literal: true.


RSpec.describe ActionController::Base do
it 'exposes the deserialization mapping via the jsonapi_pointers method' do
pointers = { id: '/data/id', type: '/data/type' }

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