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 Oct 9, 2020
1 parent 8518a8d commit f10573d
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 9 deletions.
4 changes: 2 additions & 2 deletions lib/ancestry/has_ancestry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,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
8 changes: 5 additions & 3 deletions lib/ancestry/instance_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,13 @@ def _counter_cache_column

# Ancestors

def ancestors?
# when field is removed, this will end up back at ancestors
def ancestor_ids?
ancestor_ids.present?
end
alias :has_parent? :ancestors?
alias :parent_id? :ancestors?
alias :ancestors? :ancestor_ids?
alias :has_parent? :ancestor_ids?
alias :parent_id? :ancestor_ids?

def will_save_change_to_ancestor_ids?
column = self.ancestry_base_class.ancestry_column.to_s
Expand Down
2 changes: 1 addition & 1 deletion lib/ancestry/materialized_path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ 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_ancestor_ids_was
# This is technically child_ancestor_ids_in_database
def child_ancestor_ids
# New records cannot have children
raise Ancestry::AncestryException.new(I18n.t("ancestry.no_child_for_new_record")) if new_record?
Expand Down
6 changes: 3 additions & 3 deletions test/concerns/tree_navigation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def test_tree_navigation
# Parent assertions
assert_nil lvl0_node.parent_id
assert_nil lvl0_node.parent
refute lvl0_node.parent_id?
refute lvl0_node.has_parent?
# Root assertions
assert_equal lvl0_node.id, lvl0_node.root_id
assert_equal lvl0_node, lvl0_node.root
Expand Down Expand Up @@ -53,7 +53,7 @@ def test_tree_navigation
# Parent assertions
assert_equal lvl0_node.id, lvl1_node.parent_id
assert_equal lvl0_node, lvl1_node.parent
assert lvl1_node.parent_id?
assert lvl1_node.has_parent?
# Root assertions
assert_equal lvl0_node.id, lvl1_node.root_id
assert_equal lvl0_node, lvl1_node.root
Expand Down Expand Up @@ -93,7 +93,7 @@ def test_tree_navigation
# Parent assertions
assert_equal lvl1_node.id, lvl2_node.parent_id
assert_equal lvl1_node, lvl2_node.parent
assert lvl2_node.parent_id?
assert lvl2_node.has_parent?
# Root assertions
assert_equal lvl0_node.id, lvl2_node.root_id
assert_equal lvl0_node, lvl2_node.root
Expand Down

0 comments on commit f10573d

Please sign in to comment.