Class: RuboCop::Cop::Rails::HttpStatusNameConsistency

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/rails/http_status_name_consistency.rb

Overview

Enforces consistency by using the current HTTP status names.

Examples:

# bad
render json: { error: "Invalid data" }, status: :unprocessable_entity
head :payload_too_large

# good
render json: { error: "Invalid data" }, status: :unprocessable_content
head :content_too_large

Constant Summary collapse

RESTRICT_ON_SEND =
i[render redirect_to head assert_response assert_redirected_to].freeze
PREFERRED_STATUSES =
{
  unprocessable_entity: :unprocessable_content,
  payload_too_large: :content_too_large
}.freeze
MSG =
'Prefer `:%<preferred>s` over `:%<current>s`.'

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rubocop/cop/rails/http_status_name_consistency.rb', line 45

def on_send(node)
  status_method_call(node) do |status_node|
    if status_node.hash_type?
      # Handle hash arguments like { status: :unprocessable_entity }
      status_hash_value(status_node) do |status_value|
        check_status_name_consistency(status_value)
      end
    else
      # Handle positional arguments like head :unprocessable_entity
      check_status_name_consistency(status_node)
    end
  end
end