-
Notifications
You must be signed in to change notification settings - Fork 64
Add library configuration object #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
module JSONAPI | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing magic comment # frozen_string_literal: true. |
||
module Rails | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing top-level module documentation comment. |
||
class Configuration < ActiveSupport::InheritableOptions; end | ||
|
||
DEFAULT_CONFIG = { | ||
register_parameter_parser: true, | ||
register_mime_type: true, | ||
register_renderers: true | ||
}.freeze | ||
|
||
def self.configure | ||
yield config | ||
end | ||
|
||
def self.config | ||
@config ||= JSONAPI::Rails::Configuration.new(DEFAULT_CONFIG) | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,9 +2,10 @@ | |
require 'action_controller' | ||
require 'active_support' | ||
|
||
require 'jsonapi/rails/configuration' | ||
require 'jsonapi/rails/controller' | ||
require 'jsonapi/rails/parser' | ||
require 'jsonapi/rails/renderer' | ||
require 'jsonapi/rails/controller' | ||
|
||
module JSONAPI | ||
module Rails | ||
|
@@ -19,20 +20,26 @@ class Railtie < ::Rails::Railtie | |
ActiveSupport.on_load(:action_controller) do | ||
include ::JSONAPI::Rails::Controller | ||
|
||
Mime::Type.register MEDIA_TYPE, :jsonapi | ||
if JSONAPI::Rails.config.register_mime_type | ||
Mime::Type.register MEDIA_TYPE, :jsonapi | ||
end | ||
|
||
if ::Rails::VERSION::MAJOR >= 5 | ||
::ActionDispatch::Request.parameter_parsers[:jsonapi] = PARSER | ||
else | ||
::ActionDispatch::ParamsParser::DEFAULT_PARSERS[Mime[:jsonapi]] = PARSER | ||
if JSONAPI::Rails.config.register_parameter_parser | ||
if ::Rails::VERSION::MAJOR >= 5 | ||
::ActionDispatch::Request.parameter_parsers[:jsonapi] = PARSER | ||
else | ||
::ActionDispatch::ParamsParser::DEFAULT_PARSERS[Mime[:jsonapi]] = PARSER | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line is too long. [86/80] There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line is too long. [86/80] |
||
end | ||
end | ||
|
||
RENDERERS.each do |name, renderer| | ||
::ActionController::Renderers.add(name) do |resources, options| | ||
# Renderer proc is evaluated in the controller context. | ||
self.content_type ||= Mime[:jsonapi] | ||
if JSONAPI::Rails.config.register_renderers | ||
RENDERERS.each do |name, renderer| | ||
::ActionController::Renderers.add(name) do |resources, options| | ||
# Renderer proc is evaluated in the controller context. | ||
self.content_type ||= Mime[:jsonapi] | ||
|
||
renderer.render(resources, options, self).to_json | ||
renderer.render(resources, options, self).to_json | ||
end | ||
end | ||
end | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
require 'rails_helper' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing frozen string literal comment. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing magic comment # frozen_string_literal: true. |
||
|
||
describe JSONAPI::Rails.config do | ||
context 'when the default configuration is used' do | ||
it 'should register the jsonapi parameter parser' do | ||
expect(JSONAPI::Rails.config.register_parameter_parser).to be true | ||
end | ||
|
||
it 'should register the jsonapi mime type' do | ||
expect(JSONAPI::Rails.config.register_mime_type).to be true | ||
end | ||
|
||
it 'should register the jsonapi renderers' do | ||
expect(JSONAPI::Rails.config.register_renderers).to be true | ||
end | ||
end | ||
|
||
context 'when a custom configuration is used' do | ||
before do | ||
JSONAPI::Rails.configure do |config| | ||
config.register_parameter_parser = false | ||
config.register_mime_type = false | ||
config.register_renderers = false | ||
end | ||
end | ||
|
||
it 'should not register the jsonapi parameter parser' do | ||
expect(JSONAPI::Rails.config.register_parameter_parser).to be false | ||
end | ||
|
||
it 'should not register the jsonapi mime type' do | ||
expect(JSONAPI::Rails.config.register_mime_type).to be false | ||
end | ||
|
||
it 'should not register the jsonapi renderers' do | ||
expect(JSONAPI::Rails.config.register_renderers).to be false | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing frozen string literal comment.