Skip to content

Commit 45abe0d

Browse files
authored
Deprecates Grape's Extensions for ParamsBuilder in favor of build_with (#2540)
* Deprecates Grape's Extensions for ParamsBuilder in favor of build_with Adds params_builder.rb registration process Update README.md Update UPGRADING.md Add exceptions query_parsing.rb and unknown_params_builder.rb * Fix hashie specs * Fix cops * Remove query_parsing part * Fix has has * Revert Grape.config.param_builder * Add CHANGELOG.md * Fixes
1 parent 0f57e01 commit 45abe0d

26 files changed

+496
-260
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* [#2535](https://github.com/ruby-grape/grape/pull/2535): Delegates calls to inner objects - [@ericproulx](https://github.com/ericproulx).
77
* [#2537](https://github.com/ruby-grape/grape/pull/2537): Use activesupport `try` pattern - [@ericproulx](https://github.com/ericproulx).
88
* [#2536](https://github.com/ruby-grape/grape/pull/2536): Update normalize_path like Rails - [@ericproulx](https://github.com/ericproulx).
9+
* [#2540](https://github.com/ruby-grape/grape/pull/2540): Introduce Params builder with symbolized short name - [@ericproulx](https://github.com/ericproulx).
910
* Your contribution here.
1011

1112
#### Fixes

README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -719,10 +719,13 @@ For example, for the `param_builder`, the following code could run in an initial
719719

720720
```ruby
721721
Grape.configure do |config|
722-
config.param_builder = Grape::Extensions::Hashie::Mash::ParamBuilder
722+
config.param_builder = :hashie_mash
723723
end
724724
```
725725

726+
Available parameter builders are `:hash`, `:hash_with_indifferent_access`, and `:hashie_mash`.
727+
See [params_builder](lib/grape/params_builder).
728+
726729
You can also configure a single API:
727730

728731
```ruby
@@ -789,7 +792,7 @@ By default parameters are available as `ActiveSupport::HashWithIndifferentAccess
789792

790793
```ruby
791794
class API < Grape::API
792-
include Grape::Extensions::Hashie::Mash::ParamBuilder
795+
build_with :hashie_mash
793796

794797
params do
795798
optional :color, type: String
@@ -803,16 +806,15 @@ The class can also be overridden on individual parameter blocks using `build_wit
803806

804807
```ruby
805808
params do
806-
build_with Grape::Extensions::Hash::ParamBuilder
809+
build_with :hash
807810
optional :color, type: String
808811
end
809812
```
810813

811-
Or globally with the [Configuration](#configuration) `Grape.configure.param_builder`.
812-
813814
In the example above, `params["color"]` will return `nil` since `params` is a plain `Hash`.
814815

815-
Available parameter builders are `Grape::Extensions::Hash::ParamBuilder`, `Grape::Extensions::ActiveSupport::HashWithIndifferentAccess::ParamBuilder` and `Grape::Extensions::Hashie::Mash::ParamBuilder`.
816+
Available parameter builders are `:hash`, `:hash_with_indifferent_access`, and `:hashie_mash`.
817+
See [params_builder](lib/grape/params_builder).
816818

817819
### Declared
818820

UPGRADING.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
Upgrading Grape
22
===============
33

4+
#### Params Builder
5+
6+
- Passing a class to `build_with` or `Grape.config.param_builder` has been deprecated in favor of a symbolized short_name. See `SHORTNAME_LOOKUP` in [params_builder](lib/grape/params_builder.rb).
7+
- Including Grape's extensions like `Grape::Extensions::Hashie::Mash::ParamBuilder` has been deprecated in favor of using `build_with` at the route level.
8+
49
### Upgrading to >= 2.4.0
510

611
#### Custom Validators

lib/grape.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def self.deprecator
6464
end
6565

6666
configure do |config|
67-
config.param_builder = Grape::Extensions::ActiveSupport::HashWithIndifferentAccess::ParamBuilder
67+
config.param_builder = :hash_with_indifferent_access
6868
config.compile_methods!
6969
end
7070
end

lib/grape/dsl/parameters.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ module Parameters
2323
# class API < Grape::API
2424
# desc "Get collection"
2525
# params do
26-
# build_with Grape::Extensions::Hashie::Mash::ParamBuilder
26+
# build_with :hashie_mash
2727
# requires :user_id, type: Integer
2828
# end
2929
# get do
3030
# params['user_id']
3131
# end
3232
# end
33-
def build_with(build_with = nil)
33+
def build_with(build_with)
3434
@api.namespace_inheritable(:build_params_with, build_with)
3535
end
3636

lib/grape/dsl/routing.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ def scope(_name = nil, &block)
6767
end
6868
end
6969

70+
def build_with(build_with)
71+
namespace_inheritable(:build_params_with, build_with)
72+
end
73+
7074
# Do not route HEAD requests to GET requests automatically.
7175
def do_not_route_head!
7276
namespace_inheritable(:do_not_route_head, true)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# frozen_string_literal: true
2+
3+
module Grape
4+
module Exceptions
5+
class UnknownParamsBuilder < Base
6+
def initialize(params_builder_type)
7+
super(message: compose_message(:unknown_params_builder, params_builder_type: params_builder_type))
8+
end
9+
end
10+
end
11+
end

lib/grape/extensions/active_support/hash_with_indifferent_access.rb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,8 @@ module ParamBuilder
88
extend ::ActiveSupport::Concern
99

1010
included do
11-
namespace_inheritable(:build_params_with, Grape::Extensions::ActiveSupport::HashWithIndifferentAccess::ParamBuilder)
12-
end
13-
14-
def params_builder
15-
Grape::Extensions::ActiveSupport::HashWithIndifferentAccess::ParamBuilder
11+
Grape.deprecator.warn 'This concern has been deprecated. Use `build_with` with one of the following short_name (:hash, :hash_with_indifferent_access, :hashie_mash) instead.'
12+
namespace_inheritable(:build_params_with, :hash_with_indifferent_access)
1613
end
1714

1815
def build_params

lib/grape/extensions/hash.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ module ParamBuilder
77
extend ::ActiveSupport::Concern
88

99
included do
10-
namespace_inheritable(:build_params_with, Grape::Extensions::Hash::ParamBuilder)
10+
Grape.deprecator.warn 'This concern has been deprecated. Use `build_with` with one of the following short_name (:hash, :hash_with_indifferent_access, :hashie_mash) instead.'
11+
namespace_inheritable(:build_params_with, :hash)
1112
end
1213

1314
def build_params

lib/grape/extensions/hashie/mash.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@ module Hashie
66
module Mash
77
module ParamBuilder
88
extend ::ActiveSupport::Concern
9-
included do
10-
namespace_inheritable(:build_params_with, Grape::Extensions::Hashie::Mash::ParamBuilder)
11-
end
129

13-
def params_builder
14-
Grape::Extensions::Hashie::Mash::ParamBuilder
10+
included do
11+
Grape.deprecator.warn 'This concern has been deprecated. Use `build_with` with one of the following short_name (:hash, :hash_with_indifferent_access, :hashie_mash) instead.'
12+
namespace_inheritable(:build_params_with, :hashie_mash)
1513
end
1614

1715
def build_params

0 commit comments

Comments
 (0)