Skip to content

Commit 481b5a3

Browse files
authored
Conditionally include controller mixin (#40)
* Add option to prevent JSONAPI::Rails::Controller to be mixed in.
1 parent a904687 commit 481b5a3

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

lib/generators/jsonapi/initializer/templates/initializer.rb

+1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
# config.register_mime_type = true
33
# config.register_param_parser = true
44
# config.register_renderers = true
5+
# config.extend_action_controller = true
56
end

lib/jsonapi/rails/configuration.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ class Configuration < ActiveSupport::InheritableOptions; end
55
DEFAULT_CONFIG = {
66
register_parameter_parser: true,
77
register_mime_type: true,
8-
register_renderers: true
8+
register_renderers: true,
9+
extend_action_controller: true
910
}.freeze
1011

1112
def self.configure

lib/jsonapi/rails/railtie.rb

+16-12
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,27 @@ class Railtie < ::Rails::Railtie
1616
jsonapi_error: ErrorsRenderer.new
1717
}.freeze
1818

19-
initializer 'jsonapi-rails.action_controller' do
20-
ActiveSupport.on_load(:action_controller) do
21-
include ::JSONAPI::Rails::Controller
19+
initializer 'jsonapi.init', after: :load_config_initializers do
20+
if JSONAPI::Rails.config.register_mime_type
21+
Mime::Type.register MEDIA_TYPE, :jsonapi
22+
end
2223

23-
if JSONAPI::Rails.config.register_mime_type
24-
Mime::Type.register MEDIA_TYPE, :jsonapi
24+
if JSONAPI::Rails.config.register_parameter_parser
25+
if ::Rails::VERSION::MAJOR >= 5
26+
::ActionDispatch::Request.parameter_parsers[:jsonapi] = PARSER
27+
else
28+
::ActionDispatch::ParamsParser::DEFAULT_PARSERS[Mime[:jsonapi]] = PARSER
2529
end
30+
end
2631

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
32+
if JSONAPI::Rails.config.extend_action_controller
33+
ActiveSupport.on_load(:action_controller) do
34+
include ::JSONAPI::Rails::Controller
3335
end
36+
end
3437

35-
if JSONAPI::Rails.config.register_renderers
38+
if JSONAPI::Rails.config.register_renderers
39+
ActiveSupport.on_load(:action_controller) do
3640
RENDERERS.each do |name, renderer|
3741
::ActionController::Renderers.add(name) do |resources, options|
3842
# Renderer proc is evaluated in the controller context.

0 commit comments

Comments
 (0)