acts_as_dag 1.1.0 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/acts_as_dag/acts_as_dag.rb +14 -14
- metadata +1 -1
@@ -101,14 +101,14 @@ module ActsAsDAG
|
|
101
101
|
suitable_parent = true if ActsAsDAG::HelperMethods.plinko(root, category)
|
102
102
|
end
|
103
103
|
unless suitable_parent
|
104
|
-
logger.info "Plinko couldn't find a suitable parent for #{category.name} in #{categories.collect(&:name).join(', ')}"
|
104
|
+
ActiveRecord::Base.logger.info "Plinko couldn't find a suitable parent for #{category.name} in #{categories.collect(&:name).join(', ')}"
|
105
105
|
categories_with_no_parents << category
|
106
106
|
end
|
107
107
|
end
|
108
108
|
|
109
109
|
# Add all categories from this group without suitable parents to the roots
|
110
110
|
if categories_with_no_parents.present?
|
111
|
-
logger.info "Adding #{categories_with_no_parents.collect(&:name).join(', ')} to roots"
|
111
|
+
ActiveRecord::Base.logger.info "Adding #{categories_with_no_parents.collect(&:name).join(', ')} to roots"
|
112
112
|
roots_categories.concat categories_with_no_parents
|
113
113
|
end
|
114
114
|
end
|
@@ -119,10 +119,10 @@ module ActsAsDAG
|
|
119
119
|
def reset_hierarchy(categories_to_reset = self.all)
|
120
120
|
ids = categories_to_reset.collect(&:id)
|
121
121
|
|
122
|
-
logger.info "Clearing #{self.name} hierarchy links"
|
122
|
+
ActiveRecord::Base.logger.info "Clearing #{self.name} hierarchy links"
|
123
123
|
link_table_entries.where("parent_id IN (?) OR child_id IN (?)", ids, ids).delete_all
|
124
124
|
|
125
|
-
logger.info "Clearing #{self.name} hierarchy descendants"
|
125
|
+
ActiveRecord::Base.logger.info "Clearing #{self.name} hierarchy descendants"
|
126
126
|
descendant_table_entries.where("descendant_id IN (?) OR ancestor_id IN (?)", ids, ids).delete_all
|
127
127
|
|
128
128
|
categories_to_reset.each do |category|
|
@@ -198,7 +198,7 @@ module ActsAsDAG
|
|
198
198
|
# Searches all descendants for the best parent for the other
|
199
199
|
# i.e. it lets you drop the category in at the top and it drops down the list until it finds its final resting place
|
200
200
|
def self.plinko(current, other)
|
201
|
-
|
201
|
+
ActiveRecord::Base.logger.info "Plinkoing '#{other.name}' into '#{current.name}'..."
|
202
202
|
if should_descend_from?(current, other)
|
203
203
|
# Find the descendants of the current category that +other+ should descend from
|
204
204
|
descendants_other_should_descend_from = current.descendants.select{|descendant| should_descend_from?(descendant, other)}
|
@@ -208,7 +208,7 @@ module ActsAsDAG
|
|
208
208
|
new_parents_group = descendants_other_should_descend_from.group_by{|category| matching_word_count(other, category)}.sort.reverse.first
|
209
209
|
if new_parents_group.present?
|
210
210
|
for new_parent in new_parents_group[1]
|
211
|
-
|
211
|
+
ActiveRecord::Base.logger.info " '#{other.name}' landed under '#{new_parent.name}'"
|
212
212
|
other.add_parent(new_parent)
|
213
213
|
|
214
214
|
# We've just affected the associations in ways we can not possibly imagine, so let's clear the association cache
|
@@ -268,7 +268,7 @@ module ActsAsDAG
|
|
268
268
|
# creates a single link in the given link_class's link table between parent and
|
269
269
|
# child object ids and creates the appropriate entries in the descendant table
|
270
270
|
def self.link(parent, child)
|
271
|
-
# logger.info "link(hierarchy_link_table = #{child.link_class}, hierarchy_descendant_table = #{child.descendant_class}, parent = #{parent.name}, child = #{child.name})"
|
271
|
+
# ActiveRecord::Base.logger.info "link(hierarchy_link_table = #{child.link_class}, hierarchy_descendant_table = #{child.descendant_class}, parent = #{parent.name}, child = #{child.name})"
|
272
272
|
|
273
273
|
# Sanity check
|
274
274
|
raise "Parent has no ID" if parent.id.nil?
|
@@ -280,7 +280,7 @@ module ActsAsDAG
|
|
280
280
|
# Create a new parent-child link
|
281
281
|
# Return if the link already exists because we can assume that the proper descendants already exist too
|
282
282
|
if klass.link_table_entries.where(:parent_id => parent.id, :child_id => child.id).exists?
|
283
|
-
logger.info "Skipping #{child.descendant_class} update because the link already exists"
|
283
|
+
ActiveRecord::Base.logger.info "Skipping #{child.descendant_class} update because the link already exists"
|
284
284
|
return
|
285
285
|
else
|
286
286
|
klass.link_table_entries.create!(:parent_id => parent.id, :child_id => child.id)
|
@@ -307,7 +307,7 @@ module ActsAsDAG
|
|
307
307
|
# child object id. Updates the appropriate Descendants table entries
|
308
308
|
def self.unlink(parent, child)
|
309
309
|
descendant_table_string = child.descendant_class.to_s
|
310
|
-
#
|
310
|
+
# ActiveRecord::Base.logger.info "unlink(hierarchy_link_table = #{child.link_class}, hierarchy_descendant_table = #{descendant_table_string}, parent = #{parent ? parent.name : 'nil'}, child = #{child.name})"
|
311
311
|
|
312
312
|
# Raise an exception if there is no child
|
313
313
|
raise "Child cannot be nil when deleting a category_link" unless child
|
@@ -335,7 +335,7 @@ module ActsAsDAG
|
|
335
335
|
# Now iterate through all ancestors of the descendant_links that were deleted and pick only those that have no parents, namely (A, D)
|
336
336
|
# These will be the starting points for the recreation of descendant links
|
337
337
|
starting_points = klass.find(parent.ancestor_ids + child.descendant_ids).select{|node| node.parents.empty? || node.parents == [nil] }
|
338
|
-
|
338
|
+
ActiveRecord::Base.logger.info {"starting points are #{starting_points.collect(&:name).to_sentence}" }
|
339
339
|
|
340
340
|
# POSSIBLE OPTIMIZATION: The two starting points may share descendants. We only need to process each node once, so if we could skip dups, that would be good
|
341
341
|
starting_points.each{|node| rebuild_descendant_links(node)}
|
@@ -349,22 +349,22 @@ module ActsAsDAG
|
|
349
349
|
indent = Array.new(ancestors.size, " ").join
|
350
350
|
klass = current.class
|
351
351
|
|
352
|
-
|
352
|
+
ActiveRecord::Base.logger.info {"#{indent}Rebuilding descendant links of #{current.name}"}
|
353
353
|
# Add current to the list of traversed nodes that we will pass to the children we decide to recurse to
|
354
354
|
ancestors << current
|
355
355
|
|
356
356
|
# Create descendant links to each ancestor in the array (including itself)
|
357
357
|
ancestors.reverse.each_with_index do |ancestor, index|
|
358
|
-
|
358
|
+
ActiveRecord::Base.logger.info {"#{indent}#{ancestor.name} is an ancestor of #{current.name} with distance #{index}"}
|
359
359
|
klass.descendant_table_entries.where(:ancestor_id => ancestor.id, :descendant_id => current.id, :distance => index).first_or_create!
|
360
360
|
end
|
361
361
|
|
362
362
|
# Now check each child to see if it is a descendant, or if we need to recurse
|
363
363
|
for child in current.children
|
364
|
-
|
364
|
+
ActiveRecord::Base.logger.info {"#{indent}Recursing to #{child.name}"}
|
365
365
|
rebuild_descendant_links(child, ancestors.dup)
|
366
366
|
end
|
367
|
-
|
367
|
+
ActiveRecord::Base.logger.info {"#{indent}Done recursing"}
|
368
368
|
end
|
369
369
|
end
|
370
370
|
|