Skip to content

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

Merged
merged 4 commits into from
Jul 25, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions lib/jsonapi/rails/configuration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module JSONAPI

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.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing magic comment # frozen_string_literal: true.

module Rails

Choose a reason for hiding this comment

The 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(&block)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused method argument - block. If it's necessary, use _ or _block as an argument name to indicate that it won't be used. You can also write as configure(*) if you want the method to accept any arguments but don't care about them.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused method argument - block. If it's necessary, use _ or _block as an argument name to indicate that it won't be used. You can also write as configure(*) if you want the method to accept any arguments but don't care about them.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused method argument - block. If it's necessary, use _ or _block as an argument name to indicate that it won't be used. You can also write as configure(*) if you want the method to accept any arguments but don't care about them.

yield config
end

def self.config
@config ||= JSONAPI::Rails::Configuration.new(DEFAULT_CONFIG)
end
end
end
29 changes: 18 additions & 11 deletions lib/jsonapi/rails/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [86/80]

Choose a reason for hiding this comment

The 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
Expand Down
39 changes: 39 additions & 0 deletions spec/config_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require 'rails_helper'

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.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing magic comment # frozen_string_literal: true.


RSpec.describe JSONAPI::Rails.config do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Block has too many lines. [29/25]

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