Skip to content

Commit b8375bf

Browse files
committed
Add hook for default jsonapi object.
1 parent 9bc6bfb commit b8375bf

File tree

4 files changed

+57
-25
lines changed

4 files changed

+57
-25
lines changed

lib/jsonapi/rails/action_controller.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ def deserializable_resource(key, options = {}, &block)
4040
end
4141
end
4242

43+
def jsonapi_object
44+
nil
45+
end
46+
4347
def jsonapi_expose
4448
{
4549
url_helpers: ::Rails.application.routes.url_helpers

lib/jsonapi/rails/railtie.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@ class Railtie < ::Rails::Railtie
3030
::ActionController::Renderers.add(:jsonapi) do |resources, options|
3131
self.content_type ||= Mime[:jsonapi]
3232

33+
options = options.dup
3334
# Renderer proc is evaluated in the controller context.
3435
if (pagination_links = jsonapi_pagination(resources))
3536
(options[:links] ||= {}).merge!(pagination_links)
3637
end
37-
options[:expose] = jsonapi_expose.merge!(options[:expose] || {})
38+
options[:expose] = jsonapi_expose.merge!(options[:expose] || {})
39+
options[:jsonapi] = options[:jsonapi_object] || jsonapi_object
3840

3941
RENDERERS[:jsonapi].render(resources, options).to_json
4042
end

lib/jsonapi/rails/renderer.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ def initialize(renderer = JSONAPI::Serializable::SuccessRenderer.new)
1010
end
1111

1212
def render(resources, options)
13-
opts = options.dup
14-
opts[:jsonapi] = opts.delete(:jsonapi_object)
15-
1613
@renderer.render(resources, opts)
1714
end
1815
end

spec/action_controller_spec.rb

Lines changed: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -95,35 +95,64 @@ def create
9595
end
9696
end
9797

98-
describe '#render jsonapi:' do
99-
controller do
100-
def index
101-
serializer = Class.new(JSONAPI::Serializable::Resource) do
102-
type :users
103-
attribute :name
98+
describe '#render' do
99+
context 'when calling render jsonapi: user' do
100+
controller do
101+
def index
102+
serializer = Class.new(JSONAPI::Serializable::Resource) do
103+
type :users
104+
attribute :name
105+
end
106+
user = OpenStruct.new(id: 1, name: 'Lucas')
107+
108+
render jsonapi: user, class: serializer
104109
end
105-
user = OpenStruct.new(id: 1, name: 'Lucas')
110+
end
111+
112+
subject { JSON.parse(response.body) }
113+
let(:serialized_user) do
114+
{
115+
'data' => {
116+
'id' => '1',
117+
'type' => 'users',
118+
'attributes' => { 'name' => 'Lucas' }
119+
}
120+
}
121+
end
122+
123+
it 'renders a JSON API success document' do
124+
get :index
106125

107-
render jsonapi: user, class: serializer
126+
expect(response.content_type).to eq('application/vnd.api+json')
127+
is_expected.to eq(serialized_user)
108128
end
109129
end
110130

111-
subject { JSON.parse(response.body) }
112-
let(:serialized_user) do
113-
{
114-
'data' => {
115-
'id' => '1',
116-
'type' => 'users',
117-
'attributes' => { 'name' => 'Lucas' }
131+
context 'when specifying a default jsonapi object' do
132+
controller do
133+
def index
134+
render jsonapi: nil
135+
end
136+
137+
def jsonapi_object
138+
{ version: '1.0' }
139+
end
140+
end
141+
142+
subject { JSON.parse(response.body) }
143+
let(:document) do
144+
{
145+
'data' => nil,
146+
'jsonapi' => { 'version' => '1.0' }
118147
}
119-
}
120-
end
148+
end
121149

122-
it 'renders a JSON API document' do
123-
get :index
150+
it 'renders a JSON API success document' do
151+
get :index
124152

125-
expect(response.content_type).to eq('application/vnd.api+json')
126-
is_expected.to eq(serialized_user)
153+
expect(response.content_type).to eq('application/vnd.api+json')
154+
is_expected.to eq(document)
155+
end
127156
end
128157
end
129158
end

0 commit comments

Comments
 (0)