Skip to content

Commit e40d8b9

Browse files
authored
Merge pull request rubocop#823 from anthony-robin/http-status-routes
[Fix rubocop#822] Extends `Rails/HttpStatus` cop to check `routes.rb`
2 parents 6051ed8 + 1dcbda1 commit e40d8b9

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#822](https://github.com/rubocop/rubocop-rails/issues/822): Extends `Rails/HttpStatus` cop to check `routes.rb`. ([@anthony-robin][])

lib/rubocop/cop/rails/http_status.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ module Rails
1212
# render plain: 'foo/bar', status: 304
1313
# redirect_to root_url, status: 301
1414
# head 200
15+
# get '/foobar', to: redirect('/foobar/baz', status: 301)
1516
#
1617
# # good
1718
# render :foo, status: :ok
1819
# render json: { foo: 'bar' }, status: :ok
1920
# render plain: 'foo/bar', status: :not_modified
2021
# redirect_to root_url, status: :moved_permanently
2122
# head :ok
23+
# get '/foobar', to: redirect('/foobar/baz', status: :moved_permanently)
2224
#
2325
# @example EnforcedStyle: numeric
2426
# # bad
@@ -27,25 +29,28 @@ module Rails
2729
# render plain: 'foo/bar', status: :not_modified
2830
# redirect_to root_url, status: :moved_permanently
2931
# head :ok
32+
# get '/foobar', to: redirect('/foobar/baz', status: :moved_permanently)
3033
#
3134
# # good
3235
# render :foo, status: 200
3336
# render json: { foo: 'bar' }, status: 404
3437
# render plain: 'foo/bar', status: 304
3538
# redirect_to root_url, status: 301
3639
# head 200
40+
# get '/foobar', to: redirect('/foobar/baz', status: 301)
3741
#
3842
class HttpStatus < Base
3943
include ConfigurableEnforcedStyle
4044
extend AutoCorrector
4145

42-
RESTRICT_ON_SEND = %i[render redirect_to head].freeze
46+
RESTRICT_ON_SEND = %i[render redirect_to head redirect].freeze
4347

4448
def_node_matcher :http_status, <<~PATTERN
4549
{
4650
(send nil? {:render :redirect_to} _ $hash)
4751
(send nil? {:render :redirect_to} $hash)
4852
(send nil? :head ${int sym} ...)
53+
(send nil? :redirect _ $hash)
4954
}
5055
PATTERN
5156

spec/rubocop/cop/rails/http_status_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
^^^ Prefer `:ok` over `200` to define HTTP status code.
2525
head 200, location: 'accounts'
2626
^^^ Prefer `:ok` over `200` to define HTTP status code.
27+
get '/foobar', to: redirect('/foobar/baz', status: 301)
28+
^^^ Prefer `:moved_permanently` over `301` to define HTTP status code.
2729
RUBY
2830

2931
expect_correction(<<~RUBY)
@@ -36,6 +38,7 @@
3638
redirect_to root_path(utm_source: :pr, utm_medium: :web), status: :moved_permanently
3739
head :ok
3840
head :ok, location: 'accounts'
41+
get '/foobar', to: redirect('/foobar/baz', status: :moved_permanently)
3942
RUBY
4043
end
4144

@@ -47,6 +50,7 @@
4750
redirect_to root_url, status: :moved_permanently
4851
redirect_to root_path(utm_source: :pr, utm_medium: :web), status: :moved_permanently
4952
head :ok
53+
get '/foobar', to: redirect('/foobar/baz', status: :moved_permanently)
5054
RUBY
5155
end
5256

@@ -58,6 +62,7 @@
5862
redirect_to root_url, status: 550
5963
redirect_to root_path(utm_source: :pr, utm_medium: :web), status: 550
6064
head 550
65+
get '/foobar', to: redirect('/foobar/baz', status: 550)
6166
RUBY
6267
end
6368
end
@@ -85,6 +90,8 @@
8590
^^^ Prefer `200` over `:ok` to define HTTP status code.
8691
head :ok, location: 'accounts'
8792
^^^ Prefer `200` over `:ok` to define HTTP status code.
93+
get '/foobar', to: redirect('/foobar/baz', status: :moved_permanently)
94+
^^^^^^^^^^^^^^^^^^ Prefer `301` over `:moved_permanently` to define HTTP status code.
8895
RUBY
8996

9097
expect_correction(<<~RUBY)
@@ -97,6 +104,7 @@
97104
redirect_to root_path(utm_source: :pr, utm_medium: :web), status: 301
98105
head 200
99106
head 200, location: 'accounts'
107+
get '/foobar', to: redirect('/foobar/baz', status: 301)
100108
RUBY
101109
end
102110

@@ -108,6 +116,7 @@
108116
redirect_to root_url, status: 301
109117
redirect_to root_path(utm_source: :pr, utm_medium: :web), status: 301
110118
head 200
119+
get '/foobar', to: redirect('/foobar/baz', status: 301)
111120
RUBY
112121
end
113122

0 commit comments

Comments
 (0)