module Ancestry::MaterializedPath

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/%

Constants

ANCESTRY_DELIMITER
BEFORE_LAST_SAVE_SUFFIX
IN_DATABASE_SUFFIX
ROOT

Public Class Methods

extended(base) click to toggle source
# File lib/ancestry/materialized_path.rb, line 11
def self.extended(base)
  base.send(:include, InstanceMethods)
end

Public Instance Methods

ancestors_of(object) click to toggle source
# File lib/ancestry/materialized_path.rb, line 23
def ancestors_of(object)
  t = arel_table
  node = to_node(object)
  where(t[primary_key].in(node.ancestor_ids))
end
children_of(object) click to toggle source
# File lib/ancestry/materialized_path.rb, line 35
def children_of(object)
  t = arel_table
  node = to_node(object)
  where(t[ancestry_column].eq(node.child_ancestry))
end
descendant_conditions(object) click to toggle source

deprecated

# File lib/ancestry/materialized_path.rb, line 54
def descendant_conditions(object)
  t = arel_table
  node = to_node(object)
  t[ancestry_column].matches("#{node.child_ancestry}/%", nil, true).or(t[ancestry_column].eq(node.child_ancestry))
end
descendants_of(object) click to toggle source
# File lib/ancestry/materialized_path.rb, line 48
def descendants_of(object)
  node = to_node(object)
  indirects_of(node).or(children_of(node))
end
indirects_of(object) click to toggle source

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

# File lib/ancestry/materialized_path.rb, line 42
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) click to toggle source
# File lib/ancestry/materialized_path.rb, line 29
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) click to toggle source
# File lib/ancestry/materialized_path.rb, line 72
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) click to toggle source
# File lib/ancestry/materialized_path.rb, line 85
def ordered_by_ancestry_and(order)
  ordered_by_ancestry(order)
end
path_of(object) click to toggle source
# File lib/ancestry/materialized_path.rb, line 15
def path_of(object)
  to_node(object).path
end
roots() click to toggle source
# File lib/ancestry/materialized_path.rb, line 19
def roots
  where(arel_table[ancestry_column].eq(ROOT))
end
siblings_of(object) click to toggle source
# File lib/ancestry/materialized_path.rb, line 66
def siblings_of(object)
  t = arel_table
  node = to_node(object)
  where(t[ancestry_column].eq(node[ancestry_column].presence))
end
subtree_of(object) click to toggle source
# File lib/ancestry/materialized_path.rb, line 60
def subtree_of(object)
  t = arel_table
  node = to_node(object)
  descendants_of(node).or(where(t[primary_key].eq(node.id)))
end