Skip to content

Commit c7a939c

Browse files
Michael Deutschbeauby
Michael Deutsch
authored andcommitted
Add jsonapi_pointers method to ActionController.
1 parent 927f030 commit c7a939c

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

lib/jsonapi/rails/action_controller.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
module JSONAPI
55
module Rails
66
module ActionController
7+
extend ActiveSupport::Concern
8+
79
REVERSE_MAPPING_KEY = 'jsonapi_deserializable.reverse_mapping'.freeze
810

911
module ClassMethods
@@ -30,6 +32,12 @@ def _deserializable(key, options, fallback, &block)
3032
end
3133
end
3234
end
35+
36+
private
37+
38+
def jsonapi_pointers
39+
request.env[REVERSE_MAPPING_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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
require 'rails_helper'
2+
3+
RSpec.describe JSONAPI::Rails::ActionController do
4+
class TestController < ActionController::Base
5+
deserializable_resource "things"
6+
end
7+
8+
let(:controller) { TestController.new }
9+
10+
context 'source pointers' do
11+
it 'should fetch the mapping created during deserialization' do
12+
reverse_mapping = {id: "/data/id", type: "/data/type"}
13+
allow(controller).to receive(:request) do
14+
OpenStruct.new(env: {'jsonapi_deserializable.reverse_mapping' => reverse_mapping})
15+
end
16+
expect(controller.send(:jsonapi_pointers)).to equal reverse_mapping
17+
end
18+
end
19+
end

0 commit comments

Comments
 (0)