Class: Nucop::ExplicitFactoryBotUsage

Inherits:
RuboCop::Cop::Cop
  • Object
show all
Includes:
Helpers::FilePathHelper
Defined in:
lib/nucop/cops/explicit_factory_bot_usage.rb

Overview

This cop looks for usages of ‘FactoryGirl.create`, etc. See FactoryBotHelper::FACTORY_BOT_METHODS constant for a complete list.

The factory methods listed are included everywhere, so referencing the constant should rarely be necessary.

Examples:


# bad

job = FactoryGirl.create(:job, project: project)
FactoryGirl.build(:project, code: "Super Project")

# good

job = create(:job, project: project)
build(:project, code: "Super Project")

Constant Summary collapse

MSG =
"Do not explicitly use `%<constant>s` to build objects. The factory method `%<method>s` is globally available."

Instance Method Summary collapse

Methods included from Helpers::FilePathHelper

#acceptance_or_spec_file?, #support_file?

Instance Method Details

#autocorrect(node) ⇒ Object



37
38
39
40
41
# File 'lib/nucop/cops/explicit_factory_bot_usage.rb', line 37

def autocorrect(node)
  ->(corrector) do
    corrector.replace(node.source_range, node.source.sub(/(?:FactoryGirl|FactoryBot)[.]/, ""))
  end
end

#on_send(node) ⇒ Object



27
28
29
30
31
# File 'lib/nucop/cops/explicit_factory_bot_usage.rb', line 27

def on_send(node)
  explicit_factory_bot_usage(node) do
    add_offense(node, location: :expression, message: format(MSG, constant: node.receiver.const_name, method: node.method_name))
  end
end

#relevant_file?(file) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/nucop/cops/explicit_factory_bot_usage.rb', line 33

def relevant_file?(file)
  acceptance_or_spec_file?(file) && super
end