class RuboCop::Cop::Rails::HttpStatus

Enforces use of symbolic or numeric value to define HTTP status.

@example EnforcedStyle: symbolic (default)

# bad
render :foo, status: 200
render json: { foo: 'bar' }, status: 200
render plain: 'foo/bar', status: 304
redirect_to root_url, status: 301

# good
render :foo, status: :ok
render json: { foo: 'bar' }, status: :ok
render plain: 'foo/bar', status: :not_modified
redirect_to root_url, status: :moved_permanently

@example EnforcedStyle: numeric

# bad
render :foo, status: :ok
render json: { foo: 'bar' }, status: :not_found
render plain: 'foo/bar', status: :not_modified
redirect_to root_url, status: :moved_permanently

# good
render :foo, status: 200
render json: { foo: 'bar' }, status: 404
render plain: 'foo/bar', status: 304
redirect_to root_url, status: 301

Public Instance Methods

autocorrect(node) click to toggle source
# File lib/rubocop/cop/rails/http_status.rb, line 60
def autocorrect(node)
  lambda do |corrector|
    checker = checker_class.new(node)
    corrector.replace(node.loc.expression, checker.preferred_style)
  end
end
on_send(node) click to toggle source
# File lib/rubocop/cop/rails/http_status.rb, line 48
def on_send(node)
  http_status(node) do |hash_node|
    status = status_code(hash_node)
    return unless status

    checker = checker_class.new(status)
    return unless checker.offensive?

    add_offense(checker.node, message: checker.message)
  end
end

Private Instance Methods

checker_class() click to toggle source
# File lib/rubocop/cop/rails/http_status.rb, line 69
def checker_class
  case style
  when :symbolic
    SymbolicStyleChecker
  when :numeric
    NumericStyleChecker
  end
end