Skip to content
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

Replace Grape::Config by ActiveSupport::Configurable #2332

Merged
merged 5 commits into from
May 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 10 additions & 20 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config --auto-gen-only-exclude --exclude-limit 5000`
# on 2023-04-23 13:05:19 UTC using RuboCop version 1.50.2.
# on 2023-05-27 20:47:15 UTC using RuboCop version 1.50.2.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand Down Expand Up @@ -52,18 +52,16 @@ Lint/ConstantDefinitionInBlock:
- 'spec/grape/validations/validators/presence_spec.rb'
- 'spec/grape/validations/validators/values_spec.rb'

# Offense count: 5
# Offense count: 3
# Configuration parameters: IgnoreLiteralBranches, IgnoreConstantBranches.
Lint/DuplicateBranch:
Exclude:
- 'lib/grape/extensions/deep_symbolize_hash.rb'
- 'spec/support/versioned_helpers.rb'

# Offense count: 71
# Configuration parameters: AllowComments, AllowEmptyLambdas.
Lint/EmptyBlock:
Exclude:
- 'spec/grape/api/custom_validations_spec.rb'
- 'spec/grape/api/recognize_path_spec.rb'
- 'spec/grape/api/required_parameters_with_invalid_method_spec.rb'
- 'spec/grape/api_spec.rb'
Expand Down Expand Up @@ -102,13 +100,12 @@ Lint/MissingSuper:
- 'lib/grape/router/pattern.rb'
- 'lib/grape/validations/validators/base.rb'

# Offense count: 3
# Offense count: 2
# Configuration parameters: EnforcedStyleForLeadingUnderscores.
# SupportedStylesForLeadingUnderscores: disallowed, required, optional
Naming/MemoizedInstanceVariableName:
Exclude:
- 'lib/grape/api/instance.rb'
- 'lib/grape/config.rb'
- 'lib/grape/middleware/base.rb'

# Offense count: 5
Expand Down Expand Up @@ -137,12 +134,11 @@ RSpec/AnyInstance:
- 'spec/grape/api_spec.rb'
- 'spec/grape/middleware/base_spec.rb'

# Offense count: 347
# Offense count: 342
# Configuration parameters: Prefixes, AllowedPatterns.
# Prefixes: when, with, without
RSpec/ContextWording:
Exclude:
- 'spec/grape/api/custom_validations_spec.rb'
- 'spec/grape/api/defines_boolean_in_params_spec.rb'
- 'spec/grape/api/documentation_spec.rb'
- 'spec/grape/api/inherited_helpers_spec.rb'
Expand Down Expand Up @@ -195,7 +191,7 @@ RSpec/ContextWording:
- 'spec/grape/validations_spec.rb'
- 'spec/shared/versioning_examples.rb'

# Offense count: 3
# Offense count: 2
# Configuration parameters: IgnoredMetadata.
RSpec/DescribeClass:
Exclude:
Expand All @@ -204,7 +200,6 @@ RSpec/DescribeClass:
- '**/spec/routing/**/*'
- '**/spec/system/**/*'
- '**/spec/views/**/*'
- 'spec/grape/config_spec.rb'
- 'spec/grape/named_api_spec.rb'
- 'spec/grape/validations/instance_behaivour_spec.rb'

Expand Down Expand Up @@ -329,7 +324,7 @@ RSpec/MessageChain:
Exclude:
- 'spec/grape/middleware/formatter_spec.rb'

# Offense count: 138
# Offense count: 136
# Configuration parameters: .
# SupportedStyles: have_received, receive
RSpec/MessageSpies:
Expand All @@ -340,7 +335,7 @@ RSpec/MissingExampleGroupArgument:
Exclude:
- 'spec/grape/middleware/exception_spec.rb'

# Offense count: 766
# Offense count: 765
# Configuration parameters: Max.
RSpec/MultipleExpectations:
Exclude:
Expand Down Expand Up @@ -371,8 +366,6 @@ RSpec/MultipleExpectations:
- 'spec/grape/entity_spec.rb'
- 'spec/grape/exceptions/body_parse_errors_spec.rb'
- 'spec/grape/exceptions/invalid_accept_header_spec.rb'
- 'spec/grape/exceptions/missing_group_type_spec.rb'
- 'spec/grape/exceptions/unsupported_group_type_spec.rb'
- 'spec/grape/exceptions/validation_errors_spec.rb'
- 'spec/grape/extensions/param_builders/hash_spec.rb'
- 'spec/grape/extensions/param_builders/hash_with_indifferent_access_spec.rb'
Expand Down Expand Up @@ -420,7 +413,7 @@ RSpec/MultipleMemoizedHelpers:
- 'spec/grape/request_spec.rb'
- 'spec/grape/validations/attributes_doc_spec.rb'

# Offense count: 2147
# Offense count: 2137
# Configuration parameters: EnforcedStyle, IgnoreSharedExamples.
# SupportedStyles: always, named_only
RSpec/NamedSubject:
Expand Down Expand Up @@ -454,8 +447,6 @@ RSpec/NamedSubject:
- 'spec/grape/exceptions/base_spec.rb'
- 'spec/grape/exceptions/body_parse_errors_spec.rb'
- 'spec/grape/exceptions/invalid_accept_header_spec.rb'
- 'spec/grape/exceptions/missing_group_type_spec.rb'
- 'spec/grape/exceptions/unsupported_group_type_spec.rb'
- 'spec/grape/exceptions/validation_errors_spec.rb'
- 'spec/grape/extensions/param_builders/hash_spec.rb'
- 'spec/grape/extensions/param_builders/hash_with_indifferent_access_spec.rb'
Expand Down Expand Up @@ -487,7 +478,7 @@ RSpec/NamedSubject:
- 'spec/grape/validations/validators/presence_spec.rb'
- 'spec/grape/validations_spec.rb'

# Offense count: 171
# Offense count: 174
# Configuration parameters: Max, AllowedGroups.
RSpec/NestedGroups:
Exclude:
Expand Down Expand Up @@ -568,12 +559,11 @@ RSpec/StubbedMock:
- 'spec/grape/middleware/formatter_spec.rb'
- 'spec/grape/parser_spec.rb'

# Offense count: 122
# Offense count: 114
RSpec/SubjectStub:
Exclude:
- 'spec/grape/api_spec.rb'
- 'spec/grape/dsl/callbacks_spec.rb'
- 'spec/grape/dsl/desc_spec.rb'
- 'spec/grape/dsl/helpers_spec.rb'
- 'spec/grape/dsl/inside_route_spec.rb'
- 'spec/grape/dsl/middleware_spec.rb'
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* [#2327](https://github.com/ruby-grape/grape/pull/2327): Use ActiveSupport deprecation - [@ericproulx](https://github.com/ericproulx).
* [#2330](https://github.com/ruby-grape/grape/pull/2330): Use ActiveSupport inflector - [@ericproulx](https://github.com/ericproulx).
* [#2331](https://github.com/ruby-grape/grape/pull/2331): Memory optimization when running validators - [@ericproulx](https://github.com/ericproulx).
* [#2332](https://github.com/ruby-grape/grape/pull/2332): Use ActiveSupport configurable - [@ericproulx](https://github.com/ericproulx).
* Your contribution here.

#### Fixes
Expand Down
8 changes: 7 additions & 1 deletion lib/grape.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
require 'date'
require 'active_support'
require 'active_support/concern'
require 'active_support/configurable'
require 'active_support/version'
require 'active_support/isolated_execution_state' if ActiveSupport::VERSION::MAJOR > 6
require 'active_support/core_ext/array/conversions'
Expand All @@ -34,6 +35,7 @@
I18n.load_path << File.expand_path('grape/locale/en.yml', __dir__)

module Grape
include ActiveSupport::Configurable
extend ::ActiveSupport::Autoload

eager_autoload do
Expand Down Expand Up @@ -287,9 +289,13 @@ module Types
autoload :InvalidValue
end
end

configure do |config|
config.param_builder = Grape::Extensions::ActiveSupport::HashWithIndifferentAccess::ParamBuilder
config.compile_methods!
end
end

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

require 'grape/util/lazy_value'
Expand Down
34 changes: 0 additions & 34 deletions lib/grape/config.rb

This file was deleted.

17 changes: 0 additions & 17 deletions spec/grape/config_spec.rb

This file was deleted.

9 changes: 9 additions & 0 deletions spec/grape/grape_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

RSpec.describe Grape do
describe '.config' do
subject { described_class.config }

it { is_expected.to eq(param_builder: Grape::Extensions::ActiveSupport::HashWithIndifferentAccess::ParamBuilder) }
end
end
18 changes: 4 additions & 14 deletions spec/grape/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,29 +62,19 @@ module Grape
end
end

describe 'when the param_builder is set to Hashie' do
describe 'when the build_params_with is set to Hashie' do
subject(:request_params) { described_class.new(env, **opts).params }

before do
Grape.configure do |config|
config.param_builder = Grape::Extensions::Hashie::Mash::ParamBuilder
end
end

after do
Grape.config.reset
end

context 'when the API does not include a specific param builder' do
let(:opts) { {} }

it { is_expected.to be_a(Hashie::Mash) }
it { is_expected.to be_a(Hash) }
end

context 'when the API includes a specific param builder' do
let(:opts) { { build_params_with: Grape::Extensions::Hash::ParamBuilder } }
let(:opts) { { build_params_with: Grape::Extensions::Hashie::Mash::ParamBuilder } }

it { is_expected.to be_a(Hash) }
it { is_expected.to be_a(Hashie::Mash) }
end
end

Expand Down