From ecdf320e8c6aca5cb2b77bed6d2434e63467cc54 Mon Sep 17 00:00:00 2001 From: Keenan Brock Date: Fri, 9 Oct 2020 18:24:24 -0400 Subject: [PATCH] slow removing ancestor(y) references 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 --- lib/ancestry/has_ancestry.rb | 4 ++-- lib/ancestry/instance_methods.rb | 8 +++++--- lib/ancestry/materialized_path.rb | 2 +- test/concerns/tree_navigation_test.rb | 6 +++--- 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/lib/ancestry/has_ancestry.rb b/lib/ancestry/has_ancestry.rb index f1f29494..95c12bfd 100644 --- a/lib/ancestry/has_ancestry.rb +++ b/lib/ancestry/has_ancestry.rb @@ -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 diff --git a/lib/ancestry/instance_methods.rb b/lib/ancestry/instance_methods.rb index 3c206dbf..919566a7 100644 --- a/lib/ancestry/instance_methods.rb +++ b/lib/ancestry/instance_methods.rb @@ -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 diff --git a/lib/ancestry/materialized_path.rb b/lib/ancestry/materialized_path.rb index 15140557..31d9a3ce 100644 --- a/lib/ancestry/materialized_path.rb +++ b/lib/ancestry/materialized_path.rb @@ -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? diff --git a/test/concerns/tree_navigation_test.rb b/test/concerns/tree_navigation_test.rb index 77bfc7b2..320436d9 100644 --- a/test/concerns/tree_navigation_test.rb +++ b/test/concerns/tree_navigation_test.rb @@ -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 @@ -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 @@ -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