File tree Expand file tree Collapse file tree 3 files changed +32
-0
lines changed Expand file tree Collapse file tree 3 files changed +32
-0
lines changed Original file line number Diff line number Diff line change
1
+ * [ #1510 ] ( https://github.com/rubocop/rubocop-rails/pull/1510 ) : Fix ` Rails/OrderArguments ` cop false positives when using column index argument. ([ @viralpraxis ] [ ] )
Original file line number Diff line number Diff line change @@ -52,6 +52,7 @@ def replacement(order_expressions)
52
52
order_arguments . map! { |arg | extract_column_and_direction ( arg . strip ) }
53
53
54
54
return if order_arguments . any? ( &:nil? )
55
+ return if order_arguments . any? { |column_name , _ | positional_column? ( column_name ) }
55
56
56
57
convert_to_preferred_arguments ( order_arguments ) . join ( ', ' )
57
58
end
@@ -68,6 +69,10 @@ def convert_to_preferred_arguments(order_expressions)
68
69
end
69
70
end
70
71
72
+ def positional_column? ( column_name )
73
+ column_name . match? ( /\A \d +\z / )
74
+ end
75
+
71
76
def extract_column_and_direction ( order_expression )
72
77
return unless ( column , direction = ORDER_EXPRESSION_REGEX . match ( order_expression ) &.captures )
73
78
Original file line number Diff line number Diff line change 128
128
User.order('LEFT(first_name, 1)')
129
129
RUBY
130
130
end
131
+
132
+ context 'with numeric string literal column name' do
133
+ it 'does not register an offense for `order` with a string argument' do
134
+ expect_no_offenses ( <<~RUBY )
135
+ User.order('1')
136
+ RUBY
137
+ end
138
+
139
+ it 'does not register an offense for `order` with multiple string arguments' do
140
+ expect_no_offenses ( <<~RUBY )
141
+ User.order('1', 'last_name')
142
+ RUBY
143
+ end
144
+
145
+ it 'does not registers an offense for `order` with a string argument with DESC direction' do
146
+ expect_no_offenses ( <<~RUBY )
147
+ User.order('1 DESC')
148
+ RUBY
149
+ end
150
+
151
+ it 'does not register an offense for `order` with a string argument with ASC order' do
152
+ expect_no_offenses ( <<~RUBY )
153
+ User.order('1 ASC')
154
+ RUBY
155
+ end
156
+ end
131
157
end
You can’t perform that action at this time.
0 commit comments