class RuboCop::Cop::RSpec::FactoryBot::DynamicAttributeDefinedStatically
type User::MAGIC
# good
comments_count 0
# good
kind :static
# good
closed_at { 1.day.from_now }
# good
closed_at 1.day.from_now
# bad
kind { [:active, :rejected].sample }
# good
kind [:active, :rejected].sample
# bad
@example
Prefer declaring dynamic attribute values in a block.
def autocorrect(node)
def autocorrect(node) if method_uses_parens?(node.location) autocorrect_replacing_parens(node) else autocorrect_without_parens(node) end end
def autocorrect_replacing_parens(node)
def autocorrect_replacing_parens(node) lambda do |corrector| corrector.replace(node.location.begin, ' { ') corrector.replace(node.location.end, ' }') end end
def autocorrect_without_parens(node)
def autocorrect_without_parens(node) lambda do |corrector| arguments = node.descendants.first expression = arguments.location.expression corrector.insert_before(expression, '{ ') corrector.insert_after(expression, ' }') end end
def method_uses_parens?(location)
def method_uses_parens?(location) return false unless location.begin && location.end location.begin.source == '(' && location.end.source == ')' end
def on_block(node)
def on_block(node) return if node.method_name == :trait factory_attributes(node).to_a.flatten.each do |attribute| if dynamic_defined_statically?(attribute) add_offense(attribute, location: :expression) end end end