class SeedDump::DependencyUnwrangler

Public Class Methods

new(models) click to toggle source
# File lib/seed_dump/dependency_unwrangler.rb, line 6
def initialize(models)
  @models = models
  @graph = RGL::DirectedAdjacencyGraph.new
  @evaluation_order = []
end

Public Instance Methods

evaluation_order() click to toggle source
# File lib/seed_dump/dependency_unwrangler.rb, line 12
def evaluation_order
  return [] if @models.empty?
  return @evaluation_order unless @evaluation_order.empty?

  build_graph unless graph_built?

  @evaluation_order = @graph.topsort_iterator.to_a.reverse
  @evaluation_order
end

Private Instance Methods

build_graph() click to toggle source
# File lib/seed_dump/dependency_unwrangler.rb, line 24
def build_graph
  @models.map do |model|
    referents_by_model(model).each do |referent|
      @graph.add_edge(model, referent)
    end
  end
end
collect_polymorphic_deps(association) click to toggle source
# File lib/seed_dump/dependency_unwrangler.rb, line 43
def collect_polymorphic_deps(association)
  ActiveRecord::Base.descendants.select do |other_model|
    other_model.reflect_on_all_associations(:has_many).any? do |has_many_association|
      has_many_association.options[:as] == association.name
    end
  end
end
graph_built?() click to toggle source
# File lib/seed_dump/dependency_unwrangler.rb, line 32
def graph_built?
  !@graph.edges.empty? && !@graph.vertices.empty?
end
referents_by_model(model) click to toggle source
# File lib/seed_dump/dependency_unwrangler.rb, line 36
def referents_by_model(model)
  associations = model.reflect_on_all_associations(:belongs_to)
  associations.map do |association|
    association.options[:polymorphic] ? collect_polymorphic_deps(association) : association.klass
  end.flatten
end