class Gitlab::Styles::Rubocop::Cop::RedirectWithStatus

This cop prevents usage of 'redirect_to' in actions 'destroy' without specifying 'status'. See gitlab.com/gitlab-org/gitlab-ce/issues/31840

Constants

MSG

Public Instance Methods

on_def(node) click to toggle source
# File lib/gitlab/styles/rubocop/cop/redirect_with_status.rb, line 12
def on_def(node)
  return unless in_controller?(node)
  return unless destroy?(node) || destroy_all?(node)

  node.each_descendant(:send) do |def_node|
    next unless redirect_to?(def_node)

    methods = []

    def_node.children.last.each_node(:pair) do |pair|
      methods << pair.children.first.children.first
    end

    add_offense(def_node, location: :selector) unless methods.include?(:status)
  end
end

Private Instance Methods

destroy?(node) click to toggle source
# File lib/gitlab/styles/rubocop/cop/redirect_with_status.rb, line 35
def destroy?(node)
  node.children.first == :destroy
end
destroy_all?(node) click to toggle source
# File lib/gitlab/styles/rubocop/cop/redirect_with_status.rb, line 39
def destroy_all?(node)
  node.children.first == :destroy_all
end
in_controller?(node) click to toggle source
# File lib/gitlab/styles/rubocop/cop/redirect_with_status.rb, line 31
def in_controller?(node)
  node.location.expression.source_buffer.name.end_with?('_controller.rb')
end
redirect_to?(node) click to toggle source
# File lib/gitlab/styles/rubocop/cop/redirect_with_status.rb, line 43
def redirect_to?(node)
  node.children[1] == :redirect_to
end