class TableSaw::DependencyGraph::BelongsToDirectives

Constants

QUERY

Attributes

directive[R]
manifest[R]

Public Class Methods

new(manifest, directive) click to toggle source
# File lib/table_saw/dependency_graph/belongs_to_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/belongs_to_directives.rb, line 17
def call
  associations.map do |fk|
    TableSaw::DependencyGraph::AddDirective.new(fk.to_table, ids: ids[fk.column.primary_key],
                                                             partial: directive.partial?)
  end
end

Private Instance Methods

associations() click to toggle source
# File lib/table_saw/dependency_graph/belongs_to_directives.rb, line 26
def associations
  manifest.associations.belongs_to.fetch(directive.table_name, Set.new)
end
ids() click to toggle source
# File lib/table_saw/dependency_graph/belongs_to_directives.rb, line 30
def ids
  @ids ||= associations.each_with_object({}) do |fk, memo|
    memo[fk.column.primary_key] = query_result(fk).map { |row| row[fk.column.primary_key] }
  end
end
query_result(foreign_key) click to toggle source

rubocop:disable Metrics/AbcSize

# File lib/table_saw/dependency_graph/belongs_to_directives.rb, line 37
def query_result(foreign_key)
  return [] unless directive.selectable?

  TableSaw::Connection.exec(
    format(QUERY, column: foreign_key.column.primary_key, table_name: directive.table_name,
                  clause: TableSaw::Queries::SerializeSqlInClause.new(directive.table_name,
                                                                      directive.primary_key,
                                                                      directive.ids).call,
                  polymorphic: foreign_key.type_condition)
  )
end