Module: Ancestry::MaterializedPath

Included in:
MaterializedPath2
Defined in:
lib/ancestry/materialized_path.rb

Overview

store ancestry as grandparent_id/parent_id root a=nil,id=1 children=id,id/% == 1, 1/% 3: a=1/2,id=3 children=a/id,a/id/% == 1/2/3, 1/2/3/%

Defined Under Namespace

Modules: InstanceMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



6
7
8
# File 'lib/ancestry/materialized_path.rb', line 6

def self.extended(base)
  base.send(:include, InstanceMethods)
end

Instance Method Details

#ancestors_of(object) ⇒ Object



18
19
20
21
22
# File 'lib/ancestry/materialized_path.rb', line 18

def ancestors_of(object)
  t = arel_table
  node = to_node(object)
  where(t[primary_key].in(node.ancestor_ids))
end

#ancestry_rootObject



91
92
93
# File 'lib/ancestry/materialized_path.rb', line 91

def ancestry_root
  nil
end

#children_of(object) ⇒ Object



30
31
32
33
34
# File 'lib/ancestry/materialized_path.rb', line 30

def children_of(object)
  t = arel_table
  node = to_node(object)
  where(t[ancestry_column].eq(node.child_ancestry))
end

#descendant_before_save_conditions(object) ⇒ Object



57
58
59
60
# File 'lib/ancestry/materialized_path.rb', line 57

def descendant_before_save_conditions(object)
  node = to_node(object)
  descendants_by_ancestry( node.child_ancestry_before_save )
end

#descendant_conditions(object) ⇒ Object



52
53
54
55
# File 'lib/ancestry/materialized_path.rb', line 52

def descendant_conditions(object)
  node = to_node(object)
  descendants_by_ancestry( node.child_ancestry )
end

#descendants_by_ancestry(ancestry) ⇒ Object



47
48
49
50
# File 'lib/ancestry/materialized_path.rb', line 47

def descendants_by_ancestry(ancestry)
  t = arel_table
  t[ancestry_column].matches("#{ancestry}#{ancestry_delimiter}%", nil, true).or(t[ancestry_column].eq(ancestry))
end

#descendants_of(object) ⇒ Object



43
44
45
# File 'lib/ancestry/materialized_path.rb', line 43

def descendants_of(object)
  where(descendant_conditions(object))
end

#indirects_of(object) ⇒ Object

indirect = anyone who is a descendant, but not a child



37
38
39
40
41
# File 'lib/ancestry/materialized_path.rb', line 37

def indirects_of(object)
  t = arel_table
  node = to_node(object)
  where(t[ancestry_column].matches("#{node.child_ancestry}#{ancestry_delimiter}%", nil, true))
end

#inpath_of(object) ⇒ Object



24
25
26
27
28
# File 'lib/ancestry/materialized_path.rb', line 24

def inpath_of(object)
  t = arel_table
  node = to_node(object)
  where(t[primary_key].in(node.path_ids))
end

#ordered_by_ancestry(order = nil) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/ancestry/materialized_path.rb', line 74

def ordered_by_ancestry(order = nil)
  if %w(mysql mysql2 sqlite sqlite3).include?(connection.adapter_name.downcase)
    reorder(arel_table[ancestry_column], order)
  elsif %w(postgresql oracleenhanced).include?(connection.adapter_name.downcase) && ActiveRecord::VERSION::STRING >= "6.1"
    reorder(Arel::Nodes::Ascending.new(arel_table[ancestry_column]).nulls_first, order)
  else
    reorder(
      Arel::Nodes::Ascending.new(Arel::Nodes::NamedFunction.new('COALESCE', [arel_table[ancestry_column], Arel.sql("''")])),
      order
    )
  end
end

#ordered_by_ancestry_and(order) ⇒ Object



87
88
89
# File 'lib/ancestry/materialized_path.rb', line 87

def ordered_by_ancestry_and(order)
  ordered_by_ancestry(order)
end

#path_of(object) ⇒ Object



10
11
12
# File 'lib/ancestry/materialized_path.rb', line 10

def path_of(object)
  to_node(object).path
end

#rootsObject



14
15
16
# File 'lib/ancestry/materialized_path.rb', line 14

def roots
  where(arel_table[ancestry_column].eq(ancestry_root))
end

#siblings_of(object) ⇒ Object



68
69
70
71
72
# File 'lib/ancestry/materialized_path.rb', line 68

def siblings_of(object)
  t = arel_table
  node = to_node(object)
  where(t[ancestry_column].eq(node[ancestry_column].presence))
end

#subtree_of(object) ⇒ Object



62
63
64
65
66
# File 'lib/ancestry/materialized_path.rb', line 62

def subtree_of(object)
  t = arel_table
  node = to_node(object)
  descendants_of(node).or(where(t[primary_key].eq(node.id)))
end