Class: RuboCop::Cop::Obsession::Rails::PrivateCallback

Inherits:
Base
  • Object
show all
Includes:
Helpers, VisibilityHelp
Defined in:
lib/rubocop/cop/obsession/rails/private_callback.rb

Overview

This cop checks for callbacks methods that are not private.

Callback methods are usually never called outside of the class, so there is no reason to declare them in the public section. They should be private.

Examples:


# bad
before_action :load_blog_post

def load_blog_post
  ...
end

# good
before_action :load_blog_post

private

def load_blog_post
  ...
end

Constant Summary collapse

MSG =
'Make callback method private'

Constants included from Helpers

Helpers::VERBS

Instance Method Summary collapse

Methods included from Helpers

#rails_callback?, #verb?

Instance Method Details

#on_def(node) ⇒ Object



50
51
52
53
54
# File 'lib/rubocop/cop/obsession/rails/private_callback.rb', line 50

def on_def(node)
  if @callbacks.include?(node.method_name) && node_visibility(node) == :public
    add_offense(node, message: MSG)
  end
end

#on_new_investigationObject



40
41
42
# File 'lib/rubocop/cop/obsession/rails/private_callback.rb', line 40

def on_new_investigation
  @callbacks = Set.new
end

#on_send(node) ⇒ Object



44
45
46
47
48
# File 'lib/rubocop/cop/obsession/rails/private_callback.rb', line 44

def on_send(node)
  on_callback(node) do |callback, method_name|
    @callbacks << method_name if rails_callback?(callback.to_s)
  end
end