Skip to content

Commit c5b4cf7

Browse files
author
Michael Deutsch
committed
Merge remote-tracking branch 'origin/expose_reverse_mapping'
* origin/expose_reverse_mapping: Add jsonapi_pointers method to ActionController.
2 parents f325daf + e5fc28c commit c5b4cf7

File tree

4 files changed

+31
-6
lines changed

4 files changed

+31
-6
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
@@ -18,7 +18,7 @@ class Railtie < ::Rails::Railtie
1818
initializer 'jsonapi-rails.action_controller' do
1919
ActiveSupport.on_load(:action_controller) do
2020
require 'jsonapi/rails/action_controller'
21-
extend ::JSONAPI::Rails::ActionController::ClassMethods
21+
include ::JSONAPI::Rails::ActionController
2222

2323
if JSONAPI::Rails.config.register_mime_type
2424
Mime::Type.register MEDIA_TYPE, :jsonapi

lib/jsonapi/rails/renderer.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def self.render(errors, options)
2424
if error.respond_to?(:as_jsonapi)
2525
error
2626
elsif error.is_a?(ActiveModel::Errors)
27-
ActiveModelErrors.new(error, options[:_reverse_mapping]).to_a
27+
ActiveModelErrors.new(error, options[:_jsonapi_pointers]).to_a
2828
elsif error.is_a?(Hash)
2929
JSONAPI::Serializable::Error.create(error)
3030
else
@@ -41,10 +41,8 @@ def self.render(errors, options)
4141
# @api private
4242
def rails_renderer(renderer)
4343
proc do |json, options|
44-
# Renderer proc is evaluated in the controller context, so it
45-
# has access to the request object.
46-
reverse_mapping = request.env[ActionController::REVERSE_MAPPING_KEY]
47-
options = options.merge(_reverse_mapping: reverse_mapping)
44+
# Renderer proc is evaluated in the controller context.
45+
options = options.merge(_jsonapi_pointers: jsonapi_pointers)
4846
json = renderer.render(json, options) unless json.is_a?(String)
4947
self.content_type ||= Mime[:jsonapi]
5048
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)