Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'master' of git://github.com/kaize/ancestry into kaize-m…

…aster

Conflicts:
	lib/ancestry/class_methods.rb
	lib/ancestry/instance_methods.rb
  • Loading branch information...
commit a759e6b726e1d643f1b3d6d7742751a7d066fea6 2 parents 1e523ae + 4e57f32
Stefan Henzen authored
View
28 lib/ancestry/class_methods.rb
@@ -2,12 +2,12 @@ module Ancestry
module ClassMethods
# Fetch tree node if necessary
def to_node object
- if object.is_a?(self.base_class) then object else find(object) end
+ if object.is_a?(self.ancestry_base_class) then object else find(object) end
end
# Scope on relative depth options
def scope_depth depth_options, depth
- depth_options.inject(self.base_class) do |scope, option|
+ depth_options.inject(self.ancestry_base_class) do |scope, option|
scope_name, relative_depth = option
if [:before_depth, :to_depth, :at_depth, :from_depth, :after_depth].include? scope_name
scope.send scope_name, depth + relative_depth
@@ -31,9 +31,9 @@ def orphan_strategy= orphan_strategy
def arrange options = {}
scope =
if options[:order].nil?
- self.base_class.ordered_by_ancestry
+ self.ancestry_base_class.ordered_by_ancestry
else
- self.base_class.ordered_by_ancestry_and options.delete(:order)
+ self.ancestry_base_class.ordered_by_ancestry_and options.delete(:order)
end
# Get all nodes ordered by ancestry and start sorting them into an empty hash
arrange_nodes scope.where(options)
@@ -88,9 +88,9 @@ def check_ancestry_integrity! options = {}
parents = {}
exceptions = [] if options[:report] == :list
- self.base_class.unscoped do
+ self.ancestry_base_class.unscoped do
# For each node ...
- self.base_class.find_each do |node|
+ self.ancestry_base_class.find_each do |node|
begin
# ... check validity of ancestry column
if !node.valid? and !node.errors[node.class.ancestry_column].blank?
@@ -125,10 +125,10 @@ def check_ancestry_integrity! options = {}
def restore_ancestry_integrity!
parents = {}
# Wrap the whole thing in a transaction ...
- self.base_class.transaction do
- self.base_class.unscoped do
+ self.ancestry_base_class.transaction do
+ self.ancestry_base_class.unscoped do
# For each node ...
- self.base_class.find_each do |node|
+ self.ancestry_base_class.find_each do |node|
# ... set its ancestry to nil if invalid
if !node.valid? and !node.errors[node.class.ancestry_column].blank?
node.without_ancestry_callbacks do
@@ -147,7 +147,7 @@ def restore_ancestry_integrity!
end
# For each node ...
- self.base_class.find_each do |node|
+ self.ancestry_base_class.find_each do |node|
# ... rebuild ancestry from parents array
ancestry, parent = nil, parents[node.id]
until parent.nil?
@@ -163,8 +163,8 @@ def restore_ancestry_integrity!
# Build ancestry from parent id's for migration purposes
def build_ancestry_from_parent_ids! parent_id = nil, ancestry = nil
- self.base_class.unscoped do
- self.base_class.where(:parent_id => parent_id).find_each do |node|
+ self.ancestry_base_class.unscoped do
+ self.ancestry_base_class.where(:parent_id => parent_id).find_each do |node|
node.without_ancestry_callbacks do
node.update_attribute ancestry_column, ancestry
end
@@ -177,8 +177,8 @@ def build_ancestry_from_parent_ids! parent_id = nil, ancestry = nil
def rebuild_depth_cache!
raise Ancestry::AncestryException.new("Cannot rebuild depth cache for model without depth caching.") unless respond_to? :depth_cache_column
- self.base_class.unscoped do
- self.base_class.find_each do |node|
+ self.ancestry_base_class.unscoped do
+ self.ancestry_base_class.find_each do |node|
node.update_attribute depth_cache_column, node.depth
end
end
View
4 lib/ancestry/has_ancestry.rb
@@ -23,8 +23,8 @@ def has_ancestry options = {}
self.orphan_strategy = options[:orphan_strategy] || :destroy
# Save self as base class (for STI)
- cattr_accessor :base_class
- self.base_class = self
+ cattr_accessor :ancestry_base_class
+ self.ancestry_base_class = self
# Validate format of ancestry column value
validates_format_of ancestry_column, :with => Ancestry::ANCESTRY_PATTERN, :allow_nil => true
View
60 lib/ancestry/instance_methods.rb
@@ -16,7 +16,7 @@ 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,
+ self.ancestry_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
@@ -35,21 +35,21 @@ def apply_orphan_strategy
# If this isn't a new record ...
unless new_record?
# ... make all children root if orphan strategy is rootify
- if self.base_class.orphan_strategy == :rootify
+ if self.ancestry_base_class.orphan_strategy == :rootify
unscoped_descendants.each do |descendant|
descendant.without_ancestry_callbacks do
descendant.update_attribute descendant.class.ancestry_column, (if descendant.ancestry == child_ancestry then nil else descendant.ancestry.gsub(/^#{child_ancestry}\//, '') end)
end
end
# ... destroy all descendants if orphan strategy is destroy
- elsif self.base_class.orphan_strategy == :destroy
+ elsif self.ancestry_base_class.orphan_strategy == :destroy
unscoped_descendants.each do |descendant|
descendant.without_ancestry_callbacks do
descendant.destroy
end
end
# ... make child elements of this node, child of its parent if orphan strategy is adopt
- elsif self.base_class.orphan_strategy == :adopt
+ elsif self.ancestry_base_class.orphan_strategy == :adopt
descendants.each do |descendant|
descendant.without_ancestry_callbacks do
new_ancestry = descendant.ancestor_ids.delete_if { |x| x == self.id }.join("/")
@@ -57,7 +57,7 @@ def apply_orphan_strategy
end
end
# ... throw an exception if it has children and orphan strategy is restrict
- elsif self.base_class.orphan_strategy == :restrict
+ elsif self.ancestry_base_class.orphan_strategy == :restrict
raise Ancestry::AncestryException.new('Cannot delete record because it has descendants.') unless is_childless?
end
end
@@ -69,24 +69,24 @@ 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
+ if self.send("#{self.ancestry_base_class.ancestry_column}_was").blank? then id.to_s else "#{self.send "#{self.ancestry_base_class.ancestry_column}_was"}/#{id}" end
end
# Ancestors
def ancestry_changed?
- changed.include?(self.base_class.ancestry_column.to_s)
+ changed.include?(self.ancestry_base_class.ancestry_column.to_s)
end
def ancestor_ids
- read_attribute(self.base_class.ancestry_column).to_s.split('/').map { |id| cast_primary_key(id) }
+ read_attribute(self.ancestry_base_class.ancestry_column).to_s.split('/').map { |id| cast_primary_key(id) }
end
def ancestor_conditions
- {self.base_class.primary_key => ancestor_ids}
+ {self.ancestry_base_class.primary_key => ancestor_ids}
end
def ancestors depth_options = {}
- self.base_class.scope_depth(depth_options, depth).ordered_by_ancestry.where ancestor_conditions
+ self.ancestry_base_class.scope_depth(depth_options, depth).ordered_by_ancestry.where ancestor_conditions
end
def path_ids
@@ -94,11 +94,11 @@ def path_ids
end
def path_conditions
- {self.base_class.primary_key => path_ids}
+ {self.ancestry_base_class.primary_key => path_ids}
end
def path depth_options = {}
- self.base_class.scope_depth(depth_options, depth).ordered_by_ancestry.where path_conditions
+ self.ancestry_base_class.scope_depth(depth_options, depth).ordered_by_ancestry.where path_conditions
end
def depth
@@ -106,12 +106,12 @@ def depth
end
def cache_depth
- write_attribute self.base_class.depth_cache_column, depth
+ write_attribute self.ancestry_base_class.depth_cache_column, depth
end
# Parent
def parent= parent
- write_attribute(self.base_class.ancestry_column, if parent.nil? then nil else parent.child_ancestry end)
+ write_attribute(self.ancestry_base_class.ancestry_column, if parent.nil? then nil else parent.child_ancestry end)
end
def parent_id= parent_id
@@ -140,21 +140,21 @@ def root
end
def is_root?
- read_attribute(self.base_class.ancestry_column).blank?
+ read_attribute(self.ancestry_base_class.ancestry_column).blank?
end
alias :root? :is_root?
# Children
def child_conditions
- {self.base_class.ancestry_column => child_ancestry}
+ {self.ancestry_base_class.ancestry_column => child_ancestry}
end
def children
- self.base_class.where child_conditions
+ self.ancestry_base_class.where child_conditions
end
def child_ids
- children.select(self.base_class.primary_key).map(&self.base_class.primary_key.to_sym)
+ children.select(self.ancestry_base_class.primary_key).map(&self.ancestry_base_class.primary_key.to_sym)
end
def has_children?
@@ -167,15 +167,15 @@ def is_childless?
# Siblings
def sibling_conditions
- {self.base_class.ancestry_column => read_attribute(self.base_class.ancestry_column)}
+ {self.ancestry_base_class.ancestry_column => read_attribute(self.ancestry_base_class.ancestry_column)}
end
def siblings
- self.base_class.where sibling_conditions
+ self.ancestry_base_class.where sibling_conditions
end
def sibling_ids
- siblings.select(self.base_class.primary_key).collect(&self.base_class.primary_key.to_sym)
+ siblings.select(self.ancestry_base_class.primary_key).collect(&self.ancestry_base_class.primary_key.to_sym)
end
def has_siblings?
@@ -188,28 +188,28 @@ def is_only_child?
# Descendants
def descendant_conditions
- ["#{self.base_class.table_name}.#{self.base_class.ancestry_column} like ? or #{self.base_class.table_name}.#{self.base_class.ancestry_column} = ?", "#{child_ancestry}/%", child_ancestry]
+ ["#{self.ancestry_base_class.table_name}.#{self.ancestry_base_class.ancestry_column} like ? or #{self.ancestry_base_class.table_name}.#{self.ancestry_base_class.ancestry_column} = ?", "#{child_ancestry}/%", child_ancestry]
end
def descendants depth_options = {}
- self.base_class.ordered_by_ancestry.scope_depth(depth_options, depth).where descendant_conditions
+ self.ancestry_base_class.ordered_by_ancestry.scope_depth(depth_options, depth).where descendant_conditions
end
def descendant_ids depth_options = {}
- descendants(depth_options).select(self.base_class.primary_key).collect(&self.base_class.primary_key.to_sym)
+ descendants(depth_options).select(self.ancestry_base_class.primary_key).collect(&self.ancestry_base_class.primary_key.to_sym)
end
# Subtree
def subtree_conditions
- ["#{self.base_class.table_name}.#{self.base_class.primary_key} = ? or #{self.base_class.table_name}.#{self.base_class.ancestry_column} like ? or #{self.base_class.table_name}.#{self.base_class.ancestry_column} = ?", self.id, "#{child_ancestry}/%", child_ancestry]
+ ["#{self.ancestry_base_class.table_name}.#{self.ancestry_base_class.primary_key} = ? or #{self.ancestry_base_class.table_name}.#{self.ancestry_base_class.ancestry_column} like ? or #{self.ancestry_base_class.table_name}.#{self.ancestry_base_class.ancestry_column} = ?", self.id, "#{child_ancestry}/%", child_ancestry]
end
def subtree depth_options = {}
- self.base_class.ordered_by_ancestry.scope_depth(depth_options, depth).where subtree_conditions
+ self.ancestry_base_class.ordered_by_ancestry.scope_depth(depth_options, depth).where subtree_conditions
end
def subtree_ids depth_options = {}
- subtree(depth_options).select(self.base_class.primary_key).collect(&self.base_class.primary_key.to_sym)
+ subtree(depth_options).select(self.ancestry_base_class.primary_key).collect(&self.ancestry_base_class.primary_key.to_sym)
end
# Callback disabling
@@ -237,8 +237,8 @@ def primary_key_type
@primary_key_type ||= column_for_attribute(self.class.primary_key).type
end
def unscoped_descendants
- self.base_class.unscoped do
- self.base_class.where descendant_conditions
+ self.ancestry_base_class.unscoped do
+ self.ancestry_base_class.where descendant_conditions
end
end
@@ -249,7 +249,7 @@ def sane_ancestry?
end
def unscoped_find id
- self.base_class.unscoped { self.base_class.find(id) }
+ self.ancestry_base_class.unscoped { self.ancestry_base_class.find(id) }
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.