module Ancestry::MaterializedPath

Public Class Methods

extended(base) click to toggle source
# File lib/ancestry/materialized_path.rb, line 3
def self.extended(base)
  base.validates_format_of base.ancestry_column, :with => Ancestry::ANCESTRY_PATTERN, :allow_nil => true
  base.send(:include, InstanceMethods)
end

Public Instance Methods

ancestor_conditions(object) click to toggle source
# File lib/ancestry/materialized_path.rb, line 12
def ancestor_conditions(object)
  t = arel_table
  node = to_node(object)
  t[primary_key].in(node.ancestor_ids)
end
child_conditions(object) click to toggle source
# File lib/ancestry/materialized_path.rb, line 24
def child_conditions(object)
  t = arel_table
  node = to_node(object)
  t[ancestry_column].eq(node.child_ancestry)
end
descendant_conditions(object) click to toggle source
# File lib/ancestry/materialized_path.rb, line 42
def descendant_conditions(object)
  t = arel_table
  node = to_node(object)
  # rails has case sensitive matching.
  if ActiveRecord::VERSION::MAJOR >= 5
    t[ancestry_column].matches("#{node.child_ancestry}/%", nil, true).or(t[ancestry_column].eq(node.child_ancestry))
  else
    t[ancestry_column].matches("#{node.child_ancestry}/%").or(t[ancestry_column].eq(node.child_ancestry))
  end
end
indirect_conditions(object) click to toggle source

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

# File lib/ancestry/materialized_path.rb, line 31
def indirect_conditions(object)
  t = arel_table
  node = to_node(object)
  # rails has case sensitive matching.
  if ActiveRecord::VERSION::MAJOR >= 5
    t[ancestry_column].matches("#{node.child_ancestry}/%", nil, true)
  else
    t[ancestry_column].matches("#{node.child_ancestry}/%")
  end
end
path_conditions(object) click to toggle source
# File lib/ancestry/materialized_path.rb, line 18
def path_conditions(object)
  t = arel_table
  node = to_node(object)
  t[primary_key].in(node.path_ids)
end
root_conditions() click to toggle source
# File lib/ancestry/materialized_path.rb, line 8
def root_conditions
  arel_table[ancestry_column].eq(nil)
end
sibling_conditions(object) click to toggle source
# File lib/ancestry/materialized_path.rb, line 59
def sibling_conditions(object)
  t = arel_table
  node = to_node(object)
  t[ancestry_column].eq(node[ancestry_column])
end
subtree_conditions(object) click to toggle source
# File lib/ancestry/materialized_path.rb, line 53
def subtree_conditions(object)
  t = arel_table
  node = to_node(object)
  descendant_conditions(node).or(t[primary_key].eq(node.id))
end