Class: RuboCop::Cop::GreppableRails::NoIncludeInHelper

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/greppable_rails/no_include_in_helper.rb

Overview

Prohibits include inside helper modules. Including makes it hard to grep where a helper method actually lives, and broadens the constant-dependency surface so that changes to one helper become unpredictably visible from another.

Examples:

# bad
# app/helpers/top_helper.rb
module TopHelper
  include PostHelper

  def some_link(posts)
    some_post_link(posts.first)
  end
end

# good
# app/helpers/post_link_tag.rb
module PostLinkTag
  module_function

  def some_post_link(post)
    # ...
  end
end

# app/helpers/top_helper.rb
module TopHelper
  def some_link(posts)
    PostLinkTag.some_post_link(posts.first)
  end
end

Constant Summary collapse

MSG =
"Do not include in Helper."
RESTRICT_ON_SEND =
%i[include].freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/rubocop/cop/greppable_rails/no_include_in_helper.rb', line 46

def on_send(node)
  parent = node.parent
  parent = parent.parent if parent&.begin_type?
  return unless parent&.module_type?
  return unless parent.identifier.const_name.end_with?("Helper")

  add_offense(node) if render_with_inline_option?(node)
end