Sha256: 02d63cb58cb507b507d544ba644583236f72c44ba13eedf49129c01aba2137f9

Contents?: true

Size: 1.32 KB

Versions: 5

Compression:

Stored size: 1.32 KB

Contents

require "factory_bot_rails"

module Satisfactory
  # Loads factory configurations from FactoryBot.
  #
  # @api private
  class Loader
    class << self
      # Skips factories that don't have a model that inherits from ApplicationRecord.
      #
      # @return [{Symbol => Hash}] a hash of factory configurations by factory name
      def factory_configurations # rubocop:disable Metrics/AbcSize
        FactoryBot.factories.each.with_object({}) do |(factory, model), configurations|
          next unless (model = factory.build_class)
          next unless model < ApplicationRecord

          associations = associations_for(model)
          parent_factory = factory.send(:parent)

          configurations[factory.name] = {
            associations: associations.transform_values { |a| a.map(&:name) },
            model:,
            name: factory.name,
            parent: (parent_factory.name unless parent_factory.is_a?(FactoryBot::NullFactory)),
            traits: factory.defined_traits.map(&:name),
          }
        end
      end

    private

      def associations_for(model)
        all = model.reflect_on_all_associations.reject(&:polymorphic?)
        plural = model.reflect_on_all_associations(:has_many).reject(&:polymorphic?)

        {
          plural:,
          singular: all - plural,
        }
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
satisfactory-0.3.2 lib/satisfactory/loader.rb
satisfactory-0.3.1 lib/satisfactory/loader.rb
satisfactory-0.3.0 lib/satisfactory/loader.rb
satisfactory-0.2.1 lib/satisfactory/loader.rb
satisfactory-0.2.0 lib/satisfactory/loader.rb