Sha256: a1062ba37130c4b967ffa0ff2131f877ee0b1ebc87bbf9d010dffd450ad97603

Contents?: true

Size: 945 Bytes

Versions: 4

Compression:

Stored size: 945 Bytes

Contents

module FactoryBurgers
  # Discover information about factories for a class, such as what associations
  # are defined on that class that also have factories we can use
  module Introspection
    module_function

    def factories
      FactoryBot::Internal.factories.sort_by(&:name)
    end

    # Return a list of factories for a model instance's associations
    def association_factories(klass)
      buildable_associations(klass).flat_map do |assoc|
        factories_for_class(assoc.klass).map do |factory|
          {
            association: assoc,
            factory: factory,
          }
        end
      end
    end

    def factories_for_class(klass)
      factories.select do |factory|
        factory.build_class.ancestors.include?(klass)
      end
    end

    def buildable_associations(klass)
      klass.reflect_on_all_associations.reject { |assoc| assoc.is_a?(ActiveRecord::Reflection::ThroughReflection) }
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
factory_burgers-0.1.5 lib/factory_burgers/introspection.rb
factory_burgers-0.1.4 lib/factory_burgers/introspection.rb
factory_burgers-0.1.2 lib/factory_burgers/introspection.rb
factory_burgers-0.1.0 lib/factory_burgers/introspection.rb