Skip to content

Commit 0f6c677

Browse files
committed
ancestry => ancestor_ids
1 parent 6d7b059 commit 0f6c677

File tree

3 files changed

+6
-26
lines changed

3 files changed

+6
-26
lines changed

lib/ancestry/class_methods.rb

+4-3
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,13 @@ def arrange_serializable options={}, nodes=nil, &block
6868

6969
# Pseudo-preordered array of nodes. Children will always follow parents,
7070
# for ordering nodes within a rank provide block, eg. Node.sort_by_ancestry(Node.all) {|a, b| a.rank <=> b.rank}.
71+
EMPTY_ANCESTRY=[0].freeze
7172
def sort_by_ancestry(nodes, &block)
7273
arranged = nodes if nodes.is_a?(Hash)
7374

7475
unless arranged
7576
presorted_nodes = nodes.sort do |a, b|
76-
a_cestry, b_cestry = a.ancestry || '0', b.ancestry || '0'
77+
a_cestry, b_cestry = a.ancestor_ids || EMPTY_ANCESTRY, b.ancestor_ids || EMPTY_ANCESTRY
7778

7879
if block_given? && a_cestry == b_cestry
7980
yield a, b
@@ -104,7 +105,7 @@ def check_ancestry_integrity! options = {}
104105
begin
105106
# ... check validity of ancestry column
106107
if !node.sane_ancestor_ids?
107-
raise Ancestry::AncestryIntegrityException.new("Invalid format for ancestry column of node #{node.id}: #{node.read_attribute node.ancestry_column}.")
108+
raise Ancestry::AncestryIntegrityException.new("Invalid format for ancestry column of node #{node.id}: #{node.ancestor_ids}.")
108109
end
109110
# ... check that all ancestors exist
110111
node.ancestor_ids.each do |ancestor_id|
@@ -114,7 +115,7 @@ def check_ancestry_integrity! options = {}
114115
end
115116
# ... check that all node parents are consistent with values observed earlier
116117
node.path_ids.zip([nil] + node.path_ids).each do |node_id, parent_id|
117-
parents[node_id] = parent_id unless parents.has_key? node_id
118+
parents[node_id] = parent_id unless parents.key? node_id
118119
unless parents[node_id] == parent_id
119120
raise Ancestry::AncestryIntegrityException.new("Conflicting parent id found in node #{node.id}: #{parent_id || 'nil'} for node #{node_id} while expecting #{parents[node_id] || 'nil'}")
120121
end

lib/ancestry/instance_methods.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ def ancestors?
9696
ancestor_ids.present?
9797
end
9898
alias :has_parent? :ancestors?
99+
alias :parent_id? :ancestors?
99100

100101
def sane_ancestor_ids?
101102
valid? || errors[self.ancestry_base_class.ancestry_column].blank?
@@ -145,7 +146,6 @@ def parent_id= new_parent_id
145146
def parent_id
146147
ancestor_ids.last if ancestors?
147148
end
148-
alias :parent_id? :ancestors?
149149

150150
def parent
151151
unscoped_find(parent_id) if ancestors?

lib/ancestry/materialized_path.rb

+1-22
Original file line numberDiff line numberDiff line change
@@ -78,27 +78,6 @@ def ordered_by_ancestry_and(order)
7878
end
7979

8080
module InstanceMethods
81-
82-
# Validates the ancestry, but can also be applied if validation is bypassed to determine if children should be affected
83-
def sane_ancestry?
84-
ancestry_value = read_attribute(self.ancestry_base_class.ancestry_column)
85-
(ancestry_value.nil? || !ancestor_ids.include?(self.id)) && valid?
86-
end
87-
88-
def ancestor_ids=(value)
89-
col = self.ancestry_base_class.ancestry_column
90-
value.present? ? write_attribute(col, value.join(ANCESTRY_DELIMITER)) : write_attribute(col, nil)
91-
end
92-
93-
def parent_id_before_last_save
94-
ancestor_ids_before_last_save.last
95-
end
96-
97-
# optimization - better to go directly to column and avoid parsing
98-
def sibling_of?(node)
99-
self.read_attribute(self.ancestry_base_class.ancestry_column) == node.read_attribute(self.ancestry_base_class.ancestry_column)
100-
end
101-
10281
# private (public so class methods can find it)
10382
# The ancestry value for this record's children (before save)
10483
# This is technically child_ancestry_was
@@ -110,7 +89,7 @@ def child_ancestry
11089
end
11190

11291
def child_ancestry_str
113-
child_ancestry.join("/")+"/%"
92+
(child_ancestry + ['%']).join(ANCESTRY_DELIMITER)
11493
end
11594
end
11695
end

0 commit comments

Comments
 (0)