Skip to content

Commit

Permalink
[Fix rubocop#841] Fix an error for Rails/ActionOrder
Browse files Browse the repository at this point in the history
Fixes rubocop#841.

This PR fixes an error for `Rails/ActionOrder`
when using unconventional order of multiple actions.

With this change, autocorrection blank line positions are not as expected,
so they are left to autocorrection of `Layout/EmptyLineBetweenDefs` and
`Layout/EmptyLinesAroundClassBody` cops.

This PR aims to fix the error, that it may be a cleaner autocorrection later.
  • Loading branch information
koic committed Nov 8, 2022
1 parent 62ad3ad commit ab0e985
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 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][])
8 changes: 6 additions & 2 deletions lib/rubocop/cop/rails/action_order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,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 +105,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 ab0e985

Please sign in to comment.