Skip to content

Commit e5fc28c

Browse files
author
Michael Deutsch
committed
Add jsonapi_pointers method to ActionController.
1 parent ee3acf6 commit e5fc28c

File tree

4 files changed

+30
-5
lines changed

4 files changed

+30
-5
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: 1 addition & 1 deletion
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

lib/jsonapi/rails/renderer.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,8 @@ def self.render(errors, options)
2828
# @api private
2929
def rails_renderer(renderer)
3030
proc do |json, options|
31-
# Renderer proc is evaluated in the controller context, so it
32-
# has access to the request object.
33-
reverse_mapping = request.env[ActionController::REVERSE_MAPPING_KEY]
34-
options = options.merge(_reverse_mapping: reverse_mapping)
31+
# Renderer proc is evaluated in the controller context.
32+
options = options.merge(_jsonapi_pointers: jsonapi_pointers)
3533
json = renderer.render(json, options) unless json.is_a?(String)
3634
self.content_type ||= Mime[:jsonapi]
3735
self.response_body = 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)