@@ -7,7 +7,7 @@ def to_node(object)
77 if object . is_a? ( ancestry_base_class )
88 object
99 else
10- unscoped_where { |scope | scope . find ( object . try ( primary_key ) || object ) }
10+ unscoped_where { |scope | scope . find_by! ( primary_ancestry_key => object . try ( primary_ancestry_key ) || object ) }
1111 end
1212 end
1313
@@ -52,17 +52,17 @@ def arrange(options = {})
5252 # @returns Hash{Node => {Node => {}, Node => {}}}
5353 # If a node's parent is not included, the node will be included as if it is a top level node
5454 def arrange_nodes ( nodes , orphan_strategy : :rootify )
55- node_ids = Set . new ( nodes . map ( &:id ) )
55+ node_ids = Set . new ( nodes . map ( &:ancestry_id ) )
5656 index = Hash . new { |h , k | h [ k ] = { } }
5757
5858 if orphan_strategy == :rootify
5959 nodes . each_with_object ( { } ) do |node , arranged |
60- index [ node . parent_id ] [ node ] = children = index [ node . id ]
60+ index [ node . parent_id ] [ node ] = children = index [ node . ancestry_id ]
6161 arranged [ node ] = children unless node_ids . include? ( node . parent_id )
6262 end
6363 else
6464 nodes . each_with_object ( { } ) do |node , arranged |
65- index [ node . parent_id ] [ node ] = children = index [ node . id ]
65+ index [ node . parent_id ] [ node ] = children = index [ node . ancestry_id ]
6666 if node . parent_id . nil?
6767 arranged [ node ] = children
6868 elsif !node_ids . include? ( node . parent_id )
@@ -150,14 +150,14 @@ def self._check_ancestry_integrity!(klass, column, options = {})
150150 # ... check validity of ancestry column
151151 if !node . sane_ancestor_ids?
152152 raise Ancestry ::AncestryIntegrityException , I18n . t ( "ancestry.invalid_ancestry_column" ,
153- :node_id => node . id ,
153+ :node_id => node . ancestry_id ,
154154 :ancestry_column => node . read_attribute ( column ) )
155155 end
156156 # ... check that all ancestors exist
157157 node . ancestor_ids . each do |ancestor_id |
158- unless klass . exists? ( ancestor_id )
158+ unless klass . exists? ( klass . primary_ancestry_key => ancestor_id )
159159 raise Ancestry ::AncestryIntegrityException , I18n . t ( "ancestry.reference_nonexistent_node" ,
160- :node_id => node . id ,
160+ :node_id => node . ancestry_id ,
161161 :ancestor_id => ancestor_id )
162162 end
163163 end
@@ -197,20 +197,20 @@ def restore_ancestry_integrity!
197197 end
198198 end
199199 # ... save parent id of this node in parent_ids array if it exists
200- parent_ids [ node . id ] = node . parent_id if exists? node . parent_id
200+ parent_ids [ node . ancestry_id ] = node . parent_id if exists? ( primary_ancestry_key => node . parent_id )
201201
202202 # Reset parent id in array to nil if it introduces a cycle
203- parent_id = parent_ids [ node . id ]
204- until parent_id . nil? || parent_id == node . id
203+ parent_id = parent_ids [ node . ancestry_id ]
204+ until parent_id . nil? || parent_id == node . ancestry_id
205205 parent_id = parent_ids [ parent_id ]
206206 end
207- parent_ids [ node . id ] = nil if parent_id == node . id
207+ parent_ids [ node . ancestry_id ] = nil if parent_id == node . ancestry_id
208208 end
209209
210210 # For each node ...
211211 scope . find_each do |node |
212212 # ... rebuild ancestry from parent_ids array
213- ancestor_ids , parent_id = [ ] , parent_ids [ node . id ]
213+ ancestor_ids , parent_id = [ ] , parent_ids [ node . ancestry_id ]
214214 until parent_id . nil?
215215 ancestor_ids , parent_id = [ parent_id ] + ancestor_ids , parent_ids [ parent_id ]
216216 end
@@ -229,7 +229,7 @@ def build_ancestry_from_parent_ids!(column = :parent_id, parent_id = nil, ancest
229229 node . without_ancestry_callbacks do
230230 node . update_attribute :ancestor_ids , ancestor_ids
231231 end
232- build_ancestry_from_parent_ids! column , node . id , ancestor_ids + [ node . id ]
232+ build_ancestry_from_parent_ids! column , node . ancestry_id , ancestor_ids + [ node . ancestry_id ]
233233 end
234234 end
235235 end
@@ -274,7 +274,7 @@ def self._rebuild_parent_id_cache!(klass, parent_cache_column)
274274 def self . _rebuild_counter_cache! ( klass , column , counter_col , verbose : false )
275275 child_sql = klass . child_ancestry_sql
276276 tbl = klass . table_name
277- pk = klass . primary_key
277+ pk = klass . primary_ancestry_key
278278
279279 fixed =
280280 if verbose
@@ -311,7 +311,7 @@ def self._rebuild_counter_cache!(klass, column, counter_col, verbose: false)
311311 # Builder generates thin wrappers that delegate here with baked-in column.
312312
313313 def self . _ancestry_exclude_self ( record )
314- record . errors . add ( :base , I18n . t ( "ancestry.exclude_self" , class_name : record . class . model_name . human ) ) if record . ancestor_ids . include? ( record . id )
314+ record . errors . add ( :base , I18n . t ( "ancestry.exclude_self" , class_name : record . class . model_name . human ) ) if record . ancestor_ids . include? ( record . ancestry_id )
315315 end
316316
317317 def self . _update_descendants_with_new_ancestry ( record )
@@ -350,7 +350,7 @@ def self._apply_orphan_strategy_adopt(record)
350350
351351 record . class . ancestry_base_class . descendants_of ( record ) . each do |descendant |
352352 descendant . without_ancestry_callbacks do
353- descendant . update_attribute :ancestor_ids , descendant . ancestor_ids - [ record . id ]
353+ descendant . update_attribute :ancestor_ids , descendant . ancestor_ids - [ record . ancestry_id ]
354354 end
355355 end
356356 end
0 commit comments