Module: Ancestry::MaterializedPath2
Overview
store ancestry as /grandparent_id/parent_id/ root: a=/,id=1 children=#a#id/% == /1/% 3: a=/1/2/,id=3 children=#a#id/% == /1/2/3/%
Defined Under Namespace
Modules: InstanceMethods
Class Method Summary
collapse
Instance Method Summary
collapse
#ancestors_of, #ancestry_depth_change, #children_of, #concat, #descendant_before_last_save_conditions, #descendant_conditions, #descendants_of, #inpath_of, #ordered_by_ancestry_and, #parse_ancestry_column, #path_of, #roots, #siblings_of, #subtree_of
Class Method Details
.construct_depth_sql(table_name, ancestry_column, ancestry_delimiter) ⇒ Object
50
51
52
53
54
|
# File 'lib/ancestry/materialized_path2.rb', line 50
def self.construct_depth_sql(table_name, ancestry_column, ancestry_delimiter)
tmp = %{(LENGTH(#{table_name}.#{ancestry_column}) - LENGTH(REPLACE(#{table_name}.#{ancestry_column},'#{ancestry_delimiter}','')))}
tmp += "/#{ancestry_delimiter.size}" if ancestry_delimiter.size > 1
"(#{tmp} -1)"
end
|
Instance Method Details
#ancestry_depth_sql ⇒ Object
37
38
39
|
# File 'lib/ancestry/materialized_path2.rb', line 37
def ancestry_depth_sql
@ancestry_depth_sql ||= MaterializedPath2.construct_depth_sql(table_name, ancestry_column, ancestry_delimiter)
end
|
#ancestry_root ⇒ Object
29
30
31
|
# File 'lib/ancestry/materialized_path2.rb', line 29
def ancestry_root
ancestry_delimiter
end
|
#child_ancestry_sql ⇒ Object
33
34
35
|
# File 'lib/ancestry/materialized_path2.rb', line 33
def child_ancestry_sql
concat("#{table_name}.#{ancestry_column}", "#{table_name}.#{primary_key}", "'#{ancestry_delimiter}'")
end
|
#descendants_by_ancestry(ancestry) ⇒ Object
25
26
27
|
# File 'lib/ancestry/materialized_path2.rb', line 25
def descendants_by_ancestry(ancestry)
arel_table[ancestry_column].matches("#{ancestry}%", nil, true)
end
|
#generate_ancestry(ancestor_ids) ⇒ Object
41
42
43
44
45
46
47
|
# File 'lib/ancestry/materialized_path2.rb', line 41
def generate_ancestry(ancestor_ids)
if ancestor_ids.present? && ancestor_ids.any?
"#{ancestry_delimiter}#{ancestor_ids.join(ancestry_delimiter)}#{ancestry_delimiter}"
else
ancestry_root
end
end
|
#indirects_of(object) ⇒ Object
15
16
17
18
19
|
# File 'lib/ancestry/materialized_path2.rb', line 15
def indirects_of(object)
t = arel_table
node = to_node(object)
where(t[ancestry_column].matches("#{node.child_ancestry}%#{ancestry_delimiter}%", nil, true))
end
|
#ordered_by_ancestry(order = nil) ⇒ Object
21
22
23
|
# File 'lib/ancestry/materialized_path2.rb', line 21
def ordered_by_ancestry(order = nil)
reorder(Arel::Nodes::Ascending.new(arel_table[ancestry_column]), order)
end
|