Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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
unknown authored and unknown committed Sep 8, 2011
1 parent 770f2e3 commit aec25d1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
6 changes: 3 additions & 3 deletions lib/ancestry/class_methods.rb
Expand Up @@ -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

Expand Down
28 changes: 18 additions & 10 deletions lib/ancestry/instance_methods.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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?
Expand Down Expand Up @@ -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.