Skip to content

Commit

Permalink
Allows to set a global ParamBuilder (#1833)
Browse files Browse the repository at this point in the history
  • Loading branch information
myxoh authored and dblock committed Dec 10, 2018
1 parent 669221b commit 766cdb5
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#### Features

* Your contribution here.
* [#1833](https://github.com/ruby-grape/grape/pull/1833): Allows to set the `ParamBuilder` globally - [@myxoh](https://github.com/myxoh).

#### Fixes

Expand Down
30 changes: 28 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -541,6 +542,29 @@ end

[grape-swagger]: https://github.com/ruby-grape/grape-swagger

## Configuration

Use `Grape.configure` to set up global settings at load time.
Currently the configurable settings are:

* `param_builder`: Sets the [Parameter Builder](#parameters), defaults to `Grape::Extensions::ActiveSupport::HashWithIndifferentAccess::ParamBuilder`.

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`
Expand Down Expand Up @@ -618,6 +642,8 @@ params do
end
```

Or globally with the [Configuration](#configuration) `Grape.configure.param_builder`.

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`.
Expand Down
2 changes: 1 addition & 1 deletion lib/grape.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ module Grape
autoload :Namespace

autoload :Path

autoload :Cookies
autoload :Validations
autoload :ErrorFormatter
Expand Down Expand Up @@ -194,6 +193,7 @@ module ServeFile
end
end

require 'grape/config'
require 'grape/util/content_types'

require 'grape/validations/validators/base'
Expand Down
32 changes: 32 additions & 0 deletions lib/grape/config.rb
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
2 changes: 1 addition & 1 deletion lib/grape/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Request < Rack::Request
alias rack_params params

def initialize(env, options = {})
extend options[:build_params_with] || Grape::Extensions::ActiveSupport::HashWithIndifferentAccess::ParamBuilder
extend options[:build_params_with] || Grape.config.param_builder
super(env)
end

Expand Down
17 changes: 17 additions & 0 deletions spec/grape/config_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'spec_helper'

describe '.configure' do
before do
Grape.configure do |config|
config.param_builder = 42
end
end

after do
Grape.config.reset
end

it 'is configured to the new value' do
expect(Grape.config.param_builder).to eq 42
end
end
24 changes: 24 additions & 0 deletions spec/grape/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,30 @@ module Grape
end
end

describe 'when the param_builder is set to Hashie' do
before do
Grape.configure do |config|
config.param_builder = Grape::Extensions::Hashie::Mash::ParamBuilder
end
end

after do
Grape.config.reset
end

subject(:request_params) { Grape::Request.new(env, opts).params }

context 'when the API does not include a specific param builder' do
let(:opts) { {} }
it { is_expected.to be_a(Hashie::Mash) }
end

context 'when the API includes a specific param builder' do
let(:opts) { { build_params_with: Grape::Extensions::Hash::ParamBuilder } }
it { is_expected.to be_a(Hash) }
end
end

describe '#headers' do
let(:options) do
default_options.merge(request_headers)
Expand Down

0 comments on commit 766cdb5

Please sign in to comment.