Skip to content

Commit 2cf5aea

Browse files
authored
Merge pull request rubocop#859 from koic/fix_an_error_for_rails_action_order
[Fix rubocop#841] Fix an error for `Rails/ActionOrder`
2 parents 62ad3ad + 3ad87ce commit 2cf5aea

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#841](https://github.com/rubocop/rubocop-rails/issues/841): Fix an error for `Rails/ActionOrder` when using unconventional order of multiple actions. ([@koic][])

lib/rubocop/cop/rails/action_order.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ module Rails
66
# Enforces consistent ordering of the standard Rails RESTful controller actions.
77
#
88
# The cop is configurable and can enforce any ordering of the standard actions.
9-
# All other methods are ignored.
9+
# All other methods are ignored. So, the actions specified in `ExpectedOrder` should be
10+
# defined before actions not specified.
1011
#
1112
# [source,yaml]
1213
# ----
@@ -75,8 +76,7 @@ def register_offense(previous, current)
7576
current = correction_target(current)
7677
previous = correction_target(previous)
7778

78-
corrector.replace(current, previous.source)
79-
corrector.replace(previous, current.source)
79+
swap_range(corrector, current, previous)
8080
end
8181
end
8282

@@ -106,6 +106,11 @@ def range_with_comments(node)
106106
def range_with_comments_and_lines(node)
107107
range_by_whole_lines(range_with_comments(node), include_final_newline: true)
108108
end
109+
110+
def swap_range(corrector, range1, range2)
111+
corrector.insert_before(range2, range1.source)
112+
corrector.remove(range1)
113+
end
109114
end
110115
end
111116
end

spec/rubocop/cop/rails/action_order_spec.rb

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,26 @@ def show; end
1818
RUBY
1919
end
2020

21+
it 'detects unconventional order of multiple actions' do
22+
expect_offense(<<~RUBY)
23+
class UserController < ApplicationController
24+
def create; end
25+
def edit; end
26+
^^^^^^^^^^^^^ Action `edit` should appear before `create`.
27+
def show; end
28+
^^^^^^^^^^^^^ Action `show` should appear before `edit`.
29+
end
30+
RUBY
31+
32+
expect_correction(<<~RUBY)
33+
class UserController < ApplicationController
34+
def show; end
35+
def edit; end
36+
def create; end
37+
end
38+
RUBY
39+
end
40+
2141
it 'supports methods with content' do
2242
expect_offense(<<~RUBY)
2343
class UserController < ApplicationController
@@ -33,10 +53,10 @@ def index; end
3353
expect_correction(<<~RUBY)
3454
class UserController < ApplicationController
3555
def index; end
36-
3756
def show
3857
@user = User.find(params[:id])
3958
end
59+
4060
end
4161
RUBY
4262
end
@@ -137,11 +157,11 @@ class TestController < BaseController
137157
def index
138158
end
139159
end
140-
141160
unless Rails.env.development?
142161
def edit
143162
end
144163
end
164+
145165
end
146166
RUBY
147167
end
@@ -181,8 +201,8 @@ def show; end
181201
expect_correction(<<~RUBY)
182202
class UserController < ApplicationController
183203
def show; end
184-
def edit; end
185204
def index; end
205+
def edit; end
186206
end
187207
RUBY
188208
end
@@ -205,9 +225,9 @@ def index; end
205225
class UserController < ApplicationController
206226
# index
207227
def index; end
208-
209228
# show
210229
def show; end
230+
211231
end
212232
RUBY
213233
end

0 commit comments

Comments
 (0)