-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Allows to set a global ParamBuilder #1833
Merged
Merged
Changes from 9 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
ea8f849
Allows to set a global ParamBuilder
myxoh 96ae225
Added change to changelog
myxoh f2378f3
Fixes readme
myxoh 8916254
Renames configuration to mount configuration where relevant
myxoh cf0a6c7
Fixes changelog
myxoh 37f949d
Changes configuration syntax to `configure`
myxoh e23f587
Loads configuration first thing
myxoh 386615f
Uses `.configure` on Grape rather than Grape::Config
myxoh 914d779
Rubocop missing issues
myxoh b64c636
[Issue-1775] configures the param builder with a new value
myxoh 7731d46
Merge branch 'master' into issue-1775
myxoh 3793e64
[Issue-1775] undoes mocking on request_spec
myxoh c84b037
Merge branch 'issue-1775' of github.com:ruby-grape/grape into issue-1775
myxoh 20833c3
Slight cosmetic improvements to Readme.md
myxoh 7047330
Merge branch 'master' into issue-1775
myxoh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,13 +22,14 @@ | |
- [Rails](#rails) | ||
- [Modules](#modules) | ||
- [Remounting](#remounting) | ||
- [Configuration](#configuration) | ||
- [Mount Configuration](#mount-configuration) | ||
- [Versioning](#versioning) | ||
- [Path](#path) | ||
- [Header](#header) | ||
- [Accept-Version Header](#accept-version-header) | ||
- [Param](#param) | ||
- [Describing Methods](#describing-methods) | ||
- [Configuration](#configuration) | ||
- [Parameters](#parameters) | ||
- [Params Class](#params-class) | ||
- [Declared](#declared) | ||
|
@@ -395,7 +396,7 @@ end | |
|
||
Assuming that the post and comment endpoints are mounted in `/posts` and `/comments`, you should now be able to do `get /posts/votes`, `post /posts/votes`, `get /comments/votes`. | ||
|
||
### Configuration | ||
### Mount Configuration | ||
|
||
You can configure remountable endpoints for small details changing according to where they are mounted. | ||
|
||
|
@@ -541,6 +542,29 @@ end | |
|
||
[grape-swagger]: https://github.com/ruby-grape/grape-swagger | ||
|
||
## Configuration | ||
|
||
Grape counts with a module `Grape.configure` for some basic configuration at load time. | ||
Currently the configurable settings are: | ||
|
||
* `param_builder`: Sets the default [Parameter Builder](#parameters), by default: `Grape::Extensions::ActiveSupport::HashWithIndifferentAccess::ParamBuilder` | ||
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. Sets the Parameter Builder, defaults to |
||
|
||
To change a setting value make sure that at some point on load time the code the following code runs | ||
|
||
```ruby | ||
Grape.configure do |config| | ||
config.setting = value | ||
end | ||
``` | ||
|
||
For example, for the `param_builder`, the following code could run in an initializers: | ||
|
||
```ruby | ||
Grape.configure do |config| | ||
config.param_builder = Grape::Extensions::Hashie::Mash::ParamBuilder | ||
end | ||
``` | ||
|
||
## Parameters | ||
|
||
Request parameters are available through the `params` hash object. This includes `GET`, `POST` | ||
|
@@ -618,6 +642,8 @@ params do | |
end | ||
``` | ||
|
||
Or globally with the [Configuration](#configuration) `Grape.configure.param_builder` | ||
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 a period. |
||
|
||
In the example above, `params["color"]` will return `nil` since `params` is a plain `Hash`. | ||
|
||
Available parameter builders are `Grape::Extensions::Hash::ParamBuilder`, `Grape::Extensions::ActiveSupport::HashWithIndifferentAccess::ParamBuilder` and `Grape::Extensions::Hashie::Mash::ParamBuilder`. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
module Grape | ||
module Config | ||
class Configuration | ||
ATTRIBUTES = %i[ | ||
param_builder | ||
].freeze | ||
|
||
attr_accessor(*ATTRIBUTES) | ||
|
||
def initialize | ||
reset | ||
end | ||
|
||
def reset | ||
self.param_builder = Grape::Extensions::ActiveSupport::HashWithIndifferentAccess::ParamBuilder | ||
end | ||
end | ||
|
||
def self.extended(base) | ||
def base.configure | ||
block_given? ? yield(config) : config | ||
end | ||
|
||
def base.config | ||
@configuration ||= Grape::Config::Configuration.new | ||
end | ||
end | ||
end | ||
end | ||
|
||
Grape.extend Grape::Config | ||
Grape.config.reset |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
require 'spec_helper' | ||
|
||
describe '.configure' do | ||
let!(:grape_double) { Module.new { extend Grape::Config } } | ||
myxoh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
context 'when not configured' do | ||
it 'does not change when resetted' do | ||
expect { grape_double.config.reset } | ||
.not_to(change { grape_double.config.param_builder }) | ||
end | ||
end | ||
|
||
context 'when configured' do | ||
subject(:configure) do | ||
grape_double.configure do |config| | ||
config.param_builder = 42 | ||
end | ||
end | ||
|
||
it 'changes the value' do | ||
expect { configure }.to change { grape_double.config.param_builder }.to(42) | ||
end | ||
|
||
it 'can be restored by resetting' do | ||
configure | ||
expect { grape_double.config.reset } | ||
.to change { grape_double.config.param_builder } | ||
.from(42) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Counts is probably the wrong word. I tend to keep this as short as possible. For example:
Use
Grape.configure
to setup global settings at load time.