Skip to content

Commit 3bd63fe

Browse files
authored
Add configuration/hook for meta. (#54)
1 parent f994722 commit 3bd63fe

File tree

6 files changed

+91
-3
lines changed

6 files changed

+91
-3
lines changed

Diff for: CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
### Added
22

3+
* Default configuration and hooks for the `meta` parameter.
34
* Default configuration and hooks for the `cache` parameter.
45
* Default configuration and hooks for the `links` parameter.
56
* Default configuration and hooks for the `fields` parameter.

Diff for: lib/generators/jsonapi/initializer/templates/initializer.rb

+4
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@
6262
# # A lambda/proc that will be eval'd in the controller context.
6363
# config.jsonapi_links = ->() { {} }
6464
#
65+
# # Set default meta.
66+
# # A lambda/proc that will be eval'd in the controller context.
67+
# config.jsonapi_meta = ->() { nil }
68+
#
6569
# # Set a default pagination scheme.
6670
# config.jsonapi_pagination = ->(_) { {} }
6771
end

Diff for: lib/jsonapi/rails/configuration.rb

+3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ module Configurable
3535

3636
DEFAULT_JSONAPI_LINKS = ->() { {} }
3737

38+
DEFAULT_JSONAPI_META = ->() { nil }
39+
3840
DEFAULT_JSONAPI_PAGINATION = ->(_) { {} }
3941

4042
DEFAULT_CONFIG = {
@@ -45,6 +47,7 @@ module Configurable
4547
jsonapi_fields: DEFAULT_JSONAPI_FIELDS,
4648
jsonapi_include: DEFAULT_JSONAPI_INCLUDE,
4749
jsonapi_links: DEFAULT_JSONAPI_LINKS,
50+
jsonapi_meta: DEFAULT_JSONAPI_META,
4851
jsonapi_object: DEFAULT_JSONAPI_OBJECT,
4952
jsonapi_pagination: DEFAULT_JSONAPI_PAGINATION
5053
}.freeze

Diff for: lib/jsonapi/rails/controller.rb

+9-3
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def jsonapi_errors_class
9999

100100
# Hook for the jsonapi object.
101101
# Overridden by the `jsonapi_object` renderer option.
102-
# @return [Hash]
102+
# @return [Hash,nil]
103103
def jsonapi_object
104104
JSONAPI::Rails.config[:jsonapi_object]
105105
end
@@ -117,7 +117,7 @@ def jsonapi_cache
117117
end
118118

119119
# Hook for default fields.
120-
# @return [Hash{Symbol=>Array<Symbol>}]
120+
# @return [Hash{Symbol=>Array<Symbol>},nil]
121121
def jsonapi_fields
122122
instance_exec(&JSONAPI::Rails.config[:jsonapi_fields])
123123
end
@@ -129,11 +129,17 @@ def jsonapi_include
129129
end
130130

131131
# Hook for default links.
132-
# @return [IncludeDirective]
132+
# @return [Hash]
133133
def jsonapi_links
134134
instance_exec(&JSONAPI::Rails.config[:jsonapi_links])
135135
end
136136

137+
# Hook for default meta.
138+
# @return [Hash,nil]
139+
def jsonapi_meta
140+
instance_exec(&JSONAPI::Rails.config[:jsonapi_meta])
141+
end
142+
137143
# Hook for pagination scheme.
138144
# @return [Hash]
139145
def jsonapi_pagination(resources)

Diff for: lib/jsonapi/rails/renderer.rb

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def default_options(options, controller, resources)
3232
opts[:include] ||= controller.jsonapi_include
3333
opts[:jsonapi] = opts.delete(:jsonapi_object) ||
3434
controller.jsonapi_object
35+
opts[:meta] ||= controller.jsonapi_meta
3536
end
3637
end
3738
# rubocop:enable Metrics/MethodLength, Metrics/AbcSize

Diff for: spec/controller/meta_spec.rb

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
require 'rails_helper'
2+
3+
describe ActionController::Base, type: :controller do
4+
context 'when using default configuration' do
5+
controller do
6+
def index
7+
render jsonapi: nil
8+
end
9+
end
10+
11+
subject { JSON.parse(response.body) }
12+
13+
it 'does not add meta' do
14+
get :index
15+
16+
expect(subject.key?('meta')).to be false
17+
end
18+
end
19+
20+
context 'when using a custom configuration' do
21+
controller do
22+
def index
23+
render jsonapi: nil
24+
end
25+
end
26+
27+
subject { JSON.parse(response.body) }
28+
29+
it 'modifies serialized meta' do
30+
with_config(jsonapi_meta: ->() { { foo: 'bar' } }) do
31+
get :index
32+
end
33+
34+
expect(subject['meta']).to eq('foo' => 'bar')
35+
end
36+
end
37+
38+
context 'when customizing jsonapi_meta hook' do
39+
controller do
40+
def index
41+
render jsonapi: nil
42+
end
43+
44+
def jsonapi_meta
45+
{ foo: 'bar' }
46+
end
47+
end
48+
49+
subject { JSON.parse(response.body) }
50+
51+
it 'modifies serialized meta' do
52+
get :index
53+
54+
expect(subject['meta']).to eq('foo' => 'bar')
55+
end
56+
end
57+
58+
context 'when prividing meta renderer option' do
59+
controller do
60+
def index
61+
render jsonapi: nil, meta: { foo: 'bar' }
62+
end
63+
end
64+
65+
subject { JSON.parse(response.body) }
66+
67+
it 'modifies serialized meta' do
68+
get :index
69+
70+
expect(subject['meta']).to eq('foo' => 'bar')
71+
end
72+
end
73+
end

0 commit comments

Comments
 (0)