Class: FactoryBot::NamespacedFactories::NamespacedFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/factory_bot/namespaced_factories.rb

Overview

See:

- https://github.com/thoughtbot/factory_bot/issues/199
- https://github.com/thoughtbot/factory_bot/commit/652818bd1701db67ea03cd062c8259cd7fb37807

There are two main use-cases for Namespaced Factories:

1. Sharing Factories from an Engine or Gem
2. Decomposing an application using domain driven design

Instance Method Summary collapse

Constructor Details

#initialize(namespace, options) ⇒ NamespacedFactory

Returns a new instance of NamespacedFactory.



15
16
17
18
19
20
# File 'lib/factory_bot/namespaced_factories.rb', line 15

def initialize(namespace, options)
  @namespace = namespace
  @options = options
  @prefix = options.fetch(:prefix, "#{namespace.to_s.underscore}_")
  @require_prefix = options.fetch(:require_prefix, true)
end

Instance Method Details

#factory(name, options = {}, &block) ⇒ Object



22
23
24
25
# File 'lib/factory_bot/namespaced_factories.rb', line 22

def factory(name, options = {}, &block)
  __dsl__.factory("#{prefix}#{name}".to_sym, options.merge(class: "#{namespace}::#{name.to_s.classify}"), &block)
  register_without_namespace(name, options, &block)
end