Skip to content

Commit

Permalink
Sort nodes for a proper disconnect_inv/destroy order
Browse files Browse the repository at this point in the history
Sort nodes for a proper disconnect_inv/destroy order. If the nodes
are not sorted, then we can be destroying child records instead
of disconnecting them via the parent record.
  • Loading branch information
Ladas committed Jul 24, 2017
1 parent d7be775 commit 0279221
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions app/models/manager_refresh/inventory_collection/graph.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,21 @@ def build_directed_acyclic_graph!
# depth 10. We should throw a warning maybe asking for simplifying the interconnections in the models.
assert_graph!(edges)

self.nodes = sort_nodes(nodes)

self
end

private

def sort_nodes(nodes)
# Separate to root nodes and child nodes, where child nodes are determined by having parent_inventory_collections
root_nodes, child_nodes = nodes.partition { |node| node.parent_inventory_collections.blank? }
# And order it that root nodes comes first, that way the disconnect of the child nodes should be always done as
# a part of the root nodes, as intended.
root_nodes + child_nodes
end

def assert_inventory_collections(inventory_collections)
inventory_collections.each do |inventory_collection|
unless inventory_collection.kind_of? ::ManagerRefresh::InventoryCollection
Expand Down

0 comments on commit 0279221

Please sign in to comment.