Skip to content

Commit baf39e6

Browse files
authored
Merge pull request #1101 from koic/make_rails_http_status_aware_of_string_number_status
[Fix #1080] Make `Rails/HttpStatus` aware of string number status
2 parents 8cfb90f + 411d119 commit baf39e6

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#1080](https://github.com/rubocop/rubocop-rails/issues/1080): Make `Rails/HttpStatus` aware of string number status. ([@r7kamura][])

lib/rubocop/cop/rails/http_status.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module Rails
88
# @example EnforcedStyle: symbolic (default)
99
# # bad
1010
# render :foo, status: 200
11+
# render :foo, status: '200'
1112
# render json: { foo: 'bar' }, status: 200
1213
# render plain: 'foo/bar', status: 304
1314
# redirect_to root_url, status: 301
@@ -50,7 +51,7 @@ class HttpStatus < Base
5051
PATTERN
5152

5253
def_node_matcher :status_code, <<~PATTERN
53-
(hash <(pair (sym :status) ${int sym}) ...>)
54+
(hash <(pair (sym :status) ${int sym str}) ...>)
5455
PATTERN
5556

5657
def on_send(node)
@@ -108,7 +109,7 @@ def preferred_style
108109
private
109110

110111
def symbol
111-
::Rack::Utils::SYMBOL_TO_STATUS_CODE.key(number)
112+
::Rack::Utils::SYMBOL_TO_STATUS_CODE.key(number.to_i)
112113
end
113114

114115
def number

spec/rubocop/cop/rails/http_status_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@
3939
RUBY
4040
end
4141

42+
it 'registers an offense and corrects using numeric string value' do
43+
expect_offense(<<~RUBY)
44+
render :foo, status: '200'
45+
^^^^^ Prefer `:ok` over `200` to define HTTP status code.
46+
RUBY
47+
48+
expect_correction(<<~RUBY)
49+
render :foo, status: :ok
50+
RUBY
51+
end
52+
4253
it 'does not register an offense when using symbolic value' do
4354
expect_no_offenses(<<~RUBY)
4455
render :foo, status: :ok

0 commit comments

Comments
 (0)