mongoid-tree 0.7.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +2 -2
- data/LICENSE +1 -1
- data/README.md +215 -0
- data/Rakefile +2 -8
- data/lib/mongoid/tree.rb +164 -45
- data/lib/mongoid/tree/ordering.rb +30 -0
- data/lib/mongoid/tree/traversal.rb +34 -36
- data/spec/mongoid/tree/traversal_spec.rb +50 -36
- data/spec/mongoid/tree_spec.rb +23 -13
- data/spec/spec_helper.rb +2 -5
- data/spec/support/models/node.rb +8 -0
- metadata +43 -33
- data/README.rdoc +0 -192
data/spec/spec_helper.rb
CHANGED
@@ -7,15 +7,12 @@ require 'mongoid/tree'
|
|
7
7
|
require 'rspec'
|
8
8
|
|
9
9
|
Mongoid.configure do |config|
|
10
|
-
config.
|
11
|
-
config.allow_dynamic_fields = false
|
10
|
+
config.connect_to('mongoid_tree_test')
|
12
11
|
end
|
13
12
|
|
14
13
|
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
|
15
14
|
|
16
15
|
RSpec.configure do |config|
|
17
16
|
config.mock_with :rspec
|
18
|
-
config.after
|
19
|
-
Mongoid.master.collections.reject { |c| c.name =~ /^system\./ }.each(&:drop)
|
20
|
-
end
|
17
|
+
config.after(:each) { Mongoid.purge! }
|
21
18
|
end
|
data/spec/support/models/node.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid-tree
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,71 +9,84 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-07-14 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: mongoid
|
16
|
-
requirement:
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
|
-
- -
|
19
|
+
- - <=
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '4.0'
|
22
|
+
- - ! '>='
|
20
23
|
- !ruby/object:Gem::Version
|
21
|
-
version: '
|
24
|
+
version: '3.0'
|
22
25
|
type: :runtime
|
23
26
|
prerelease: false
|
24
|
-
version_requirements:
|
27
|
+
version_requirements: !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - <=
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '4.0'
|
33
|
+
- - ! '>='
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '3.0'
|
25
36
|
- !ruby/object:Gem::Dependency
|
26
37
|
name: rake
|
27
|
-
requirement:
|
38
|
+
requirement: !ruby/object:Gem::Requirement
|
28
39
|
none: false
|
29
40
|
requirements:
|
30
41
|
- - ! '>='
|
31
42
|
- !ruby/object:Gem::Version
|
32
|
-
version: 0.
|
43
|
+
version: 0.9.2
|
33
44
|
type: :development
|
34
45
|
prerelease: false
|
35
|
-
version_requirements:
|
46
|
+
version_requirements: !ruby/object:Gem::Requirement
|
47
|
+
none: false
|
48
|
+
requirements:
|
49
|
+
- - ! '>='
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: 0.9.2
|
36
52
|
- !ruby/object:Gem::Dependency
|
37
53
|
name: rspec
|
38
|
-
requirement:
|
54
|
+
requirement: !ruby/object:Gem::Requirement
|
39
55
|
none: false
|
40
56
|
requirements:
|
41
57
|
- - ~>
|
42
58
|
- !ruby/object:Gem::Version
|
43
|
-
version: '2.
|
59
|
+
version: '2.8'
|
44
60
|
type: :development
|
45
61
|
prerelease: false
|
46
|
-
version_requirements:
|
47
|
-
- !ruby/object:Gem::Dependency
|
48
|
-
name: rdoc
|
49
|
-
requirement: &70188241261040 !ruby/object:Gem::Requirement
|
62
|
+
version_requirements: !ruby/object:Gem::Requirement
|
50
63
|
none: false
|
51
64
|
requirements:
|
52
65
|
- - ~>
|
53
66
|
- !ruby/object:Gem::Version
|
54
|
-
version: '2.
|
55
|
-
type: :development
|
56
|
-
prerelease: false
|
57
|
-
version_requirements: *70188241261040
|
67
|
+
version: '2.8'
|
58
68
|
- !ruby/object:Gem::Dependency
|
59
|
-
name:
|
60
|
-
requirement:
|
69
|
+
name: yard
|
70
|
+
requirement: !ruby/object:Gem::Requirement
|
61
71
|
none: false
|
62
72
|
requirements:
|
63
|
-
- -
|
73
|
+
- - ~>
|
64
74
|
- !ruby/object:Gem::Version
|
65
|
-
version:
|
75
|
+
version: '0.8'
|
66
76
|
type: :development
|
67
77
|
prerelease: false
|
68
|
-
version_requirements:
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
none: false
|
80
|
+
requirements:
|
81
|
+
- - ~>
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0.8'
|
69
84
|
description: A tree structure for Mongoid documents using the materialized path pattern
|
70
85
|
email:
|
71
86
|
- benedikt@synatic.net
|
72
87
|
executables: []
|
73
88
|
extensions: []
|
74
|
-
extra_rdoc_files:
|
75
|
-
- README.rdoc
|
76
|
-
- LICENSE
|
89
|
+
extra_rdoc_files: []
|
77
90
|
files:
|
78
91
|
- lib/mongoid/tree/ordering.rb
|
79
92
|
- lib/mongoid/tree/traversal.rb
|
@@ -85,17 +98,14 @@ files:
|
|
85
98
|
- spec/support/macros/tree_macros.rb
|
86
99
|
- spec/support/models/node.rb
|
87
100
|
- LICENSE
|
88
|
-
- README.
|
101
|
+
- README.md
|
89
102
|
- Rakefile
|
90
103
|
- Gemfile
|
91
104
|
- .rspec
|
92
105
|
homepage: https://github.com/benedikt/mongoid-tree
|
93
106
|
licenses: []
|
94
107
|
post_install_message:
|
95
|
-
rdoc_options:
|
96
|
-
- --main
|
97
|
-
- README.rdoc
|
98
|
-
- --charset=UTF-8
|
108
|
+
rdoc_options: []
|
99
109
|
require_paths:
|
100
110
|
- lib
|
101
111
|
required_ruby_version: !ruby/object:Gem::Requirement
|
@@ -112,7 +122,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
112
122
|
version: '0'
|
113
123
|
requirements: []
|
114
124
|
rubyforge_project:
|
115
|
-
rubygems_version: 1.8.
|
125
|
+
rubygems_version: 1.8.24
|
116
126
|
signing_key:
|
117
127
|
specification_version: 3
|
118
128
|
summary: A tree structure for Mongoid documents
|
data/README.rdoc
DELETED
@@ -1,192 +0,0 @@
|
|
1
|
-
= mongoid-tree http://travis-ci.org/benedikt/mongoid-tree.png
|
2
|
-
|
3
|
-
A tree structure for Mongoid documents using the materialized path pattern
|
4
|
-
|
5
|
-
== Requirements
|
6
|
-
|
7
|
-
* mongoid (~> 2.0)
|
8
|
-
|
9
|
-
|
10
|
-
== Install
|
11
|
-
|
12
|
-
To install mongoid_tree, simply add it to your Gemfile:
|
13
|
-
|
14
|
-
gem 'mongoid-tree', :require => 'mongoid/tree'
|
15
|
-
|
16
|
-
In order to get the latest development version of mongoid-tree:
|
17
|
-
|
18
|
-
gem 'mongoid-tree', :git => 'git://github.com/benedikt/mongoid-tree', :require => 'mongoid/tree'
|
19
|
-
|
20
|
-
You might want to remove the <tt>:require => 'mongoid/tree'</tt> option and explicitly <tt>require 'mongoid/tree'</tt> where needed and finally run
|
21
|
-
|
22
|
-
bundle install
|
23
|
-
|
24
|
-
|
25
|
-
== Usage
|
26
|
-
|
27
|
-
Read the API documentation at http://benedikt.github.com/mongoid-tree and take a look at the Mongoid::Tree module
|
28
|
-
|
29
|
-
class Node
|
30
|
-
include Mongoid::Document
|
31
|
-
include Mongoid::Tree
|
32
|
-
end
|
33
|
-
|
34
|
-
|
35
|
-
=== Utility methods
|
36
|
-
|
37
|
-
There are several utility methods that help getting to other related documents in the tree:
|
38
|
-
|
39
|
-
Node.root
|
40
|
-
Node.roots
|
41
|
-
Node.leaves
|
42
|
-
|
43
|
-
node.root
|
44
|
-
node.parent
|
45
|
-
node.children
|
46
|
-
node.ancestors
|
47
|
-
node.ancestors_and_self
|
48
|
-
node.descendants
|
49
|
-
node.descendants_and_self
|
50
|
-
node.siblings
|
51
|
-
node.siblings_and_self
|
52
|
-
node.leaves
|
53
|
-
|
54
|
-
In addition it's possible to check certain aspects of the document's position in the tree:
|
55
|
-
|
56
|
-
node.root?
|
57
|
-
node.leaf?
|
58
|
-
node.depth
|
59
|
-
node.ancestor_of?(other)
|
60
|
-
node.descendant_of?(other)
|
61
|
-
node.sibling_of?(other)
|
62
|
-
|
63
|
-
See Mongoid::Tree for more information on these methods.
|
64
|
-
|
65
|
-
|
66
|
-
=== Ordering
|
67
|
-
|
68
|
-
Mongoid::Tree doesn't order children by default. To enable ordering of tree nodes include the Mongoid::Tree::Ordering module. This will add a <tt>position</tt> field to your document and provide additional utility methods:
|
69
|
-
|
70
|
-
node.lower_siblings
|
71
|
-
node.higher_siblings
|
72
|
-
node.first_sibling_in_list
|
73
|
-
node.last_sibling_in_list
|
74
|
-
|
75
|
-
node.move_up
|
76
|
-
node.move_down
|
77
|
-
node.move_to_top
|
78
|
-
node.move_to_bottom
|
79
|
-
node.move_above(other)
|
80
|
-
node.move_below(other)
|
81
|
-
|
82
|
-
node.at_top?
|
83
|
-
node.at_bottom?
|
84
|
-
|
85
|
-
Example:
|
86
|
-
|
87
|
-
class Node
|
88
|
-
include Mongoid::Document
|
89
|
-
include Mongoid::Tree
|
90
|
-
include Mongoid::Tree::Ordering
|
91
|
-
end
|
92
|
-
|
93
|
-
See Mongoid::Tree::Ordering for more information on these methods.
|
94
|
-
|
95
|
-
=== Traversal
|
96
|
-
|
97
|
-
It's possible to traverse the tree using different traversal methods using the Mongoid::Tree::Traversal module.
|
98
|
-
|
99
|
-
Example:
|
100
|
-
|
101
|
-
class Node
|
102
|
-
include Mongoid::Document
|
103
|
-
include Mongoid::Tree
|
104
|
-
include Mongoid::Tree::Traversal
|
105
|
-
end
|
106
|
-
|
107
|
-
node.traverse(:breadth_first) do |n|
|
108
|
-
# Do something with Node n
|
109
|
-
end
|
110
|
-
|
111
|
-
|
112
|
-
=== Destroying
|
113
|
-
|
114
|
-
Mongoid::Tree does not handle destroying of nodes by default. However it provides several strategies that help you to deal with children of deleted documents. You can simply add them as <tt>before_destroy</tt> callbacks.
|
115
|
-
|
116
|
-
Available strategies are:
|
117
|
-
|
118
|
-
* :nullify_children -- Sets the children's parent_id to null
|
119
|
-
* :move_children_to_parent -- Moves the children to the current document's parent
|
120
|
-
* :destroy_children -- Destroys all children by calling their #destroy method (invokes callbacks)
|
121
|
-
* :delete_descendants -- Deletes all descendants using a database query (doesn't invoke callbacks)
|
122
|
-
|
123
|
-
Example:
|
124
|
-
|
125
|
-
class Node
|
126
|
-
include Mongoid::Document
|
127
|
-
include Mongoid::Tree
|
128
|
-
|
129
|
-
before_destroy :nullify_children
|
130
|
-
end
|
131
|
-
|
132
|
-
|
133
|
-
=== Callbacks
|
134
|
-
|
135
|
-
There are two callbacks that are called before and after the rearranging process. This enables you to do additional computations after the documents position in the tree is updated. See Mongoid::Tree for details.
|
136
|
-
|
137
|
-
Example:
|
138
|
-
|
139
|
-
class Page
|
140
|
-
include Mongoid::Document
|
141
|
-
include Mongoid::Tree
|
142
|
-
|
143
|
-
after_rearrange :rebuild_path
|
144
|
-
|
145
|
-
field :slug
|
146
|
-
field :path
|
147
|
-
|
148
|
-
private
|
149
|
-
|
150
|
-
def rebuild_path
|
151
|
-
self.path = self.ancestors_and_self.collect(&:slug).join('/')
|
152
|
-
end
|
153
|
-
end
|
154
|
-
|
155
|
-
|
156
|
-
=== Validations
|
157
|
-
|
158
|
-
Mongoid::Tree currently does not validate the document's children or parent associations by default. To explicitly enable validation for children and parent documents it's required to add a <tt>validates_associated</tt> validation.
|
159
|
-
|
160
|
-
Example
|
161
|
-
|
162
|
-
class Node
|
163
|
-
include Mongoid::Document
|
164
|
-
include Mongoid::Tree
|
165
|
-
|
166
|
-
validates_associated :parent, :children
|
167
|
-
end
|
168
|
-
|
169
|
-
|
170
|
-
== Build Status
|
171
|
-
|
172
|
-
mongoid-tree is on {Travis}[http://travis-ci.org/benedikt/mongoid-tree] running the specs on Ruby 1.8.7, Ruby 1.9.2 and Ruby Enterprise Edition.
|
173
|
-
|
174
|
-
|
175
|
-
== Known issues
|
176
|
-
|
177
|
-
See https://github.com/benedikt/mongoid-tree/issues
|
178
|
-
|
179
|
-
|
180
|
-
== Repository
|
181
|
-
|
182
|
-
See https://github.com/benedikt/mongoid-tree and feel free to fork it!
|
183
|
-
|
184
|
-
|
185
|
-
== Contributors
|
186
|
-
|
187
|
-
See a list of all contributors at https://github.com/benedikt/mongoid-tree/contributors. Thanks a lot everyone!
|
188
|
-
|
189
|
-
|
190
|
-
== Copyright
|
191
|
-
|
192
|
-
Copyright (c) 2010-2011 Benedikt Deicke. See LICENSE for details.
|