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

Add library configuration object #17

merged 4 commits into from
Jul 25, 2017

Conversation

ezekg
Copy link
Contributor

@ezekg ezekg commented Dec 19, 2016

Implemented along with this is a configuration option to define a custom parameter parser for the :jsonapi mime-type, something which I think is a needed addition.

end

context 'when a custom jsonapi parameter parser is used' do
let(:parser) { lambda {} }

Choose a reason for hiding this comment

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

Use the -> { ... } lambda literal syntax for single line lambdas.

@@ -0,0 +1,23 @@
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.

else
::ActionDispatch::ParamsParser::DEFAULT_PARSERS[Mime[:jsonapi]] = PARSER
::ActionDispatch::ParamsParser::DEFAULT_PARSERS[Mime[:jsonapi]] = JSONAPI::Rails.config.parameter_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. [116/80]

@@ -23,9 +22,9 @@ class Railtie < ::Rails::Railtie
Mime::Type.register MEDIA_TYPE, :jsonapi

if ::Rails::VERSION::MAJOR >= 5
::ActionDispatch::Request.parameter_parsers[:jsonapi] = PARSER
::ActionDispatch::Request.parameter_parsers[:jsonapi] = JSONAPI::Rails.config.parameter_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. [106/80]

}
}.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.

class Configuration < ActiveSupport::InheritableOptions; end

DEFAULT_CONFIG = {
parameter_parser: lambda { |body|

Choose a reason for hiding this comment

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

Avoid using {...} for multi-line blocks.

@@ -0,0 +1,21 @@
module JSONAPI
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.

@@ -0,0 +1,21 @@
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.

@beauby
Copy link
Member

beauby commented Dec 20, 2016

Thanks for the PR @ezekg! How do you feel about a simple boolean option to prevent registering the params parser? (e.g. JSONAPI::Rails.configure { |config| config.register_params_parser = false })

@ezekg
Copy link
Contributor Author

ezekg commented Dec 20, 2016

I actually experimented with that first, along with adding an option for toggling the registration of the jsonapi mime type in case it's being registered another way. But the library depends on both of those being defined a certain way (jsonapi mime type being under :jsonapi, instead of say an alias for the :json mime type). So in the end, both of those can't really be 'disabled' (instead, they're just defined elsewhere), so I figured exposing the definition itself was the best option.

Would you rather implement toggles instead of exposing the params parser?

@beauby
Copy link
Member

beauby commented Dec 21, 2016

@ezekg Right, so rails expects a params parser for the application/vnd.api+json mime type once it is registered (which jsonapi-rails does). The reason why I favor a boolean config to avoid declaring a params parser is that an existing app with a custom params parser for the jsonapi mime type wouldn't have to move that parser definition.

Ideally, jsonapi-rails should allow a more fine-grained configuration of what it declares in the initializers (registering params parser, registering renderer), and ensure it does not redefine the :jsonapi mime type if already defined.

@ezekg
Copy link
Contributor Author

ezekg commented Dec 21, 2016

Okay yeah, that sounds good to me. I'll modify the PR to implement it that way.

Implements a global configuration object so that we can conditionally
register things such as the renderer, parameter parser and mime type.
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]

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]

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.

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.

@ezekg
Copy link
Contributor Author

ezekg commented Dec 22, 2016

First pass on the changes we discussed. Do we want to implement any validation to ensure that the mime-type/parser is never redefined? (Or error handling in the cases where they aren't defined?) I'm now able to implement like so, which is great:

# config/initializers/jsonapi.rb

# Register JSON API mime-type
Mime::Type.register "application/vnd.api+json", :jsonapi

# Register parameter parser for JSON API mime-type to transform param keys from
# camel case to snake case
ActionDispatch::Request.parameter_parsers[:jsonapi] = -> (raw_post) {
  data = ActiveSupport::JSON.decode raw_post
  data = { _json: data } unless data.is_a? Hash
  data.deep_transform_keys! &:underscore
  data.with_indifferent_access
}

# Make sure jsonapi/rails doesn't overwrite our registrations
JSONAPI::Rails.configure do |config|
  config.register_parameter_parser = false
  config.register_mime_type = false
end

@beauby
Copy link
Member

beauby commented Dec 22, 2016

Safeguards for not overriding already registered mime type/params parser seems like a good idea (we just have to make sure that jsonapi-rb's code is ran after the user-supplied rails initializer) .
Regarding warnings for when they're not, I think it is not necessary as long as the default behavior is to register them.

@ezekg
Copy link
Contributor Author

ezekg commented Dec 22, 2016

Yeah, so right now it looks like we're hooking into the controller setup, which I believe is after user-supplied initializers are run. Is that correct? If so, we should be safe to do a little bit of validation.

@beauby
Copy link
Member

beauby commented Mar 16, 2017

hooking into the controller setup, which I believe is after user-supplied initializers are run. Is that correct?

I don't know, but I'll look into it. Other than that, this PR looks really good, apologies for taking literally months to review it 🙄

@ezekg
Copy link
Contributor Author

ezekg commented Mar 16, 2017

Haha It's cool. I've been using my fork in prod for a few months and things are good. 👍

@ezekg
Copy link
Contributor Author

ezekg commented Apr 13, 2017

Are you still wanting to move forward on this, @beauby?

@beauby
Copy link
Member

beauby commented Apr 13, 2017

@ezekg Yes, definitely. I just want to take care of ActiveModel::Errors serialization first, but this is on my radar!

@@ -0,0 +1,39 @@
require 'rails_helper'

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]

@@ -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 magic comment # frozen_string_literal: true.

@@ -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 magic comment # frozen_string_literal: true.

@beauby
Copy link
Member

beauby commented Jul 25, 2017

The time has finally come 🙏
Thanks again for this @ezekg!

@beauby beauby merged commit 0d513ca into jsonapi-rb:master Jul 25, 2017
mdeutsch pushed a commit to patientslikeme/jsonapi-rails that referenced this pull request Jul 25, 2017
* upstream/master:
  Conditionally include controller mixin (jsonapi-rb#40)
  Add initializer generator. (jsonapi-rb#39)
  Add ActiveSupport::Notifications instrumentation. (jsonapi-rb#41)
  Add library configuration object (jsonapi-rb#17)
  Rename JSONAPI::Rails::ActionController to JSONAPI::Rails::Controller. (jsonapi-rb#38)
  Add hook for default jsonapi object. (jsonapi-rb#37)
  Add hook for default exposures. (jsonapi-rb#36)
  Add hook for automatic pagination links. (jsonapi-rb#35)
  Upgrade to new jsonapi-deserializable version. (jsonapi-rb#34)
  Prepare for v0.2.1 release
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants