Skip to content

Commit

Permalink
Remove deprecation msg + small refactor (#2502)
Browse files Browse the repository at this point in the history
* Remove deprecation msg + small refactor

* Add CHANGELOG.md and UPGRADING.md
  • Loading branch information
ericproulx authored Oct 1, 2024
1 parent 7f0595c commit 5b0066c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 37 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* [#2497](https://github.com/ruby-grape/grape/pull/2497): Update RuboCop to 1.66.1 - [@ericproulx](https://github.com/ericproulx).
* [#2500](https://github.com/ruby-grape/grape/pull/2500): Remove deprecated `file` method - [@ericproulx](https://github.com/ericproulx).
* [#2501](https://github.com/ruby-grape/grape/pull/2501): Remove deprecated `except` and `proc` options in values validator - [@ericproulx](https://github.com/ericproulx).
* [#2502](https://github.com/ruby-grape/grape/pull/2502): Remove deprecation `options` in `desc` - [@ericproulx](https://github.com/ericproulx).
* Your contribution here.

#### Fixes
Expand Down
3 changes: 3 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ See [#2500](https://github.com/ruby-grape/grape/pull/2500) for more information.
- The `except` and `proc` options have been removed from the `values` validator. Use `except_values` validator or assign `proc` directly to `values`.
See [#2501](https://github.com/ruby-grape/grape/pull/2501) for more information.

- `Passing an options hash and a block to 'desc'` deprecation has been removed. Move all hash options to block instead.
See [#2502](https://github.com/ruby-grape/grape/pull/2502) for more information.

### Upgrading to >= 2.2.0

### `Length` validator
Expand Down
51 changes: 27 additions & 24 deletions lib/grape/dsl/desc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,33 +70,23 @@ module Desc
# # ...
# end
#
def desc(description, options = {}, &config_block)
if config_block
endpoint_configuration = if defined?(configuration)
# When the instance is mounted - the configuration is executed on mount time
if configuration.respond_to?(:evaluate)
configuration.evaluate
# Within `given` or `mounted blocks` the configuration is already evaluated
elsif configuration.is_a?(Hash)
configuration
end
end
endpoint_configuration ||= {}
config_class = desc_container(endpoint_configuration)
def desc(description, options = nil, &config_block)
opts =
if config_block
desc_container(endpoint_configuration).then do |config_class|
config_class.configure do
description(description)
end

config_class.configure do
description description
config_class.configure(&config_block)
config_class.settings
end
else
options&.merge(description: description) || { description: description }
end

config_class.configure(&config_block)
Grape.deprecator.warn('Passing a options hash and a block to `desc` is deprecated. Move all hash options to block.') if options.any?
options = config_class.settings
else
options = options.merge(description: description)
end

namespace_setting :description, options
route_setting :description, options
namespace_setting :description, opts
route_setting :description, opts
end

# Returns an object which configures itself via an instance-context DSL.
Expand All @@ -116,6 +106,19 @@ def config_context.failure(*args)
end
end
end

private

def endpoint_configuration
return {} unless defined?(configuration)

if configuration.respond_to?(:evaluate)
configuration.evaluate
# Within `given` or `mounted blocks` the configuration is already evaluated
elsif configuration.is_a?(Hash)
configuration
end
end
end
end
end
13 changes: 0 additions & 13 deletions spec/grape/dsl/desc_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,5 @@
expect(subject.namespace_setting(:description)).to eq(expected_options)
expect(subject.route_setting(:description)).to eq(expected_options)
end

it 'can be set with options and a block' do
expect(Grape.deprecator).to receive(:warn).with('Passing a options hash and a block to `desc` is deprecated. Move all hash options to block.')

desc_text = 'The description'
detail_text = 'more details'
options = { message: 'none' }
subject.desc desc_text, options do
detail detail_text
end
expect(subject.namespace_setting(:description)).to eq(description: desc_text, detail: detail_text)
expect(subject.route_setting(:description)).to eq(description: desc_text, detail: detail_text)
end
end
end

0 comments on commit 5b0066c

Please sign in to comment.