diff --git a/CHANGELOG.md b/CHANGELOG.md index 458852b3bd..3948d65107 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ #### Features * Your contribution here. +* [#1940](https://github.com/ruby-grape/grape/pull/1940): Get rid of a needless step in HashWithIndifferentAccess - [@dnesteryuk](https://github.com/dnesteryuk). * [#1938](https://github.com/ruby-grape/grape/pull/1938): Add project metadata to the gemspec - [@orien](https://github.com/orien). * [#1920](https://github.com/ruby-grape/grape/pull/1920): Replace Virtus with dry-types - [@dnesteryuk](https://github.com/dnesteryuk). diff --git a/lib/grape/dsl/inside_route.rb b/lib/grape/dsl/inside_route.rb index 2953d590df..0fc5948ada 100644 --- a/lib/grape/dsl/inside_route.rb +++ b/lib/grape/dsl/inside_route.rb @@ -84,7 +84,8 @@ def should_be_empty_array?(declared_param, passed_children_params) end def declared_param_is_array?(declared_param) - route_options_params[declared_param.to_s] && route_options_params[declared_param.to_s][:type] == 'Array' + key = declared_param.to_s + route_options_params[key] && route_options_params[key][:type] == 'Array' end def route_options_params diff --git a/lib/grape/endpoint.rb b/lib/grape/endpoint.rb index 0551ef88d2..336db40b1e 100644 --- a/lib/grape/endpoint.rb +++ b/lib/grape/endpoint.rb @@ -154,7 +154,7 @@ def mount_in(router) methods << Grape::Http::Headers::HEAD end methods.each do |method| - unless route.request_method.to_s.upcase == method + unless route.request_method == method route = Grape::Router::Route.new(method, route.origin, route.attributes.to_h) end router.append(route.apply(self)) diff --git a/lib/grape/extensions/active_support/hash_with_indifferent_access.rb b/lib/grape/extensions/active_support/hash_with_indifferent_access.rb index c6bf2ca1aa..105e236f54 100644 --- a/lib/grape/extensions/active_support/hash_with_indifferent_access.rb +++ b/lib/grape/extensions/active_support/hash_with_indifferent_access.rb @@ -14,10 +14,9 @@ def params_builder end def build_params - params = ::ActiveSupport::HashWithIndifferentAccess[rack_params] + params = ::ActiveSupport::HashWithIndifferentAccess.new(rack_params) params.deep_merge!(grape_routing_args) if env[Grape::Env::GRAPE_ROUTING_ARGS] - # TODO: remove, in Rails 4 or later ::ActiveSupport::HashWithIndifferentAccess converts nested Hashes into indifferent access ones - DeepHashWithIndifferentAccess.deep_hash_with_indifferent_access(params) + params end end end diff --git a/lib/grape/extensions/deep_hash_with_indifferent_access.rb b/lib/grape/extensions/deep_hash_with_indifferent_access.rb deleted file mode 100644 index e18601f8d8..0000000000 --- a/lib/grape/extensions/deep_hash_with_indifferent_access.rb +++ /dev/null @@ -1,18 +0,0 @@ -module Grape - module Extensions - module DeepHashWithIndifferentAccess - def self.deep_hash_with_indifferent_access(object) - case object - when ::Hash - object.inject(::ActiveSupport::HashWithIndifferentAccess.new) do |new_hash, (key, value)| - new_hash.merge!(key => deep_hash_with_indifferent_access(value)) - end - when ::Array - object.map { |element| deep_hash_with_indifferent_access(element) } - else - object - end - end - end - end -end diff --git a/lib/grape/router/route.rb b/lib/grape/router/route.rb index 52e0098b31..0eff8607fa 100644 --- a/lib/grape/router/route.rb +++ b/lib/grape/router/route.rb @@ -62,10 +62,12 @@ def route_path end def initialize(method, pattern, **options) + upcased_method = method.to_s.upcase + @suffix = options[:suffix] - @options = options.merge(method: method.to_s.upcase) + @options = options.merge(method: upcased_method) @pattern = Pattern.new(pattern, **options) - @translator = AttributeTranslator.new(**options, request_method: method.to_s.upcase) + @translator = AttributeTranslator.new(**options, request_method: upcased_method) end def exec(env)