Class: Dagable::Edge
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Dagable::Edge
- Defined in:
- lib/dagable/edge.rb
Overview
Abstract base class for direct edge records. Each dagable model gets a concrete subclass (e.g. Category::Edge) backed by {table}_edges.
An edge represents a single direct parent-child relationship. When an edge is created, link_ancestries must be called to update the transitive paths in the ancestry table.
Instance Method Summary collapse
-
#link_ancestries ⇒ Object
Materializes all transitive ancestry rows implied by this edge.
Instance Method Details
#link_ancestries ⇒ Object
Materializes all transitive ancestry rows implied by this edge.
For an edge (parent -> child), this computes the cross product of all predecessors of the parent with all successors of the child, and inserts an ancestry row for each pair. The depth is calculated as the sum of the predecessor’s depth to the parent + the child’s depth to the successor + 1.
This is safe to call multiple times — existing ancestry rows are skipped via INSERT … ON CONFLICT DO NOTHING.
25 26 27 28 29 |
# File 'lib/dagable/edge.rb', line 25 def link_ancestries records = ancestry_records(ancestry_model) ancestry_model.insert_all(records, unique_by: %i[predecessor_id successor_id]) if records.any? end |