Skip to content

Commit 0605a29

Browse files
Michael Deutschbeauby
Michael Deutsch
authored andcommitted
Expose deserialization reverse mapping in controllers (#29)
* Add jsonapi_pointers method to ActionController. * Rename reverse_mapping key to jsonapi_pointers.
1 parent 927f030 commit 0605a29

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

lib/jsonapi/rails/action_controller.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
module JSONAPI
55
module Rails
66
module ActionController
7-
REVERSE_MAPPING_KEY = 'jsonapi_deserializable.reverse_mapping'.freeze
7+
extend ActiveSupport::Concern
88

9-
module ClassMethods
9+
JSONAPI_POINTERS_KEY = 'jsonapi_deserializable.jsonapi_pointers'.freeze
10+
11+
class_methods do
1012
def deserializable_resource(key, options = {}, &block)
1113
_deserializable(key, options,
1214
JSONAPI::Deserializable::Resource, &block)
@@ -24,12 +26,18 @@ def _deserializable(key, options, fallback, &block)
2426

2527
before_action(options) do |controller|
2628
resource = klass.new(controller.params[:_jsonapi].to_unsafe_hash)
27-
controller.request.env[REVERSE_MAPPING_KEY] =
29+
controller.request.env[JSONAPI_POINTERS_KEY] =
2830
resource.reverse_mapping
2931
controller.params[key.to_sym] = resource.to_hash
3032
end
3133
end
3234
end
35+
36+
private
37+
38+
def jsonapi_pointers
39+
request.env[JSONAPI_POINTERS_KEY]
40+
end
3341
end
3442
end
3543
end

lib/jsonapi/rails/railtie.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Railtie < ::Rails::Railtie
1717
initializer 'jsonapi-rails.action_controller' do
1818
ActiveSupport.on_load(:action_controller) do
1919
require 'jsonapi/rails/action_controller'
20-
extend ::JSONAPI::Rails::ActionController::ClassMethods
20+
include ::JSONAPI::Rails::ActionController
2121

2222
Mime::Type.register MEDIA_TYPE, :jsonapi
2323

@@ -35,9 +35,8 @@ class Railtie < ::Rails::Railtie
3535

3636
::ActionController::Renderers.add(:jsonapi_error) do |errors, options|
3737
# Renderer proc is evaluated in the controller context, so it
38-
# has access to the request object.
39-
reverse_mapping = request.env[ActionController::REVERSE_MAPPING_KEY]
40-
options = options.merge(_reverse_mapping: reverse_mapping)
38+
# has access to the jsonapi_pointers method.
39+
options = options.merge(_jsonapi_pointers: jsonapi_pointers)
4140
self.content_type ||= Mime[:jsonapi]
4241

4342
RENDERERS[:jsonapi_error].render(errors, options).to_json

spec/action_controller_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require 'rails_helper'
2+
3+
RSpec.describe ActionController::Base do
4+
it 'exposes the deserialization mapping via the jsonapi_pointers method' do
5+
pointers = { id: '/data/id', type: '/data/type' }
6+
7+
allow(subject).to receive(:request) do
8+
OpenStruct.new(
9+
env: {
10+
JSONAPI::Rails::ActionController::JSONAPI_POINTERS_KEY => pointers
11+
}
12+
)
13+
end
14+
15+
expect(subject.send(:jsonapi_pointers)).to equal pointers
16+
end
17+
end

0 commit comments

Comments
 (0)