Skip to content

Commit 655967a

Browse files
committed
Accept actions defined via alias in Rails/LexicallyScopedActionFilter
1 parent 0449e6d commit 655967a

File tree

3 files changed

+53
-7
lines changed

3 files changed

+53
-7
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#263](https://github.com/rubocop/rubocop-rails/pull/263): Accept actions defined via `alias` in `Rails/LexicallyScopedActionFilter`. ([@fatkodima][])

lib/rubocop/cop/rails/lexically_scoped_action_filter.rb

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,19 +144,29 @@ def defined_action_methods(block)
144144
end
145145

146146
def aliased_action_methods(node, defined_methods)
147-
alias_methods = node.each_child_node(:send).select { |send_node| send_node.method?(:alias_method) }
148-
149-
hash_of_alias_methods = alias_methods.each_with_object({}) do |alias_method, result|
150-
result[alias_method.last_argument.value] = alias_method.first_argument.value
151-
end
152-
147+
alias_methods = alias_methods(node)
153148
defined_methods.each_with_object([]) do |defined_method, aliased_method|
154-
if (new_method_name = hash_of_alias_methods[defined_method])
149+
if (new_method_name = alias_methods[defined_method])
155150
aliased_method << new_method_name
156151
end
157152
end
158153
end
159154

155+
def alias_methods(node)
156+
result = {}
157+
node.each_child_node(:send, :alias) do |child_node|
158+
case child_node.type
159+
when :send
160+
if child_node.method?(:alias_method)
161+
result[child_node.last_argument.value] = child_node.first_argument.value
162+
end
163+
when :alias
164+
result[child_node.old_identifier.value] = child_node.new_identifier.value
165+
end
166+
end
167+
result
168+
end
169+
160170
# @param node [RuboCop::AST::Node]
161171
# @return [Array<Symbol>]
162172
def array_values(node) # rubocop:disable Metrics/MethodLength

spec/rubocop/cop/rails/lexically_scoped_action_filter_spec.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,41 @@ def authorize!
150150
RUBY
151151
end
152152

153+
it 'does not register an offense when action method is aliased by `alias`' do
154+
expect_no_offenses(<<~RUBY)
155+
class FooController < ApplicationController
156+
before_action :authorize!, only: %i[index show]
157+
158+
def index
159+
end
160+
alias show index
161+
162+
private
163+
164+
def authorize!
165+
end
166+
end
167+
RUBY
168+
end
169+
170+
it 'registers an offense when action method is not aliased by `alias`' do
171+
expect_offense(<<~RUBY)
172+
class FooController < ApplicationController
173+
before_action :authorize!, only: %i[foo show]
174+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `foo` is not explicitly defined on the class.
175+
176+
def index
177+
end
178+
alias show index
179+
180+
private
181+
182+
def authorize!
183+
end
184+
end
185+
RUBY
186+
end
187+
153188
it "doesn't register an offense when using conditional statements" do
154189
expect_no_offenses <<~RUBY
155190
class Test < ActionController

0 commit comments

Comments
 (0)