Skip to content

Commit

Permalink
Fix attribute translator setter
Browse files Browse the repository at this point in the history
This was simply not working before.
  • Loading branch information
Jell committed Dec 21, 2023
1 parent bc1d790 commit da31f80
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
8 changes: 4 additions & 4 deletions lib/grape/router/attribute_translator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
26 changes: 26 additions & 0 deletions spec/grape/router/attribute_translator_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit da31f80

Please sign in to comment.