Class: Dagable::Associations

Inherits:
Module
  • Object
show all
Defined in:
lib/dagable/associations.rb

Overview

A dynamic module that, when included into an ActiveRecord model, declares all has_many associations needed for the DAG. Constructed with the class names of the concrete Edge and Ancestry subclasses.

Declared associations

Edge associations (direct relationships):

  • parent_edges / parents — direct parent records via the edge table

  • child_edges / children — direct child records via the edge table

Ancestry associations (raw ancestry table rows):

  • predecessor_connections — ancestry rows where this node is the successor

  • successor_connections — ancestry rows where this node is the predecessor

Traversal methods (predecessors, successors, self_and_predecessors, self_and_successors) are provided by Dagable::InstanceMethods instead of has_many :through to correctly exclude self-referential rows.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(edge_class, ancestry_class) ⇒ Associations

Returns a new instance of Associations.



24
25
26
27
28
29
# File 'lib/dagable/associations.rb', line 24

def initialize(edge_class, ancestry_class)
  super()

  @edge_class = edge_class
  @ancestry_class = ancestry_class
end

Instance Attribute Details

#ancestry_classObject (readonly)

Returns the value of attribute ancestry_class.



22
23
24
# File 'lib/dagable/associations.rb', line 22

def ancestry_class
  @ancestry_class
end

#edge_classObject (readonly)

Returns the value of attribute edge_class.



22
23
24
# File 'lib/dagable/associations.rb', line 22

def edge_class
  @edge_class
end

Instance Method Details

#included(base) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/dagable/associations.rb', line 31

def included(base)
  base.has_many :parent_edges, class_name: edge_class, foreign_key: :child_id, dependent: :destroy,
                               inverse_of: :child
  base.has_many :parents, through: :parent_edges

  base.has_many :child_edges, class_name: edge_class, foreign_key: :parent_id, dependent: :destroy,
                              inverse_of: :parent
  base.has_many :children, through: :child_edges

  base.has_many :predecessor_connections,
                class_name: ancestry_class,
                foreign_key: :successor_id,
                dependent: :destroy,
                inverse_of: :successor

  base.has_many :successor_connections,
                class_name: ancestry_class,
                foreign_key: :predecessor_id,
                dependent: :destroy,
                inverse_of: :predecessor
end