Skip to content

Commit

Permalink
rename ancestry to ancestor_ids in more places
Browse files Browse the repository at this point in the history
  • Loading branch information
kbrock committed Nov 1, 2020
1 parent 79a79bd commit ac13415
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 26 deletions.
6 changes: 4 additions & 2 deletions lib/ancestry/class_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion lib/ancestry/instance_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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?
Expand Down
31 changes: 8 additions & 23 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_id_widcard, 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_id_widcard, nil, true).or(t[ancestry_column].eq(node.child_ancestor_ids))
end

def subtree_of(object)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ac13415

Please sign in to comment.