class TableSaw::DependencyGraph::HasManyDirectives

Constants

QUERY

Attributes

directive[R]
manifest[R]

Public Class Methods

new(manifest, directive) click to toggle source
# File lib/table_saw/dependency_graph/has_many_directives.rb, line 12
def initialize(manifest, directive)
  @manifest = manifest
  @directive = directive
end

Public Instance Methods

call() click to toggle source
# File lib/table_saw/dependency_graph/has_many_directives.rb, line 17
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 Instance Methods

associations() click to toggle source
# File lib/table_saw/dependency_graph/has_many_directives.rb, line 29
def associations
  manifest.associations.has_many.fetch(directive.table_name, Set.new)
end
query_result(foreign_key) click to toggle source
# File lib/table_saw/dependency_graph/has_many_directives.rb, line 43
def query_result(foreign_key)
  return [] unless directive.selectable?

  TableSaw::Connection.exec(
    format(QUERY, primary_key: TableSaw.schema_cache.primary_keys(foreign_key.from_table),
                  table: foreign_key.from_table,
                  clause: TableSaw::Queries::SerializeSqlInClause.new(foreign_key.from_table,
                                                                      foreign_key.column.primary_key,
                                                                      directive.ids).call,
                  polymorphic: foreign_key.type_condition)
  )
end
valid_associations() click to toggle source

rubocop:disable Metrics/AbcSize

# File lib/table_saw/dependency_graph/has_many_directives.rb, line 34
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.include?(fk.from_table)

    manifest.has_many.fetch(directive.table_name, []).include?(fk.from_table)
  end
end