mongoid_nested_set 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.1.2
@@ -59,6 +59,37 @@ module Mongoid::Acts::NestedSet
59
59
  end
60
60
 
61
61
 
62
+ # Iterates over tree elements with ancestors.
63
+ # Only accepts default ordering, ordering by an other field than lft
64
+ # does not work. This is much more efficient than calling ancestors for
65
+ # each object because it doesn't require any additional database queries.
66
+ #
67
+ # Example:
68
+ # Category.each_with_ancestors(Category.root.self_and_descendants) do |o, ancestors|
69
+ #
70
+ def each_with_ancestors(objects)
71
+ ancestors = nil
72
+ last_parent = nil
73
+ objects.each do |o|
74
+ if ancestors == nil
75
+ ancestors = o.root? ? [] : o.ancestors.entries
76
+ end
77
+ if ancestors.empty? || o.parent_id != ancestors.last.id
78
+ # we are on a new level, did we descend or ascend?
79
+ if ancestors.any? {|a| a.id == o.parent_id}
80
+ # ascend
81
+ ancestors.pop while (!ancestors.empty? && ancestors.last.id != o.parent_id)
82
+ elsif !o.root?
83
+ # descend
84
+ ancestors << last_parent
85
+ end
86
+ end
87
+ yield(o, ancestors)
88
+ last_parent = o
89
+ end
90
+ end
91
+
92
+
62
93
  # Provides a chainable relation to select all descendants of a set of records,
63
94
  # excluding the record set itself.
64
95
  # Similar to parent.descendants, except this allows you to find all descendants
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mongoid_nested_set}
8
- s.version = "0.1.1"
8
+ s.version = "0.1.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Brandon Turner"]
12
- s.date = %q{2010-12-17}
12
+ s.date = %q{2010-12-29}
13
13
  s.description = %q{Fully featured tree implementation for Mongoid using the nested set model}
14
14
  s.email = %q{bturner@bltweb.net}
15
15
  s.extra_rdoc_files = [
@@ -567,6 +567,42 @@ describe "A Mongoid::Document" do
567
567
  @nodes[:jackets].depth.should == 4
568
568
  end
569
569
 
570
+ it "can loop over elements starting at root with level" do
571
+ i = 0
572
+ Node.each_with_level(@nodes[:clothing].self_and_descendants) do |o, level|
573
+ level.should == o.depth
574
+ i += 1
575
+ end
576
+ i.should == 11
577
+ end
578
+
579
+ it "can loop over elements starting at non-root with level" do
580
+ i = 0
581
+ Node.each_with_level(@nodes[:mens].self_and_descendants) do |o, level|
582
+ level.should == o.depth
583
+ i += 1
584
+ end
585
+ i.should == 4
586
+ end
587
+
588
+ it "can loop over elements starting at root with ancestors" do
589
+ i = 0
590
+ Node.each_with_ancestors(@nodes[:clothing].self_and_descendants) do |o, ancestors|
591
+ ancestors.should == o.ancestors.entries
592
+ i += 1
593
+ end
594
+ i.should == 11
595
+ end
596
+
597
+ it "can loop over elements starting at non-root with ancestors" do
598
+ i = 0
599
+ Node.each_with_ancestors(@nodes[:mens].self_and_descendants) do |o, ancestors|
600
+ ancestors.should == o.ancestors.entries
601
+ i += 1
602
+ end
603
+ i.should == 4
604
+ end
605
+
570
606
  context "with dependent=delete_all" do
571
607
  it "deletes descendants when destroyed" do
572
608
  @nodes[:mens].destroy
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 1
9
- version: 0.1.1
8
+ - 2
9
+ version: 0.1.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Brandon Turner
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-12-17 00:00:00 -06:00
17
+ date: 2010-12-29 00:00:00 -06:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -145,7 +145,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
145
145
  requirements:
146
146
  - - ">="
147
147
  - !ruby/object:Gem::Version
148
- hash: -2723032972505420732
148
+ hash: -1884730706523752762
149
149
  segments:
150
150
  - 0
151
151
  version: "0"