Permalink
Browse files

First Commit

Added Functionality of Parentify.

If a node is deleted, all descendants are added to the parent
of the deleted node.
  • Loading branch information...
1 parent 770f2e3 commit aec25d11a267011cd487bd9b003d6eff73e4439d unknown committed Sep 8, 2011
Showing with 21 additions and 13 deletions.
  1. +3 −3 lib/ancestry/class_methods.rb
  2. +18 −10 lib/ancestry/instance_methods.rb
View
6 lib/ancestry/class_methods.rb
@@ -19,11 +19,11 @@ def scope_depth depth_options, depth
# Orphan strategy writer
def orphan_strategy= orphan_strategy
- # Check value of orphan strategy, only rootify, restrict or destroy is allowed
- if [:rootify, :restrict, :destroy].include? orphan_strategy
+ # Check value of orphan strategy, only rootify, parentify, restrict or destroy is allowed
+ if [:rootify, :parentify, :restrict, :destroy].include? orphan_strategy
class_variable_set :@@orphan_strategy, orphan_strategy
else
- raise Ancestry::AncestryException.new("Invalid orphan strategy, valid ones are :rootify, :restrict and :destroy.")
+ raise Ancestry::AncestryException.new("Invalid orphan strategy, valid ones are :rootify,:parentify, :restrict and :destroy.")
end
end
View
28 lib/ancestry/instance_methods.rb
@@ -16,18 +16,18 @@ def update_descendants_with_new_ancestry
# ... replace old ancestry with new ancestry
descendant.without_ancestry_callbacks do
descendant.update_attribute(
- self.base_class.ancestry_column,
- descendant.read_attribute(descendant.class.ancestry_column).gsub(
- /^#{self.child_ancestry}/,
- if read_attribute(self.class.ancestry_column).blank? then id.to_s else "#{read_attribute self.class.ancestry_column }/#{id}" end
- )
+ self.base_class.ancestry_column,
+ descendant.read_attribute(descendant.class.ancestry_column).gsub(
+ /^#{self.child_ancestry}/,
+ if read_attribute(self.class.ancestry_column).blank? then id.to_s else "#{read_attribute self.class.ancestry_column }/#{id}" end
+ )
)
end
end
end
end
end
-
+
# Apply orphan strategy
def apply_orphan_strategy
# Skip this if callbacks are disabled
@@ -48,23 +48,30 @@ def apply_orphan_strategy
descendant.destroy
end
end
- # ... throw an exception if it has children and orphan strategy is restrict
+ # ... make child elements of this node, child of its parent if orphan strategy is parentify
+ elsif self.base_class.orphan_strategy == :parentify
+ descendants.all.each do |descendant|
+ descendant.without_ancestry_callbacks do
+ descendant.update_attribute descendant.class.ancestry_column, descendant.ancestor_ids.delete_if { |x| x == self.id }.join("/")
+ end
+ end
+ # ... throw an exception if it has children and orphan strategy is restrict
elsif self.base_class.orphan_strategy == :restrict
raise Ancestry::AncestryException.new('Cannot delete record because it has descendants.') unless is_childless?
end
end
end
end
- # The ancestry value for this record's children
+ # The ancestry value for this record's children
def child_ancestry
# New records cannot have children
raise Ancestry::AncestryException.new('No child ancestry for new record. Save record before performing tree operations.') if new_record?
if self.send("#{self.base_class.ancestry_column}_was").blank? then id.to_s else "#{self.send "#{self.base_class.ancestry_column}_was"}/#{id}" end
end
- # Ancestors
+ # Ancestors
def ancestor_ids
read_attribute(self.base_class.ancestry_column).to_s.split('/').map { |id| cast_primary_key(id) }
end
@@ -158,7 +165,7 @@ def siblings
end
def sibling_ids
- siblings.all(:select => self.base_class.primary_key).collect(&self.base_class.primary_key.to_sym)
+ siblings.all(:select => self.base_class.primary_key).collect(&self.base_class.primary_key.to_sym)
end
def has_siblings?
@@ -228,5 +235,6 @@ def cast_primary_key(key)
def primary_key_type
@primary_key_type ||= column_for_attribute(self.class.primary_key).type
end
+
end
end

0 comments on commit aec25d1

Please sign in to comment.