mongoid_tree 0.3.4 → 0.3.5
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +11 -6
- data/Gemfile.lock +32 -32
- data/VERSION +1 -1
- data/lib/mongoid_tree.rb +21 -4
- data/mongoid_tree.gemspec +2 -2
- data/spec/mongoid_tree_spec.rb +100 -4
- metadata +3 -3
data/Gemfile
CHANGED
@@ -1,14 +1,19 @@
|
|
1
1
|
source :rubygems
|
2
2
|
|
3
|
+
# My personal global Gemfile in my work directory
|
3
4
|
if File.exist?(file = File.expand_path('../../Gemfile',__FILE__))
|
4
5
|
instance_eval(File.read(file))
|
5
6
|
end
|
6
7
|
|
7
|
-
|
8
|
-
gem "
|
8
|
+
group :development do
|
9
|
+
gem "jeweler"
|
10
|
+
end
|
11
|
+
|
12
|
+
gem "mongoid", ">= 2.0.0.beta.16"
|
9
13
|
# Currently used MongoDB version : 1.4.3
|
10
14
|
|
11
|
-
|
12
|
-
gem "
|
13
|
-
gem "
|
14
|
-
gem "factory_girl"
|
15
|
+
group :test do
|
16
|
+
gem "rspec", ">= 2.0.0"
|
17
|
+
gem "cucumber", ">= 0.8.5"
|
18
|
+
gem "factory_girl"
|
19
|
+
end
|
data/Gemfile.lock
CHANGED
@@ -1,56 +1,57 @@
|
|
1
1
|
GEM
|
2
2
|
remote: http://rubygems.org/
|
3
3
|
specs:
|
4
|
-
activemodel (3.0.
|
5
|
-
activesupport (= 3.0.
|
4
|
+
activemodel (3.0.1)
|
5
|
+
activesupport (= 3.0.1)
|
6
6
|
builder (~> 2.1.2)
|
7
7
|
i18n (~> 0.4.1)
|
8
|
-
activesupport (3.0.
|
8
|
+
activesupport (3.0.1)
|
9
9
|
awesome_print (0.2.1)
|
10
|
-
bson (1.
|
10
|
+
bson (1.1.1)
|
11
11
|
builder (2.1.2)
|
12
|
-
cucumber (0.
|
12
|
+
cucumber (0.9.2)
|
13
13
|
builder (~> 2.1.2)
|
14
14
|
diff-lcs (~> 1.1.2)
|
15
|
-
gherkin (~> 2.
|
16
|
-
|
17
|
-
term-ansicolor (~> 1.0.
|
15
|
+
gherkin (~> 2.2.5)
|
16
|
+
json (~> 1.4.6)
|
17
|
+
term-ansicolor (~> 1.0.5)
|
18
18
|
diff-lcs (1.1.2)
|
19
|
-
factory_girl (1.3.
|
19
|
+
factory_girl (1.3.2)
|
20
20
|
gemcutter (0.6.1)
|
21
|
-
gherkin (2.
|
22
|
-
|
21
|
+
gherkin (2.2.9)
|
22
|
+
json (~> 1.4.6)
|
23
|
+
term-ansicolor (~> 1.0.5)
|
23
24
|
git (1.2.5)
|
24
25
|
i18n (0.4.1)
|
25
26
|
jeweler (1.4.0)
|
26
27
|
gemcutter (>= 0.1.0)
|
27
28
|
git (>= 1.2.5)
|
28
29
|
rubyforge (>= 2.0.0)
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
mongo (= 1.0.
|
36
|
-
tzinfo (
|
30
|
+
json (1.4.6)
|
31
|
+
json_pure (1.4.6)
|
32
|
+
mongo (1.0.9)
|
33
|
+
bson (>= 1.0.5)
|
34
|
+
mongoid (2.0.0.beta.19)
|
35
|
+
activemodel (~> 3.0)
|
36
|
+
mongo (= 1.0.9)
|
37
|
+
tzinfo (~> 0.3.22)
|
37
38
|
will_paginate (~> 3.0.pre)
|
38
|
-
rspec (2.0.
|
39
|
-
rspec-core (
|
40
|
-
rspec-expectations (
|
41
|
-
rspec-mocks (
|
42
|
-
rspec-core (2.0.
|
43
|
-
rspec-expectations (2.0.
|
39
|
+
rspec (2.0.1)
|
40
|
+
rspec-core (~> 2.0.1)
|
41
|
+
rspec-expectations (~> 2.0.1)
|
42
|
+
rspec-mocks (~> 2.0.1)
|
43
|
+
rspec-core (2.0.1)
|
44
|
+
rspec-expectations (2.0.1)
|
44
45
|
diff-lcs (>= 1.1.2)
|
45
|
-
rspec-mocks (2.0.
|
46
|
+
rspec-mocks (2.0.1)
|
47
|
+
rspec-core (~> 2.0.1)
|
48
|
+
rspec-expectations (~> 2.0.1)
|
46
49
|
rubyforge (2.0.4)
|
47
50
|
json_pure (>= 1.1.7)
|
48
51
|
term-ansicolor (1.0.5)
|
49
|
-
|
50
|
-
tzinfo (0.3.22)
|
52
|
+
tzinfo (0.3.23)
|
51
53
|
will_paginate (3.0.pre2)
|
52
54
|
wirble (0.1.3)
|
53
|
-
yard (0.5.8)
|
54
55
|
|
55
56
|
PLATFORMS
|
56
57
|
ruby
|
@@ -60,7 +61,6 @@ DEPENDENCIES
|
|
60
61
|
cucumber (>= 0.8.5)
|
61
62
|
factory_girl
|
62
63
|
jeweler
|
63
|
-
mongoid (
|
64
|
-
rspec (>= 2.0.0
|
64
|
+
mongoid (>= 2.0.0.beta.16)
|
65
|
+
rspec (>= 2.0.0)
|
65
66
|
wirble
|
66
|
-
yard
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.3.
|
1
|
+
0.3.5
|
data/lib/mongoid_tree.rb
CHANGED
@@ -80,15 +80,15 @@ module Mongoid
|
|
80
80
|
end
|
81
81
|
|
82
82
|
def root
|
83
|
-
base_class.find(parent_ids.first)
|
83
|
+
base_class.find(parent_ids.first) unless root?
|
84
84
|
end
|
85
85
|
|
86
86
|
def ancestors
|
87
|
-
base_class.where(:
|
87
|
+
base_class.where(:_id.in => parent_ids)
|
88
88
|
end
|
89
89
|
|
90
90
|
def ancestors_and_self
|
91
|
-
ancestors
|
91
|
+
ancestors << self
|
92
92
|
end
|
93
93
|
|
94
94
|
#Comparable
|
@@ -150,13 +150,30 @@ module Mongoid
|
|
150
150
|
child_ids_array = parent.child_ids.clone
|
151
151
|
child_ids_array.delete(id)
|
152
152
|
parent.update_attributes(:child_ids => child_ids_array )
|
153
|
-
self.update_attributes(:parent_ids => [])
|
153
|
+
self.update_attributes(:parent_ids => [], :position => nil )
|
154
154
|
# and append
|
155
155
|
target_node.children << self
|
156
156
|
# recurse through subtree
|
157
157
|
rebuild_paths
|
158
158
|
end
|
159
159
|
|
160
|
+
def move_to_position(target_node, index)
|
161
|
+
if index > target_node.child_ids.count
|
162
|
+
move_to(target_node)
|
163
|
+
else
|
164
|
+
# unhinge - I was getting a nil on another implementation, so this is a bit longer but works
|
165
|
+
child_ids_array = parent.child_ids.clone
|
166
|
+
child_ids_array.delete(id)
|
167
|
+
parent.update_attributes(:child_ids => child_ids_array )
|
168
|
+
self.update_attributes(:parent_ids => [], :position => nil )
|
169
|
+
|
170
|
+
target_node.children.sort[index - 1].insert_before(self)
|
171
|
+
|
172
|
+
# recurse through subtree
|
173
|
+
rebuild_paths
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
160
177
|
|
161
178
|
def rebuild_paths
|
162
179
|
update_path
|
data/mongoid_tree.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{mongoid_tree}
|
8
|
-
s.version = "0.3.
|
8
|
+
s.version = "0.3.5"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Rainer Kuhn"]
|
12
|
-
s.date = %q{2010-
|
12
|
+
s.date = %q{2010-10-21}
|
13
13
|
s.description = %q{Fully featured tree implementation for Mongoid using materialized paths and relative associations. Featuring Depth and Breadth first search.}
|
14
14
|
s.email = %q{rkuhn@littleweblab.com}
|
15
15
|
s.extra_rdoc_files = [
|
data/spec/mongoid_tree_spec.rb
CHANGED
@@ -1,13 +1,95 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe Mongoid::Acts::Tree do
|
4
4
|
|
5
5
|
before do
|
6
6
|
[ Node ].each { |klass| klass.collection.remove }
|
7
7
|
end
|
8
|
-
|
8
|
+
|
9
|
+
shared_examples_for "a root node" do
|
10
|
+
its (:parent_ids) {should be_empty}
|
11
|
+
its (:parent) {should be_nil}
|
12
|
+
its (:root) {should be_nil}
|
13
|
+
it {should be_root}
|
14
|
+
its (:ancestors) {should be_empty}
|
15
|
+
specify{subject.ancestors_and_self.should eq([subject])}
|
16
|
+
end
|
17
|
+
|
18
|
+
shared_examples_for "a parent node" do
|
19
|
+
its (:child_ids) {should_not be_empty}
|
20
|
+
its (:children) {should_not eq([])}
|
21
|
+
specify{subject.depth_first.should_not eq([subject])}
|
22
|
+
specify{subject.breadth_first.should_not eq([subject])}
|
23
|
+
end
|
24
|
+
|
25
|
+
shared_examples_for "a child node" do
|
26
|
+
its (:parent_ids) {should_not be_empty}
|
27
|
+
its (:parent) {should eq(parent)}
|
28
|
+
its (:root) {should_not be_nil}
|
29
|
+
it {should_not be_root}
|
30
|
+
its (:ancestors) {should_not be_empty}
|
31
|
+
specify{subject.ancestors_and_self.should_not eq([subject])}
|
32
|
+
end
|
33
|
+
|
34
|
+
shared_examples_for "a leaf node" do
|
35
|
+
its (:child_ids) {should be_empty}
|
36
|
+
its (:children) {should eq([])}
|
37
|
+
specify{subject.depth_first.should eq([subject])}
|
38
|
+
specify{subject.breadth_first.should eq([subject])}
|
39
|
+
it { should be_a_leaf }
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
describe "A node" do
|
44
|
+
let(:node) { Node.create :name => "A Node" }
|
45
|
+
subject {node}
|
46
|
+
|
47
|
+
it { should be_valid }
|
48
|
+
it_should_behave_like "a leaf node"
|
49
|
+
it_should_behave_like "a root node"
|
50
|
+
|
51
|
+
context "when adding a child" do
|
52
|
+
let(:parent){node}
|
53
|
+
let(:child){Node.create(:name => "A Child")}
|
54
|
+
before do
|
55
|
+
node.children << child
|
56
|
+
end
|
57
|
+
|
58
|
+
it_should_behave_like "a parent node"
|
59
|
+
it_should_behave_like "a root node"
|
60
|
+
|
61
|
+
it "should change the children count" do
|
62
|
+
expect{
|
63
|
+
node.children << Node.create(:name => "another child")
|
64
|
+
}.to change{node.child_ids.count}.by(1)
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should exist 2 nodes" do
|
68
|
+
Node.count.should be(2)
|
69
|
+
end
|
70
|
+
|
71
|
+
it "should have 1 child" do
|
72
|
+
node.children.count.should be(1)
|
73
|
+
end
|
74
|
+
|
75
|
+
it "should be able to access the child" do
|
76
|
+
node.children.first.should eq(child)
|
77
|
+
end
|
78
|
+
|
79
|
+
its (:depth_first) {should eq([parent, child])}
|
80
|
+
its (:breadth_first) {should eq([parent,child])}
|
81
|
+
|
82
|
+
context "the child" do
|
83
|
+
subject{child}
|
84
|
+
it_should_behave_like "a leaf node"
|
85
|
+
it_should_behave_like "a child node"
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
91
|
+
|
9
92
|
describe "A tree of nodes" do
|
10
|
-
|
11
93
|
context "A node with a child" do
|
12
94
|
|
13
95
|
before do
|
@@ -166,7 +248,21 @@ describe "MongoidTree" do
|
|
166
248
|
getNode(9).parent_ids.should eql([getNode(1).id, getNode(2).id, getNode(6).id])
|
167
249
|
getNode(10).parent_ids.should eql([getNode(1).id, getNode(2).id, getNode(6).id, getNode(9).id])
|
168
250
|
getNode(11).parent_ids.should eql([getNode(1).id, getNode(2).id, getNode(6).id, getNode(9).id])
|
169
|
-
|
251
|
+
end
|
252
|
+
end
|
253
|
+
|
254
|
+
it "should fix the position" do
|
255
|
+
getNode(11).move_to(getNode(1))
|
256
|
+
getNode(11).position.should be(4)
|
257
|
+
end
|
258
|
+
|
259
|
+
it "should move a node to a specific position" do
|
260
|
+
getNode(11).move_to_position(getNode(1),2)
|
261
|
+
getNode(2).position.should be(1)
|
262
|
+
getNode(11).position.should be(2)
|
263
|
+
getNode(7).position.should be(3)
|
264
|
+
getNode(8).position.should be(4)
|
265
|
+
|
170
266
|
end
|
171
267
|
|
172
268
|
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 3
|
8
|
-
-
|
9
|
-
version: 0.3.
|
8
|
+
- 5
|
9
|
+
version: 0.3.5
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Rainer Kuhn
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-
|
17
|
+
date: 2010-10-21 00:00:00 +02:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|