module Ancestry::MaterializedPath
Constants
- ANCESTRY_DELIMITER
- BEFORE_LAST_SAVE_SUFFIX
- IN_DATABASE_SUFFIX
Public Class Methods
extended(base)
click to toggle source
# File lib/ancestry/materialized_path.rb, line 7 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 19 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 31 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) # 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
descendants_of(object)
click to toggle source
# File lib/ancestry/materialized_path.rb, line 49 def descendants_of(object) where(descendant_conditions(object)) 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 38 def indirects_of(object) t = arel_table node = to_node(object) # rails has case sensitive matching. if ActiveRecord::VERSION::MAJOR >= 5 where(t[ancestry_column].matches("#{node.child_ancestry}/%", nil, true)) else where(t[ancestry_column].matches("#{node.child_ancestry}/%")) end end
inpath_of(object)
click to toggle source
# File lib/ancestry/materialized_path.rb, line 25 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 77 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).include?(connection.adapter_name.downcase) && ActiveRecord::VERSION::STRING >= "6.1" reorder(Arel::Nodes::Ascending.new(arel_table[ancestry_column]).nulls_first) 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 90 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 11 def path_of(object) to_node(object).path end
roots()
click to toggle source
# File lib/ancestry/materialized_path.rb, line 15 def roots where(arel_table[ancestry_column].eq(nil)) end
siblings_of(object)
click to toggle source
# File lib/ancestry/materialized_path.rb, line 71 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 65 def subtree_of(object) t = arel_table node = to_node(object) where(descendant_conditions(node).or(t[primary_key].eq(node.id))) end