module FactoryBot::Refinements::ActiveRecord

Public Class Methods

default_factory(association) click to toggle source
# File lib/factory_bot/refinements/active_record.rb, line 7
def self.default_factory(association)
  association.klass.model_name.singular
end

Public Instance Methods

build_with_factory(...) click to toggle source
# File lib/factory_bot/refinements/active_record.rb, line 25
def build_with_factory(...)
  factory = @__factory || ActiveRecord.default_factory(proxy_association)

  FactoryBot.build(factory, ...).tap {|record|
    proxy_association.add_to_target record

    @__factory = nil
  }
end
create_with_factory(*args, **kwargs, &block) click to toggle source
# File lib/factory_bot/refinements/active_record.rb, line 16
def create_with_factory(*args, **kwargs, &block)
  factory = @__factory || ActiveRecord.default_factory(proxy_association)
  key     = proxy_association.reflection.inverse_of.name

  FactoryBot.create(factory, *args, key => proxy_association.owner, **kwargs, &block).tap {
    @__factory = nil
  }
end
factory(name) click to toggle source
# File lib/factory_bot/refinements/active_record.rb, line 12
def factory(name)
  tap { @__factory = name }
end