Class: RuboCop::Cop::Betterment::ImplicitRedirectType

Inherits:
RuboCop::Cop
  • Object
show all
Defined in:
lib/rubocop/cop/betterment/implicit_redirect_type.rb

Overview

Require explicit redirect statuses in routes.rb. Permanent redirects (301) are cached by clients, which makes it difficult to change later, and/or reuse the route for something else.

Examples:

# bad
get '/', redirect('/dashboard')
get { |params, request| '/dashboard' }

# good
get '/', redirect('/dashboard', status: 301)
get(status: 302) { |params, request| '/dashboard' }

Constant Summary collapse

ROUTES_FILE_NAME =
'routes.rb'
MSG =
'Rails will create a permanent (301) redirect, which is dangerous. ' \
'Please specify your desired status, e.g. redirect(..., status: 302)'

Instance Method Summary collapse

Instance Method Details

#on_block(node) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/rubocop/cop/betterment/implicit_redirect_type.rb', line 48

def on_block(node)
  return unless routes_file?

  if block_form_with_options(node) { |options| options.none? { |n| valid_status_option?(n) } } || block_form_without_options?(node)
    add_offense(node)
  end
end

#on_send(node) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/rubocop/cop/betterment/implicit_redirect_type.rb', line 56

def on_send(node)
  return unless routes_file?

  if arg_form_with_options(node) { |options| options.none? { |n| valid_status_option?(n) } } || arg_form_without_options?(node)
    add_offense(node)
  end
end