Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #841] Fix an error for Rails/ActionOrder #859

Merged
merged 1 commit into from
Nov 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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