Skip to content

Commit 0d513ca

Browse files
ezekgbeauby
authored andcommitted
Add library configuration object (#17)
* Add library configuration object Implements a global configuration object so that we can conditionally register things such as the renderer, parameter parser and mime type.
1 parent 7092fe6 commit 0d513ca

File tree

3 files changed

+76
-11
lines changed

3 files changed

+76
-11
lines changed

lib/jsonapi/rails/configuration.rb

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module JSONAPI
2+
module Rails
3+
class Configuration < ActiveSupport::InheritableOptions; end
4+
5+
DEFAULT_CONFIG = {
6+
register_parameter_parser: true,
7+
register_mime_type: true,
8+
register_renderers: true
9+
}.freeze
10+
11+
def self.configure
12+
yield config
13+
end
14+
15+
def self.config
16+
@config ||= JSONAPI::Rails::Configuration.new(DEFAULT_CONFIG)
17+
end
18+
end
19+
end

lib/jsonapi/rails/railtie.rb

+18-11
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
require 'action_controller'
33
require 'active_support'
44

5+
require 'jsonapi/rails/configuration'
6+
require 'jsonapi/rails/controller'
57
require 'jsonapi/rails/parser'
68
require 'jsonapi/rails/renderer'
7-
require 'jsonapi/rails/controller'
89

910
module JSONAPI
1011
module Rails
@@ -19,20 +20,26 @@ class Railtie < ::Rails::Railtie
1920
ActiveSupport.on_load(:action_controller) do
2021
include ::JSONAPI::Rails::Controller
2122

22-
Mime::Type.register MEDIA_TYPE, :jsonapi
23+
if JSONAPI::Rails.config.register_mime_type
24+
Mime::Type.register MEDIA_TYPE, :jsonapi
25+
end
2326

24-
if ::Rails::VERSION::MAJOR >= 5
25-
::ActionDispatch::Request.parameter_parsers[:jsonapi] = PARSER
26-
else
27-
::ActionDispatch::ParamsParser::DEFAULT_PARSERS[Mime[:jsonapi]] = PARSER
27+
if JSONAPI::Rails.config.register_parameter_parser
28+
if ::Rails::VERSION::MAJOR >= 5
29+
::ActionDispatch::Request.parameter_parsers[:jsonapi] = PARSER
30+
else
31+
::ActionDispatch::ParamsParser::DEFAULT_PARSERS[Mime[:jsonapi]] = PARSER
32+
end
2833
end
2934

30-
RENDERERS.each do |name, renderer|
31-
::ActionController::Renderers.add(name) do |resources, options|
32-
# Renderer proc is evaluated in the controller context.
33-
self.content_type ||= Mime[:jsonapi]
35+
if JSONAPI::Rails.config.register_renderers
36+
RENDERERS.each do |name, renderer|
37+
::ActionController::Renderers.add(name) do |resources, options|
38+
# Renderer proc is evaluated in the controller context.
39+
self.content_type ||= Mime[:jsonapi]
3440

35-
renderer.render(resources, options, self).to_json
41+
renderer.render(resources, options, self).to_json
42+
end
3643
end
3744
end
3845
end

spec/config_spec.rb

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
require 'rails_helper'
2+
3+
describe JSONAPI::Rails.config do
4+
context 'when the default configuration is used' do
5+
it 'should register the jsonapi parameter parser' do
6+
expect(JSONAPI::Rails.config.register_parameter_parser).to be true
7+
end
8+
9+
it 'should register the jsonapi mime type' do
10+
expect(JSONAPI::Rails.config.register_mime_type).to be true
11+
end
12+
13+
it 'should register the jsonapi renderers' do
14+
expect(JSONAPI::Rails.config.register_renderers).to be true
15+
end
16+
end
17+
18+
context 'when a custom configuration is used' do
19+
before do
20+
JSONAPI::Rails.configure do |config|
21+
config.register_parameter_parser = false
22+
config.register_mime_type = false
23+
config.register_renderers = false
24+
end
25+
end
26+
27+
it 'should not register the jsonapi parameter parser' do
28+
expect(JSONAPI::Rails.config.register_parameter_parser).to be false
29+
end
30+
31+
it 'should not register the jsonapi mime type' do
32+
expect(JSONAPI::Rails.config.register_mime_type).to be false
33+
end
34+
35+
it 'should not register the jsonapi renderers' do
36+
expect(JSONAPI::Rails.config.register_renderers).to be false
37+
end
38+
end
39+
end

0 commit comments

Comments
 (0)