File tree Expand file tree Collapse file tree 2 files changed +21
-5
lines changed Expand file tree Collapse file tree 2 files changed +21
-5
lines changed Original file line number Diff line number Diff line change @@ -103,7 +103,7 @@ module Tree
103
103
end
104
104
105
105
##
106
- # This module implements class methods that will be available
106
+ # This module implements class methods that will be available
107
107
# on the document that includes Mongoid::Tree
108
108
module ClassMethods
109
109
@@ -213,7 +213,7 @@ def leaves
213
213
# @note Generated by Mongoid
214
214
#
215
215
# @param [Mongoid::Tree] document
216
-
216
+
217
217
##
218
218
# @!method parent_ids
219
219
# Returns a list of the document's parent_ids, starting with the root node.
@@ -272,7 +272,7 @@ def root
272
272
#
273
273
# @return [Mongoid::Criteria] Mongoid criteria to retrieve the documents ancestors
274
274
def ancestors
275
- base_class . where ( :_id . in => parent_ids )
275
+ base_class . or ( parent_ids . map { | id | { :_id => id } } )
276
276
end
277
277
278
278
##
Original file line number Diff line number Diff line change 270
270
end
271
271
272
272
describe 'ancestors' do
273
- it "#ancestors should return the documents ancestors" do
274
- node ( :subchild ) . ancestors . to_a . should == [ node ( :root ) , node ( :child ) ]
273
+ describe '#ancestors' do
274
+ it "should return the documents ancestors" do
275
+ node ( :subchild ) . ancestors . to_a . should == [ node ( :root ) , node ( :child ) ]
276
+ end
277
+
278
+ it "should return the ancestors in correct order even after rearranging" do
279
+ setup_tree <<-ENDTREE
280
+ - root:
281
+ - child:
282
+ - subchild
283
+ ENDTREE
284
+
285
+ child = node ( :child ) ; child . parent = nil ; child . save!
286
+ root = node ( :root ) ; root . parent = node ( :child ) ; root . save!
287
+ subchild = node ( :subchild ) ; subchild . parent = root ; subchild . save!
288
+
289
+ subchild . ancestors . to_a . should == [ child , root ]
290
+ end
275
291
end
276
292
277
293
it "#ancestors_and_self should return the documents ancestors and itself" do
You can’t perform that action at this time.
0 commit comments