hasghari/table_saw

View on GitHub
lib/table_saw/dependency_graph/has_many_directives.rb

Summary

Maintainability
A
35 mins
Test Coverage
A
100%
# frozen_string_literal: true

module TableSaw
  module DependencyGraph
    class HasManyDirectives
      attr_reader :manifest, :directive

      def initialize(manifest, directive)
        @manifest = manifest
        @directive = directive
      end

      def call
        valid_associations.map do |fk|
          TableSaw::DependencyGraph::AddDirective.new(
            fk.from_table,
            ids: query_result(fk).map { |r| r[TableSaw.schema_cache.primary_keys(fk.from_table)] },
            partial: directive.partial?
          )
        end
      end

      private

      def associations
        manifest.associations.has_many.fetch(directive.table_name, Set.new)
      end

      # rubocop:disable Metrics/AbcSize
      def valid_associations
        associations.select do |fk|
          next false if directive.partial? && TableSaw.schema_cache.primary_keys(fk.from_table).nil?
          next true if directive.has_many.key?(fk.from_table)

          manifest.has_many.fetch(directive.table_name, {}).key?(fk.from_table)
        end
      end
      # rubocop:enable Metrics/AbcSize

      def query_result(foreign_key)
        return [] unless directive.selectable?

        TableSaw.connection.exec_query(
          TableSaw::DependencyGraph::BuildHasManyQuery.new(manifest, directive, foreign_key).call
        )
      end
    end
  end
end