Class: RuboCop::Cop::Discourse::FabricatorShorthand

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
IgnoredNode
Defined in:
lib/rubocop/cop/discourse/fabricator_shorthand.rb

Overview

When fabricating a record without custom attributes, we can use the fabricator shorthand as long as the identifier matches the fabricator name.

# bad fab!(:user) { Fabricate(:user) }

# good fab!(:user)

When using custom attributes or the identifier doesn’t match, the shorthand can’t be used.

# good fab!(:user) { Fabricate(:user, trust_level: TrustLevel) }

# good fab!(:another_user) { Fabricate(:user) }

Instance Method Summary collapse

Instance Method Details

#on_block(node) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rubocop/cop/discourse/fabricator_shorthand.rb', line 41

def on_block(node)
  offending_fabricator?(node) do |expression, _identifier|
    add_offense(
      node,
      message: message(expression.source)
    ) do |corrector|
      next if part_of_ignored_node?(node)
      corrector.replace(node, expression.source)
    end
    ignore_node(node)
  end
end