Class: RuboCop::Cop::Rails::MailerName

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/rails/mailer_name.rb

Overview

Enforces that mailer names end with ‘Mailer` suffix.

Without the ‘Mailer` suffix it isn’t immediately apparent what’s a mailer and which views are related to the mailer.

Examples:

# bad
class User < ActionMailer::Base
end

class User < ApplicationMailer
end

# good
class UserMailer < ActionMailer::Base
end

class UserMailer < ApplicationMailer
end

Constant Summary collapse

MSG =
'Mailer should end with `Mailer` suffix.'

Instance Method Summary collapse

Instance Method Details

#on_class(node) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/rubocop/cop/rails/mailer_name.rb', line 50

def on_class(node)
  class_definition?(node) do |name_node|
    add_offense(name_node) do |corrector|
      autocorrect(corrector, name_node)
    end
  end
end

#on_send(node) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/rubocop/cop/rails/mailer_name.rb', line 58

def on_send(node)
  return unless class_new_definition?(node)

  casgn_parent = node.each_ancestor(:casgn).first
  return unless casgn_parent

  name = casgn_parent.children[1]
  return if mailer_suffix?(name)

  add_offense(casgn_parent.loc.name) do |corrector|
    autocorrect(corrector, casgn_parent)
  end
end