Class: Rubocop::Cop::Rails::IncludeUrlHelper

Inherits:
RuboCop::Cop::Base
  • Object
show all
Defined in:
lib/rubocop/cop/rails/include_url_helper.rb

Overview

Avoid including ‘ActionView::Helpers::UrlHelper`.

It adds/overrides ~40 methods while usually only one is needed.
Instead, use the `Gitlab::Routing.url_helpers`/`Application.routes.url_helpers`(outside of gitlab)
and `ActionController::Base.helpers.link_to`.

See also

Examples:

# bad
class Foo
  include ActionView::Helpers::UrlHelper  # <-- includes 40 new methods !

  def link_to_something
    link_to(...)
  end
end

# good
class Foo
  def link_to_something
    url = Gitlab::Routing.url_helpers.project_blob_path(...)
    ActionController::Base.helpers.link_to(url, "Link text")
  end
end

Constant Summary collapse

MSG =
<<~MSG
  Avoid including `ActionView::Helpers::UrlHelper`.
  It adds/overrides ~40 methods while usually only one is needed.
  Instead, use the `Gitlab::Routing.url_helpers`/`Application.routes.url_helpers`(outside of gitlab)
  and `ActionController::Base.helpers.link_to`.
  See https://gitlab.com/gitlab-org/gitlab/-/issues/340567.
MSG

Instance Method Summary collapse

Instance Method Details

#include_url_helpers_node?(node) ⇒ Object



41
42
43
# File 'lib/rubocop/cop/rails/include_url_helper.rb', line 41

def_node_matcher :include_url_helpers_node?, <<~PATTERN
  (send nil? :include (const (const (const {nil? cbase} :ActionView) :Helpers) :UrlHelper))
PATTERN

#on_send(node) ⇒ Object



45
46
47
# File 'lib/rubocop/cop/rails/include_url_helper.rb', line 45

def on_send(node)
  add_offense(node) if include_url_helpers_node?(node)
end