Module: FactoryBotHelpers::EcfrGemHelpers

Extended by:
EcfrGemHelpers
Included in:
EcfrGemHelpers
Defined in:
lib/ecfr/testing/factory_bot_helpers/ecfr_gem_initialize_helpers.rb

Instance Method Summary collapse

Instance Method Details

#override_factory_class(phactory, klass) ⇒ Object

Allows an upstream user wants to override the factory defined in this gem be instantiated as another class defined in thier applicaiton (inheriting from a gem defined class). See the Extensible module for more details.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/ecfr/testing/factory_bot_helpers/ecfr_gem_initialize_helpers.rb', line 8

def override_factory_class(phactory, klass)
  FactoryBot.modify do
    factory phactory do
      initialize_with {
        attrs = FactoryBotHelpers::EcfrGemHelpers.pre_initialize_attrs_for(
          phactory,
          attributes
        )
        attrs.deep_stringify_keys! if attrs.respond_to?(:deep_stringify_keys)
        klass.constantize.new(attrs)
      }
    end
  end
end

#pre_initialize_attrs_for(factory, attrs) ⇒ Object

Provides a single method that any factory can call for attribute manipulation and is used to when override_factory_class is used to generate a factory definied in this gem but as another class (that inherits from a gem defined class) defined in their application.



27
28
29
30
31
32
33
34
35
36
# File 'lib/ecfr/testing/factory_bot_helpers/ecfr_gem_initialize_helpers.rb', line 27

def pre_initialize_attrs_for(factory, attrs)
  case factory
  when :content_version_result
    pre_initialize_attrs_for_content_version_result(attrs)
  when :structure
    attrs[:data]
  else
    attrs
  end
end