class RuboCop::Cop::Rails::HttpStatus
assert_redirected_to ‘/some/path’, status: 301
assert_response 200
head 200
redirect_to root_url, status: 301
render plain: ‘foo/bar’, status: 304
render json: { foo: ‘bar’ }, status: 404
render :foo, status: 200
# good
assert_redirected_to ‘/some/path’, status: :moved_permanently
assert_response :ok
head :ok
redirect_to root_url, status: :moved_permanently
render plain: ‘foo/bar’, status: :not_modified
render json: { foo: ‘bar’ }, status: :not_found
render :foo, status: :ok
# bad
@example EnforcedStyle: numeric
assert_redirected_to ‘/some/path’, status: :moved_permanently
assert_response :ok
head :ok
redirect_to root_url, status: :moved_permanently
render plain: ‘foo/bar’, status: :not_modified
render json: { foo: ‘bar’ }, status: :ok
render :foo, status: :ok
# good
assert_redirected_to ‘/some/path’, status: 301
assert_response 200
head 200
redirect_to root_url, status: 301
render plain: ‘foo/bar’, status: 304
render json: { foo: ‘bar’ }, status: 200
render :foo, status: ‘200’
render :foo, status: 200
# bad
@example EnforcedStyle: symbolic (default)
Enforces use of symbolic or numeric value to define HTTP status.
def checker_class
def checker_class case style when :symbolic SymbolicStyleChecker when :numeric NumericStyleChecker end end
def on_send(node)
def on_send(node) http_status(node) do |hash_node_or_status_code| status = if hash_node_or_status_code.hash_type? status_code(hash_node_or_status_code) else hash_node_or_status_code end return unless status checker = checker_class.new(status) return unless checker.offensive? add_offense(checker.node, message: checker.message) do |corrector| corrector.replace(checker.node, checker.preferred_style) end end end