Sha256: 9df27ec1d330b1f20fe89f0b610a92cf36dc1ff05aec316936ed337259450aa5

Contents?: true

Size: 1.16 KB

Versions: 2

Compression:

Stored size: 1.16 KB

Contents

# frozen_string_literal: true

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

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

      def call
        valid_associations.map do |table, column|
          TableSaw::DependencyGraph::AddDirective.new(
            table, ids: query_result(table, column).map { |r| r['id'] }, partial: directive.partial?
          )
        end
      end

      private

      def associations
        context.has_many.fetch(directive.table_name, [])
      end

      def valid_associations
        associations.select do |table, _column|
          next false if directive.partial? && context.tables_with_no_ids.include?(table)

          context.has_many_mapping.fetch(directive.table_name, []).include?(table)
        end
      end

      def query_result(table, column)
        return [] unless directive.selectable?

        context.perform_query(
          format('select id from %{table} where %{column} in (%{ids})',
                 table: table, column: column, ids: directive.ids.join(','))
        )
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
table_saw-0.5.0 lib/table_saw/dependency_graph/has_many_directives.rb
table_saw-0.4.0 lib/table_saw/dependency_graph/has_many_directives.rb