Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 163 lines (152 sloc) 6.903 kb
ecea76f @stefankroes Version 1.2.0
authored
1 module Ancestry
2 module ClassMethods
3 # Fetch tree node if necessary
4 def to_node object
4e57f32 @NOX73 base_class replace ancestry_base_class
NOX73 authored
5 if object.is_a?(self.ancestry_base_class) then object else find(object) end
ecea76f @stefankroes Version 1.2.0
authored
6 end
7
8 # Scope on relative depth options
9 def scope_depth depth_options, depth
4e57f32 @NOX73 base_class replace ancestry_base_class
NOX73 authored
10 depth_options.inject(self.ancestry_base_class) do |scope, option|
ecea76f @stefankroes Version 1.2.0
authored
11 scope_name, relative_depth = option
12 if [:before_depth, :to_depth, :at_depth, :from_depth, :after_depth].include? scope_name
13 scope.send scope_name, depth + relative_depth
14 else
15 raise Ancestry::AncestryException.new("Unknown depth option: #{scope_name}.")
16 end
17 end
18 end
19
20 # Orphan strategy writer
21 def orphan_strategy= orphan_strategy
22 # Check value of orphan strategy, only rootify, restrict or destroy is allowed
23 if [:rootify, :restrict, :destroy].include? orphan_strategy
24 class_variable_set :@@orphan_strategy, orphan_strategy
25 else
26 raise Ancestry::AncestryException.new("Invalid orphan strategy, valid ones are :rootify, :restrict and :destroy.")
27 end
28 end
29
30 # Arrangement
31 def arrange options = {}
32 scope =
33 if options[:order].nil?
4e57f32 @NOX73 base_class replace ancestry_base_class
NOX73 authored
34 self.ancestry_base_class.ordered_by_ancestry
ecea76f @stefankroes Version 1.2.0
authored
35 else
4e57f32 @NOX73 base_class replace ancestry_base_class
NOX73 authored
36 self.ancestry_base_class.ordered_by_ancestry_and options.delete(:order)
ecea76f @stefankroes Version 1.2.0
authored
37 end
38 # Get all nodes ordered by ancestry and start sorting them into an empty hash
5614a3b @kueda Added class method to sort nodes by ancestry.
kueda authored
39 arrange_nodes scope.all(options)
40 end
41
42 # Arrange array of nodes into a nested hash of the form
43 # {node => children}, where children = {} if the node has no children
44 def arrange_nodes(nodes)
45 # Get all nodes ordered by ancestry and start sorting them into an empty hash
46 nodes.inject(ActiveSupport::OrderedHash.new) do |arranged_nodes, node|
ecea76f @stefankroes Version 1.2.0
authored
47 # Find the insertion point for that node by going through its ancestors
48 node.ancestor_ids.inject(arranged_nodes) do |insertion_point, ancestor_id|
49 insertion_point.each do |parent, children|
50 # Change the insertion point to children if node is a descendant of this parent
51 insertion_point = children if ancestor_id == parent.id
5614a3b @kueda Added class method to sort nodes by ancestry.
kueda authored
52 end
53 insertion_point
54 end[node] = ActiveSupport::OrderedHash.new
55 arranged_nodes
56 end
57 end
58
59 # Pseudo-preordered array of nodes. Children will always follow parents,
60 # but the ordering of nodes within a rank depends on their order in the
61 # array that gets passed in
62 def sort_by_ancestry(nodes)
63 arranged = nodes.is_a?(Hash) ? nodes : arrange_nodes(nodes.sort_by{|n| n.ancestry || '0'})
64 arranged.inject([]) do |sorted_nodes, pair|
65 node, children = pair
66 sorted_nodes << node
67 sorted_nodes += sort_by_ancestry(children) unless children.blank?
68 sorted_nodes
ecea76f @stefankroes Version 1.2.0
authored
69 end
70 end
71
72 # Integrity checking
34a13a9 @stefankroes Fixes for 1.2.1
authored
73 def check_ancestry_integrity! options = {}
ecea76f @stefankroes Version 1.2.0
authored
74 parents = {}
34a13a9 @stefankroes Fixes for 1.2.1
authored
75 exceptions = [] if options[:report] == :list
ecea76f @stefankroes Version 1.2.0
authored
76 # For each node ...
4e57f32 @NOX73 base_class replace ancestry_base_class
NOX73 authored
77 self.ancestry_base_class.find_each do |node|
34a13a9 @stefankroes Fixes for 1.2.1
authored
78 begin
79 # ... check validity of ancestry column
80 if !node.valid? and !node.errors[node.class.ancestry_column].blank?
81 raise Ancestry::AncestryIntegrityException.new "Invalid format for ancestry column of node #{node.id}: #{node.read_attribute node.ancestry_column}."
ecea76f @stefankroes Version 1.2.0
authored
82 end
34a13a9 @stefankroes Fixes for 1.2.1
authored
83 # ... check that all ancestors exist
84 node.ancestor_ids.each do |ancestor_id|
85 unless exists? ancestor_id
86 raise Ancestry::AncestryIntegrityException.new "Reference to non-existent node in node #{node.id}: #{ancestor_id}."
87 end
88 end
89 # ... check that all node parents are consistent with values observed earlier
90 node.path_ids.zip([nil] + node.path_ids).each do |node_id, parent_id|
91 parents[node_id] = parent_id unless parents.has_key? node_id
92 unless parents[node_id] == parent_id
93 raise Ancestry::AncestryIntegrityException.new "Conflicting parent id found in node #{node.id}: #{parent_id || 'nil'} for node #{node_id} while expecting #{parents[node_id] || 'nil'}"
94 end
95 end
96 rescue Ancestry::AncestryIntegrityException => integrity_exception
97 case options[:report]
98 when :list then exceptions << integrity_exception
99 when :echo then puts integrity_exception
100 else raise integrity_exception
ecea76f @stefankroes Version 1.2.0
authored
101 end
102 end
103 end
34a13a9 @stefankroes Fixes for 1.2.1
authored
104 exceptions if options[:report] == :list
ecea76f @stefankroes Version 1.2.0
authored
105 end
106
107 # Integrity restoration
108 def restore_ancestry_integrity!
109 parents = {}
770f2e3 Fix bug in restore_ancestry_integrity!, added test to verify we didn't r...
Arthur Holstvoogd authored
110 # Wrap the whole thing in a transaction ...
4e57f32 @NOX73 base_class replace ancestry_base_class
NOX73 authored
111 self.ancestry_base_class.transaction do
770f2e3 Fix bug in restore_ancestry_integrity!, added test to verify we didn't r...
Arthur Holstvoogd authored
112 # For each node ...
4e57f32 @NOX73 base_class replace ancestry_base_class
NOX73 authored
113 self.ancestry_base_class.find_each do |node|
770f2e3 Fix bug in restore_ancestry_integrity!, added test to verify we didn't r...
Arthur Holstvoogd authored
114 # ... set its ancestry to nil if invalid
115 if !node.valid? and !node.errors[node.class.ancestry_column].blank?
116 node.without_ancestry_callbacks do
117 node.update_attribute node.ancestry_column, nil
118 end
ecea76f @stefankroes Version 1.2.0
authored
119 end
770f2e3 Fix bug in restore_ancestry_integrity!, added test to verify we didn't r...
Arthur Holstvoogd authored
120 # ... save parent of this node in parents array if it exists
121 parents[node.id] = node.parent_id if exists? node.parent_id
ecea76f @stefankroes Version 1.2.0
authored
122
770f2e3 Fix bug in restore_ancestry_integrity!, added test to verify we didn't r...
Arthur Holstvoogd authored
123 # Reset parent id in array to nil if it introduces a cycle
124 parent = parents[node.id]
125 until parent.nil? || parent == node.id
126 parent = parents[parent]
127 end
128 parents[node.id] = nil if parent == node.id
ecea76f @stefankroes Version 1.2.0
authored
129 end
770f2e3 Fix bug in restore_ancestry_integrity!, added test to verify we didn't r...
Arthur Holstvoogd authored
130 # For each node ...
4e57f32 @NOX73 base_class replace ancestry_base_class
NOX73 authored
131 self.ancestry_base_class.find_each do |node|
770f2e3 Fix bug in restore_ancestry_integrity!, added test to verify we didn't r...
Arthur Holstvoogd authored
132 # ... rebuild ancestry from parents array
133 ancestry, parent = nil, parents[node.id]
134 until parent.nil?
135 ancestry, parent = if ancestry.nil? then parent else "#{parent}/#{ancestry}" end, parents[parent]
136 end
137 node.without_ancestry_callbacks do
138 node.update_attribute node.ancestry_column, ancestry
139 end
ecea76f @stefankroes Version 1.2.0
authored
140 end
141 end
142 end
143
144 # Build ancestry from parent id's for migration purposes
145 def build_ancestry_from_parent_ids! parent_id = nil, ancestry = nil
4e57f32 @NOX73 base_class replace ancestry_base_class
NOX73 authored
146 self.ancestry_base_class.find_each(:conditions => {:parent_id => parent_id}) do |node|
ecea76f @stefankroes Version 1.2.0
authored
147 node.without_ancestry_callbacks do
148 node.update_attribute ancestry_column, ancestry
149 end
150 build_ancestry_from_parent_ids! node.id, if ancestry.nil? then "#{node.id}" else "#{ancestry}/#{node.id}" end
151 end
152 end
153
154 # Rebuild depth cache if it got corrupted or if depth caching was just turned on
155 def rebuild_depth_cache!
156 raise Ancestry::AncestryException.new("Cannot rebuild depth cache for model without depth caching.") unless respond_to? :depth_cache_column
4e57f32 @NOX73 base_class replace ancestry_base_class
NOX73 authored
157 self.ancestry_base_class.find_each do |node|
ecea76f @stefankroes Version 1.2.0
authored
158 node.update_attribute depth_cache_column, node.depth
159 end
160 end
161 end
5614a3b @kueda Added class method to sort nodes by ancestry.
kueda authored
162 end
Something went wrong with that request. Please try again.