diff --git a/CHANGELOG.md b/CHANGELOG.md index 821d3fd3b1..1afadbf214 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ #### Fixes +* [#2375](https://github.com/ruby-grape/grape/pull/2375): Fix setter methods for `Grape::Router::AttributeTranslator` - [@Jell](https://github.com/Jell). * [#2370](https://github.com/ruby-grape/grape/pull/2370): Remove route_xyz method_missing deprecation - [@ericproulx](https://github.com/ericproulx). * [#2372](https://github.com/ruby-grape/grape/pull/2372): Fix `declared` method for hash params with overlapping names - [@jcagarcia](https://github.com/jcagarcia). * [#2373](https://github.com/ruby-grape/grape/pull/2373): Fix markdown files for following 1-line format - [@jcagarcia](https://github.com/jcagarcia). diff --git a/lib/grape/router/attribute_translator.rb b/lib/grape/router/attribute_translator.rb index 8264e2196b..e45efc53b7 100644 --- a/lib/grape/router/attribute_translator.rb +++ b/lib/grape/router/attribute_translator.rb @@ -38,15 +38,15 @@ def to_h end def method_missing(method_name, *args) - if setter?(method_name[-1]) - attributes[method_name[0..]] = *args + if setter?(method_name) + attributes[method_name.to_s.chomp('=').to_sym] = args.first else attributes[method_name] end end def respond_to_missing?(method_name, _include_private = false) - if setter?(method_name[-1]) + if setter?(method_name) true else @attributes.key?(method_name) @@ -56,7 +56,7 @@ def respond_to_missing?(method_name, _include_private = false) private def setter?(method_name) - method_name[-1] == '=' + method_name.end_with?('=') end end end diff --git a/spec/grape/router/attribute_translator_spec.rb b/spec/grape/router/attribute_translator_spec.rb new file mode 100644 index 0000000000..0254a279ed --- /dev/null +++ b/spec/grape/router/attribute_translator_spec.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +describe Grape::Router::AttributeTranslator do + (Grape::Router::AttributeTranslator::ROUTE_ATTRIBUTES + Grape::Router::AttributeTranslator::ROUTE_ATTRIBUTES).each do |attribute| + describe "##{attribute}" do + it "returns value from #{attribute} key if present" do + translator = described_class.new(attribute => 'value') + expect(translator.public_send(attribute)).to eq('value') + end + + it "returns nil from #{attribute} key if missing" do + translator = described_class.new + expect(translator.public_send(attribute)).to be_nil + end + end + + describe "##{attribute}=" do + it "sets value for #{attribute}", :aggregate_failures do + translator = described_class.new(attribute => 'value') + expect do + translator.public_send("#{attribute}=", 'new_value') + end.to change(translator, attribute).from('value').to('new_value') + end + end + end +end