From ac13415de86e6a0317abfdb20c7f7313a4873b3e Mon Sep 17 00:00:00 2001 From: Keenan Brock Date: Sat, 20 Jun 2020 13:43:24 -0400 Subject: [PATCH] rename ancestry to ancestor_ids in more places --- lib/ancestry/class_methods.rb | 6 ++++-- lib/ancestry/instance_methods.rb | 2 +- lib/ancestry/materialized_path.rb | 31 ++++++++----------------------- 3 files changed, 13 insertions(+), 26 deletions(-) diff --git a/lib/ancestry/class_methods.rb b/lib/ancestry/class_methods.rb index 525790bd..11e50fac 100644 --- a/lib/ancestry/class_methods.rb +++ b/lib/ancestry/class_methods.rb @@ -75,12 +75,14 @@ def arrange_serializable options={}, nodes=nil, &block end # Pseudo-preordered array of nodes. Children will always follow parents, + # for ordering nodes within a rank provide block, eg. Node.sort_by_ancestry(Node.all) {|a, b| a.rank <=> b.rank}. + EMPTY_ANCESTRY=[0].freeze def sort_by_ancestry(nodes, &block) arranged = nodes if nodes.is_a?(Hash) unless arranged presorted_nodes = nodes.sort do |a, b| - a_cestry, b_cestry = a.ancestry || '0', b.ancestry || '0' + a_cestry, b_cestry = a.ancestor_ids || EMPTY_ANCESTRY, b.ancestor_ids || EMPTY_ANCESTRY if block_given? && a_cestry == b_cestry yield a, b @@ -130,7 +132,7 @@ def check_ancestry_integrity! options = {} end # ... check that all node parents are consistent with values observed earlier node.path_ids.zip([nil] + node.path_ids).each do |node_id, parent_id| - parents[node_id] = parent_id unless parents.has_key? node_id + parents[node_id] = parent_id unless parents.key? node_id unless parents[node_id] == parent_id raise Ancestry::AncestryIntegrityException.new(I18n.t("ancestry.conflicting_parent_id", :node_id => node_id, diff --git a/lib/ancestry/instance_methods.rb b/lib/ancestry/instance_methods.rb index 3c337b44..3c206dbf 100644 --- a/lib/ancestry/instance_methods.rb +++ b/lib/ancestry/instance_methods.rb @@ -96,6 +96,7 @@ def ancestors? ancestor_ids.present? end alias :has_parent? :ancestors? + alias :parent_id? :ancestors? def will_save_change_to_ancestor_ids? column = self.ancestry_base_class.ancestry_column.to_s @@ -158,7 +159,6 @@ def parent_id= new_parent_id def parent_id ancestor_ids.last if ancestors? end - alias :parent_id? :ancestors? def parent unscoped_find(parent_id) if ancestors? diff --git a/lib/ancestry/materialized_path.rb b/lib/ancestry/materialized_path.rb index fea20b91..15140557 100644 --- a/lib/ancestry/materialized_path.rb +++ b/lib/ancestry/materialized_path.rb @@ -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_id_widcard, nil, true)) end def descendants_of(object) @@ -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_id_widcard, nil, true).or(t[ancestry_column].eq(node.child_ancestor_ids)) end def subtree_of(object) @@ -80,32 +80,17 @@ def ordered_by_ancestry_and(order) end module InstanceMethods - def ancestor_ids=(value) - col = self.ancestry_base_class.ancestry_column - value.present? ? write_attribute(col, value.join(ANCESTRY_DELIMITER)) : write_attribute(col, nil) - end - - def parent_id_before_last_save - ancestor_ids_before_last_save.last - end - - # optimization - better to go directly to column and avoid parsing - def sibling_of?(node) - self.read_attribute(self.ancestry_base_class.ancestry_column) == node.read_attribute(self.ancestry_base_class.ancestry_column) - end - # 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_was + def child_ancestor_ids # New records cannot have children raise Ancestry::AncestryException.new(I18n.t("ancestry.no_child_for_new_record")) if new_record? - path_was = ancestor_ids_in_database - path_was + [id] + ancestor_ids_in_database + [id] end - def child_ancestry_str - child_ancestry.join("/")+"/%" + def child_ancestor_id_widcard + (child_ancestor_ids + ['%']).join(ANCESTRY_DELIMITER) end end end