Skip to content

Commit da31f80

Browse files
committed
Fix attribute translator setter
This was simply not working before.
1 parent bc1d790 commit da31f80

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#### Fixes
1414

15+
* [#2375](https://github.com/ruby-grape/grape/pull/2375): Fix setter methods for `Grape::Router::AttributeTranslator` - [@Jell](https://github.com/Jell).
1516
* [#2370](https://github.com/ruby-grape/grape/pull/2370): Remove route_xyz method_missing deprecation - [@ericproulx](https://github.com/ericproulx).
1617
* [#2372](https://github.com/ruby-grape/grape/pull/2372): Fix `declared` method for hash params with overlapping names - [@jcagarcia](https://github.com/jcagarcia).
1718
* [#2373](https://github.com/ruby-grape/grape/pull/2373): Fix markdown files for following 1-line format - [@jcagarcia](https://github.com/jcagarcia).

lib/grape/router/attribute_translator.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ def to_h
3838
end
3939

4040
def method_missing(method_name, *args)
41-
if setter?(method_name[-1])
42-
attributes[method_name[0..]] = *args
41+
if setter?(method_name)
42+
attributes[method_name.to_s.chomp('=').to_sym] = args.first
4343
else
4444
attributes[method_name]
4545
end
4646
end
4747

4848
def respond_to_missing?(method_name, _include_private = false)
49-
if setter?(method_name[-1])
49+
if setter?(method_name)
5050
true
5151
else
5252
@attributes.key?(method_name)
@@ -56,7 +56,7 @@ def respond_to_missing?(method_name, _include_private = false)
5656
private
5757

5858
def setter?(method_name)
59-
method_name[-1] == '='
59+
method_name.end_with?('=')
6060
end
6161
end
6262
end
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# frozen_string_literal: true
2+
3+
describe Grape::Router::AttributeTranslator do
4+
(Grape::Router::AttributeTranslator::ROUTE_ATTRIBUTES + Grape::Router::AttributeTranslator::ROUTE_ATTRIBUTES).each do |attribute|
5+
describe "##{attribute}" do
6+
it "returns value from #{attribute} key if present" do
7+
translator = described_class.new(attribute => 'value')
8+
expect(translator.public_send(attribute)).to eq('value')
9+
end
10+
11+
it "returns nil from #{attribute} key if missing" do
12+
translator = described_class.new
13+
expect(translator.public_send(attribute)).to be_nil
14+
end
15+
end
16+
17+
describe "##{attribute}=" do
18+
it "sets value for #{attribute}", :aggregate_failures do
19+
translator = described_class.new(attribute => 'value')
20+
expect do
21+
translator.public_send("#{attribute}=", 'new_value')
22+
end.to change(translator, attribute).from('value').to('new_value')
23+
end
24+
end
25+
end
26+
end

0 commit comments

Comments
 (0)