Skip to content

Commit

Permalink
slow removing ancestor(y) references
Browse files Browse the repository at this point in the history
goal is to have all methods relative to ancestor_ids

if possible names will contain ancestor_ids

then, we may be able to configure this to have multiple parents
  • Loading branch information
kbrock committed Jun 20, 2020
1 parent 0f6c677 commit eccb183
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
4 changes: 2 additions & 2 deletions lib/ancestry/has_ancestry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ def has_ancestry options = {}
self.counter_cache_column = options[:counter_cache]
end

after_create :increase_parent_counter_cache, if: :has_parent?
after_destroy :decrease_parent_counter_cache, if: :has_parent?
after_create :increase_parent_counter_cache, if: :ancestor_ids?
after_destroy :decrease_parent_counter_cache, if: :ancestor_ids?
after_update :update_parent_counter_cache
end

Expand Down
12 changes: 7 additions & 5 deletions lib/ancestry/instance_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,20 @@ def _counter_cache_column

# Ancestors

def ancestors?
def ancestor_ids?
ancestor_ids.present?
end
alias :has_parent? :ancestors?
alias :parent_id? :ancestors?
# deprecate ancestors? and probably has_parent?
alias :ancestors? :ancestor_ids?
alias :has_parent? :ancestor_ids?
alias :parent_id? :ancestor_ids?

def sane_ancestor_ids?
valid? || errors[self.ancestry_base_class.ancestry_column].blank?
end

def ancestors depth_options = {}
return self.ancestry_base_class.none unless ancestors?
return self.ancestry_base_class.none unless ancestor_ids?
self.ancestry_base_class.scope_depth(depth_options, depth).ordered_by_ancestry.ancestors_of(self)
end

Expand Down Expand Up @@ -190,7 +192,7 @@ def has_children?
alias_method :children?, :has_children?

def is_childless?
!has_children?
!children?
end
alias_method :childless?, :is_childless?

Expand Down
14 changes: 7 additions & 7 deletions lib/ancestry/materialized_path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ def inpath_of(object)
def children_of(object)
t = arel_table
node = to_node(object)
where(t[ancestry_column].eq(node.child_ancestry))
where(t[ancestry_column].eq(node.child_ancestor_ids))
end

# indirect = anyone who is a descendant, but not a child
def indirects_of(object)
t = arel_table
node = to_node(object)
where(t[ancestry_column].matches(node.child_ancestry_str, nil, true))
where(t[ancestry_column].matches(node.child_ancestor_ids_str, nil, true))
end

def descendants_of(object)
Expand All @@ -47,7 +47,7 @@ def descendants_of(object)
def descendant_conditions(object)
t = arel_table
node = to_node(object)
t[ancestry_column].matches(node.child_ancestry_str, nil, true).or(t[ancestry_column].eq(node.child_ancestry))
t[ancestry_column].matches(node.child_ancestor_ids_str, nil, true).or(t[ancestry_column].eq(node.child_ancestor_ids))
end

def subtree_of(object)
Expand Down Expand Up @@ -80,16 +80,16 @@ def ordered_by_ancestry_and(order)
module InstanceMethods
# private (public so class methods can find it)
# The ancestry value for this record's children (before save)
# This is technically child_ancestry_was
def child_ancestry
# This is technically child_ancestor_ids_in_database
def child_ancestor_ids
# New records cannot have children
raise Ancestry::AncestryException.new('No child ancestry for new record. Save record before performing tree operations.') if new_record?
path_was = ancestor_ids_in_database
path_was + [id]
end

def child_ancestry_str
(child_ancestry + ['%']).join(ANCESTRY_DELIMITER)
def child_ancestor_ids_str
(child_ancestor_ids + ['%']).join(ANCESTRY_DELIMITER)
end
end
end
Expand Down

0 comments on commit eccb183

Please sign in to comment.