Skip to content

Commit

Permalink
Merge pull request #859 from koic/fix_an_error_for_rails_action_order
Browse files Browse the repository at this point in the history
[Fix #841] Fix an error for `Rails/ActionOrder`
  • Loading branch information
koic authored Nov 11, 2022
2 parents 62ad3ad + 3ad87ce commit 2cf5aea
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
1 change: 1 addition & 0 deletions changelog/fix_an_error_for_rails_action_order.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#841](https://github.com/rubocop/rubocop-rails/issues/841): Fix an error for `Rails/ActionOrder` when using unconventional order of multiple actions. ([@koic][])
11 changes: 8 additions & 3 deletions lib/rubocop/cop/rails/action_order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ module Rails
# Enforces consistent ordering of the standard Rails RESTful controller actions.
#
# The cop is configurable and can enforce any ordering of the standard actions.
# All other methods are ignored.
# All other methods are ignored. So, the actions specified in `ExpectedOrder` should be
# defined before actions not specified.
#
# [source,yaml]
# ----
Expand Down Expand Up @@ -75,8 +76,7 @@ def register_offense(previous, current)
current = correction_target(current)
previous = correction_target(previous)

corrector.replace(current, previous.source)
corrector.replace(previous, current.source)
swap_range(corrector, current, previous)
end
end

Expand Down Expand Up @@ -106,6 +106,11 @@ def range_with_comments(node)
def range_with_comments_and_lines(node)
range_by_whole_lines(range_with_comments(node), include_final_newline: true)
end

def swap_range(corrector, range1, range2)
corrector.insert_before(range2, range1.source)
corrector.remove(range1)
end
end
end
end
Expand Down
28 changes: 24 additions & 4 deletions spec/rubocop/cop/rails/action_order_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,26 @@ def show; end
RUBY
end

it 'detects unconventional order of multiple actions' do
expect_offense(<<~RUBY)
class UserController < ApplicationController
def create; end
def edit; end
^^^^^^^^^^^^^ Action `edit` should appear before `create`.
def show; end
^^^^^^^^^^^^^ Action `show` should appear before `edit`.
end
RUBY

expect_correction(<<~RUBY)
class UserController < ApplicationController
def show; end
def edit; end
def create; end
end
RUBY
end

it 'supports methods with content' do
expect_offense(<<~RUBY)
class UserController < ApplicationController
Expand All @@ -33,10 +53,10 @@ def index; end
expect_correction(<<~RUBY)
class UserController < ApplicationController
def index; end
def show
@user = User.find(params[:id])
end
end
RUBY
end
Expand Down Expand Up @@ -137,11 +157,11 @@ class TestController < BaseController
def index
end
end
unless Rails.env.development?
def edit
end
end
end
RUBY
end
Expand Down Expand Up @@ -181,8 +201,8 @@ def show; end
expect_correction(<<~RUBY)
class UserController < ApplicationController
def show; end
def edit; end
def index; end
def edit; end
end
RUBY
end
Expand All @@ -205,9 +225,9 @@ def index; end
class UserController < ApplicationController
# index
def index; end
# show
def show; end
end
RUBY
end
Expand Down

0 comments on commit 2cf5aea

Please sign in to comment.