Class: RuboCop::Cop::Obsession::Rails::ServiceName

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

Overview

This cop checks for services and jobs whose name do not start with a verb.

Services and jobs with only one public method should have a name that starts with a verb, because these classes are essentially performing one action, and the best way to describe an action is with a verb.

Examples:


# bad
class Company
  def perform
    ...
  end
end

# good
class ImportCompany
  def perform
    ...
  end
end

# bad
class BlogPostPopularityJob < ApplicationJob
  def perform
    ...
  end
end

# good
class UpdateBlogPostPopularityJob < ApplicationJob
  def perform
    ...
  end
end

Direct Known Subclasses

ServicePerformMethod

Constant Summary collapse

MSG =
'Service or Job name should start with a verb.'
IGNORED_PUBLIC_METHODS =
%i[initialize lock_period].freeze

Instance Method Summary collapse

Methods included from Helpers

#rails_callback?, #verb?

Instance Method Details

#on_class(class_node) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/rubocop/cop/obsession/rails/service_name.rb', line 52

def on_class(class_node)
  return if public_methods(class_node).length != 1
  class_name = class_node.identifier.source
  class_name_first_word = class_name.underscore.split('_').first

  add_offense(class_node) if !verb?(class_name_first_word)
end