class RuboCop::Cop::RSpec::FactoryBot::DynamicAttributeDefinedStatically
Prefer declaring dynamic attribute values in a block.
@example
# bad kind [:active, :rejected].sample # good kind { [:active, :rejected].sample } # bad closed_at 1.day.from_now # good closed_at { 1.day.from_now } # good kind :static # good comments_count 0 # good type User::MAGIC
Constants
- MSG
Public Instance Methods
autocorrect(node)
click to toggle source
# File lib/rubocop/cop/rspec/factory_bot/dynamic_attribute_defined_statically.rb, line 49 def autocorrect(node) if method_uses_parens?(node.location) autocorrect_replacing_parens(node) else autocorrect_without_parens(node) end end
on_block(node)
click to toggle source
# File lib/rubocop/cop/rspec/factory_bot/dynamic_attribute_defined_statically.rb, line 40 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
Private Instance Methods
autocorrect_replacing_parens(node)
click to toggle source
# File lib/rubocop/cop/rspec/factory_bot/dynamic_attribute_defined_statically.rb, line 64 def autocorrect_replacing_parens(node) lambda do |corrector| corrector.replace(node.location.begin, ' { ') corrector.replace(node.location.end, ' }') end end
autocorrect_without_parens(node)
click to toggle source
# File lib/rubocop/cop/rspec/factory_bot/dynamic_attribute_defined_statically.rb, line 71 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
method_uses_parens?(location)
click to toggle source
# File lib/rubocop/cop/rspec/factory_bot/dynamic_attribute_defined_statically.rb, line 59 def method_uses_parens?(location) return false unless location.begin && location.end location.begin.source == '(' && location.end.source == ')' end