Module: FactoryBot::Preload::Helpers

Includes:
Syntax::Methods
Defined in:
lib/factory_bot/preload/helpers.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.define_helper_methodsObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/factory_bot/preload/helpers.rb', line 18

def self.define_helper_methods
  ActiveRecord::Base.descendants.each do |model|
    next if FactoryBot::Preload.reserved_tables.include?(model.table_name)

    helper_name = model.name.underscore.tr("/", "_").pluralize

    helper_name = FactoryBot::Preload.helper_name.call(
      model.name,
      helper_name
    )

    define_method(helper_name) do |name|
      factory(name, model)
    end
  end
end

.included(_base) ⇒ Object



35
36
37
# File 'lib/factory_bot/preload/helpers.rb', line 35

def self.included(_base)
  FactoryBot::Preload::Helpers.define_helper_methods
end

.load_modelsObject



8
9
10
11
12
13
14
15
16
# File 'lib/factory_bot/preload/helpers.rb', line 8

def self.load_models
  return unless defined?(Rails)

  models_path = Rails.application.root.join("app/models/**/*.rb")

  Dir[models_path].sort.each do |file|
    require_dependency file
  end
end

Instance Method Details

#factory(name, model = nil, &block) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/factory_bot/preload/helpers.rb', line 39

def factory(name, model = nil, &block)
  if block
    factory_set(name, &block)
  else
    factory_get(name, model)
  end
end