Class: FactoryBurgers::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/factory_burgers/builder.rb

Overview

Build resources from specified factories, traits, and attributes

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(owner = nil) ⇒ Builder

‘owner` is an optional resource that we can attach new resources to E.g. a `User` for whom we wish to build a `Post`.



8
9
10
# File 'lib/factory_burgers/builder.rb', line 8

def initialize(owner = nil)
  @owner = owner
end

Instance Attribute Details

#ownerObject (readonly)

Returns the value of attribute owner.



4
5
6
# File 'lib/factory_burgers/builder.rb', line 4

def owner
  @owner
end

Instance Method Details

#build(factory, traits, attributes, as: nil) ⇒ Object

TODO: clean up method signature



13
14
15
16
17
18
19
20
# File 'lib/factory_burgers/builder.rb', line 13

def build(factory, traits, attributes, as: nil) # rubocop:disable Naming/MethodParameterName; as is a good name here
  resource = insistently do
    FactoryBot.create(factory, *traits, attributes)
  end
  update_assocation_to_owner(as, resource) if owner && as

  return resource
end