Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

base_class replace ancestry_base_class

  • Loading branch information...
commit 4e57f32e1758e36ca38c017bec5dd87b384f048a 1 parent 770f2e3
@NOX73 NOX73 authored
View
20 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.all(options)
@@ -74,7 +74,7 @@ def check_ancestry_integrity! options = {}
parents = {}
exceptions = [] if options[:report] == :list
# 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?
@@ -108,9 +108,9 @@ def check_ancestry_integrity! options = {}
def restore_ancestry_integrity!
parents = {}
# Wrap the whole thing in a transaction ...
- self.base_class.transaction do
+ self.ancestry_base_class.transaction 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
@@ -128,7 +128,7 @@ def restore_ancestry_integrity!
parents[node.id] = nil if parent == node.id
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?
@@ -143,7 +143,7 @@ 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.find_each(:conditions => {:parent_id => parent_id}) do |node|
+ self.ancestry_base_class.find_each(:conditions => {:parent_id => parent_id}) do |node|
node.without_ancestry_callbacks do
node.update_attribute ancestry_column, ancestry
end
@@ -154,7 +154,7 @@ def build_ancestry_from_parent_ids! parent_id = nil, ancestry = nil
# Rebuild depth cache if it got corrupted or if depth caching was just turned on
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.find_each do |node|
+ 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
@@ -27,8 +27,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
primary_key_format = options[:primary_key_format] || /[0-9]+/
View
58 lib/ancestry/instance_methods.rb
@@ -10,13 +10,13 @@ def update_descendants_with_new_ancestry
# Skip this if callbacks are disabled
unless ancestry_callbacks_disabled?
# If node is valid, not a new record and ancestry was updated ...
- if changed.include?(self.base_class.ancestry_column.to_s) && !new_record? && valid?
+ if changed.include?(self.ancestry_base_class.ancestry_column.to_s) && !new_record? && valid?
# ... for each descendant ...
descendants.each do |descendant|
# ... 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 al children root if orphan strategy is rootify
- if self.base_class.orphan_strategy == :rootify
+ if self.ancestry_base_class.orphan_strategy == :rootify
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
descendants.all.each do |descendant|
descendant.without_ancestry_callbacks do
descendant.destroy
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
@@ -61,20 +61,20 @@ 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 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.scoped :conditions => ancestor_conditions
+ self.ancestry_base_class.scope_depth(depth_options, depth).ordered_by_ancestry.scoped :conditions => ancestor_conditions
end
def path_ids
@@ -82,11 +82,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.scoped :conditions => path_conditions
+ self.ancestry_base_class.scope_depth(depth_options, depth).ordered_by_ancestry.scoped :conditions => path_conditions
end
def depth
@@ -94,16 +94,16 @@ 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.blank? then nil else parent.child_ancestry end)
+ write_attribute(self.ancestry_base_class.ancestry_column, if parent.blank? then nil else parent.child_ancestry end)
end
def parent_id= parent_id
- self.parent = if parent_id.blank? then nil else self.base_class.find(parent_id) end
+ self.parent = if parent_id.blank? then nil else self.ancestry_base_class.find(parent_id) end
end
def parent_id
@@ -111,7 +111,7 @@ def parent_id
end
def parent
- if parent_id.blank? then nil else self.base_class.find(parent_id) end
+ if parent_id.blank? then nil else self.ancestry_base_class.find(parent_id) end
end
# Root
@@ -120,24 +120,24 @@ def root_id
end
def root
- if root_id == id then self else self.base_class.find(root_id) end
+ if root_id == id then self else self.ancestry_base_class.find(root_id) end
end
def is_root?
- read_attribute(self.base_class.ancestry_column).blank?
+ read_attribute(self.ancestry_base_class.ancestry_column).blank?
end
# 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.scoped :conditions => child_conditions
+ self.ancestry_base_class.scoped :conditions => child_conditions
end
def child_ids
- children.all(:select => self.base_class.primary_key).map(&self.base_class.primary_key.to_sym)
+ children.all(:select => self.ancestry_base_class.primary_key).map(&self.ancestry_base_class.primary_key.to_sym)
end
def has_children?
@@ -150,15 +150,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.scoped :conditions => sibling_conditions
+ self.ancestry_base_class.scoped :conditions => sibling_conditions
end
def sibling_ids
- siblings.all(:select => self.base_class.primary_key).collect(&self.base_class.primary_key.to_sym)
+ siblings.all(:select => self.ancestry_base_class.primary_key).collect(&self.ancestry_base_class.primary_key.to_sym)
end
def has_siblings?
@@ -171,28 +171,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).scoped :conditions => descendant_conditions
+ self.ancestry_base_class.ordered_by_ancestry.scope_depth(depth_options, depth).scoped :conditions => descendant_conditions
end
def descendant_ids depth_options = {}
- descendants(depth_options).all(:select => self.base_class.primary_key).collect(&self.base_class.primary_key.to_sym)
+ descendants(depth_options).all(: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).scoped :conditions => subtree_conditions
+ self.ancestry_base_class.ordered_by_ancestry.scope_depth(depth_options, depth).scoped :conditions => subtree_conditions
end
def subtree_ids depth_options = {}
- subtree(depth_options).all(:select => self.base_class.primary_key).collect(&self.base_class.primary_key.to_sym)
+ subtree(depth_options).all(:select => self.ancestry_base_class.primary_key).collect(&self.ancestry_base_class.primary_key.to_sym)
end
# Callback disabling
Please sign in to comment.
Something went wrong with that request. Please try again.