Skip to content

Commit

Permalink
[Fix rubocop#300] Fix Rails/RenderInline error on variable key in r…
Browse files Browse the repository at this point in the history
…ender options

Closes: rubocop#300
  • Loading branch information
tejasbubane committed Jul 24, 2020
1 parent 2c6e0da commit 9a47d83
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,7 @@
### Bug fixes

* [#297](https://github.com/rubocop-hq/rubocop-rails/pull/297): Handle an upstream Ruby issue where the DidYouMean module is not available, which would break the `Rails/UnknownEnv` cop. ([@taylorthurlow][])
* [#300](https://github.com/rubocop-hq/rubocop-rails/issues/300): Fix `Rails/RenderInline` error on variable key in render options. ([@tejasbubane][])

### Changes

Expand Down
14 changes: 4 additions & 10 deletions lib/rubocop/cop/rails/render_inline.rb
Expand Up @@ -27,21 +27,15 @@ module Rails
class RenderInline < Cop
MSG = 'Prefer using a template over inline rendering.'

def_node_matcher :render_with_options?, <<~PATTERN
(send nil? :render $(hash ...))
def_node_matcher :render_with_inline_option?, <<~PATTERN
(send nil? :render (hash <(pair {(sym :inline) (str "inline")} _) ...>))
PATTERN

def on_send(node)
render_with_options?(node) do |options|
add_offense(node) if includes_inline_key?(options)
render_with_inline_option?(node) do
add_offense(node)
end
end

private

def includes_inline_key?(node)
node.keys.find { |key| key.value.to_sym == :inline }
end
end
end
end
Expand Down
13 changes: 13 additions & 0 deletions spec/rubocop/cop/rails/render_inline_spec.rb
Expand Up @@ -22,4 +22,17 @@
render :index
RUBY
end

it 'does not register an offense when passing other options to render' do
expect_no_offenses(<<~RUBY)
render json: users, serializer: UserSerializer
RUBY
end

it 'does not register an offense when passing other options where key is a variable' do
expect_no_offenses(<<~RUBY)
serializer = users.respond_to?(:each) ? :each_serializer : :serializer
render json: users, serializer => UserSerializer
RUBY
end
end

0 comments on commit 9a47d83

Please sign in to comment.