mongo_mapper_tree 0.0.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +2 -0
- data/lib/mongo_mapper/plugins/tree.rb +84 -86
- data/lib/version.rb +2 -2
- metadata +40 -48
data/README.rdoc
CHANGED
@@ -29,6 +29,8 @@ This adds class_attributes called parent_id_field, path_field, depth_field, tree
|
|
29
29
|
|
30
30
|
Check test_tree.rb and test_search_class.rb for examples.
|
31
31
|
|
32
|
+
For best performance add a index on the field called path_field.
|
33
|
+
|
32
34
|
== Note on Patches/Pull Requests
|
33
35
|
|
34
36
|
* Fork the project.
|
@@ -10,114 +10,112 @@ module MongoMapper
|
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
end
|
13
|
+
def tree_search_class
|
14
|
+
self.class.tree_search_class
|
15
|
+
end
|
17
16
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
end
|
17
|
+
def will_save_tree
|
18
|
+
if parent && self.descendants.include?(parent)
|
19
|
+
errors.add(:base, :cyclic)
|
22
20
|
end
|
21
|
+
end
|
23
22
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
end
|
23
|
+
def fix_position(opts = {})
|
24
|
+
if parent.nil?
|
25
|
+
self[parent_id_field] = nil
|
26
|
+
self[path_field] = []
|
27
|
+
self[depth_field] = 0
|
28
|
+
elsif !!opts[:force] || self.changes.include?(parent_id_field)
|
29
|
+
@_will_move = true
|
30
|
+
self[path_field] = parent[path_field] + [parent._id]
|
31
|
+
self[depth_field] = parent[depth_field] + 1
|
34
32
|
end
|
33
|
+
end
|
35
34
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
35
|
+
def fix_position!
|
36
|
+
fix_position(:force => true)
|
37
|
+
save
|
38
|
+
end
|
40
39
|
|
41
|
-
|
42
|
-
|
43
|
-
|
40
|
+
def root?
|
41
|
+
self[parent_id_field].nil?
|
42
|
+
end
|
44
43
|
|
45
|
-
|
46
|
-
|
47
|
-
|
44
|
+
def root
|
45
|
+
self[path_field].first.nil? ? self : tree_search_class.find(self[path_field].first)
|
46
|
+
end
|
48
47
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
48
|
+
def ancestors
|
49
|
+
return [] if root?
|
50
|
+
tree_search_class.find(self[path_field])
|
51
|
+
end
|
53
52
|
|
54
|
-
|
55
|
-
|
56
|
-
|
53
|
+
def self_and_ancestors
|
54
|
+
ancestors << self
|
55
|
+
end
|
57
56
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
57
|
+
def siblings
|
58
|
+
tree_search_class.where({
|
59
|
+
:_id => { "$ne" => self._id },
|
60
|
+
parent_id_field => self[parent_id_field]
|
61
|
+
}).sort(tree_order).all
|
62
|
+
end
|
64
63
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
64
|
+
def self_and_siblings
|
65
|
+
tree_search_class.where({
|
66
|
+
parent_id_field => self[parent_id_field]
|
67
|
+
}).sort(tree_order).all
|
68
|
+
end
|
70
69
|
|
71
|
-
|
72
|
-
|
73
|
-
|
70
|
+
def children
|
71
|
+
tree_search_class.where(parent_id_field => self._id).sort(tree_order).all
|
72
|
+
end
|
74
73
|
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
74
|
+
def descendants
|
75
|
+
return [] if new_record?
|
76
|
+
tree_search_class.where(path_field => self._id).sort(tree_order).all
|
77
|
+
end
|
79
78
|
|
80
|
-
|
81
|
-
|
82
|
-
|
79
|
+
def self_and_descendants
|
80
|
+
[self] + self.descendants
|
81
|
+
end
|
83
82
|
|
84
|
-
|
85
|
-
|
86
|
-
|
83
|
+
def is_ancestor_of?(other)
|
84
|
+
other[path_field].include?(self._id)
|
85
|
+
end
|
87
86
|
|
88
|
-
|
89
|
-
|
90
|
-
|
87
|
+
def is_or_is_ancestor_of?(other)
|
88
|
+
(other == self) or is_ancestor_of?(other)
|
89
|
+
end
|
91
90
|
|
92
|
-
|
93
|
-
|
94
|
-
|
91
|
+
def is_descendant_of?(other)
|
92
|
+
self[path_field].include?(other._id)
|
93
|
+
end
|
95
94
|
|
96
|
-
|
97
|
-
|
98
|
-
|
95
|
+
def is_or_is_descendant_of?(other)
|
96
|
+
(other == self) or is_descendant_of?(other)
|
97
|
+
end
|
99
98
|
|
100
|
-
|
101
|
-
|
102
|
-
|
99
|
+
def is_sibling_of?(other)
|
100
|
+
(other != self) and (other[parent_id_field] == self[parent_id_field])
|
101
|
+
end
|
103
102
|
|
104
|
-
|
105
|
-
|
106
|
-
|
103
|
+
def is_or_is_sibling_of?(other)
|
104
|
+
(other == self) or is_sibling_of?(other)
|
105
|
+
end
|
107
106
|
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
end
|
114
|
-
@_will_move = true
|
107
|
+
def move_children
|
108
|
+
if @_will_move
|
109
|
+
@_will_move = false
|
110
|
+
self.children.each do |child|
|
111
|
+
child.fix_position!
|
115
112
|
end
|
113
|
+
@_will_move = true
|
116
114
|
end
|
115
|
+
end
|
117
116
|
|
118
|
-
|
119
|
-
|
120
|
-
end
|
117
|
+
def destroy_descendants
|
118
|
+
tree_search_class.destroy(self.descendants.map(&:_id))
|
121
119
|
end
|
122
120
|
|
123
121
|
included do
|
@@ -145,7 +143,7 @@ module MongoMapper
|
|
145
143
|
class_attribute :tree_order
|
146
144
|
|
147
145
|
key parent_id_field, ObjectId
|
148
|
-
key path_field, Array, :default => []
|
146
|
+
key path_field, Array, :default => []
|
149
147
|
key depth_field, Integer, :default => 0
|
150
148
|
|
151
149
|
belongs_to :parent, :class => tree_search_class
|
@@ -157,4 +155,4 @@ module MongoMapper
|
|
157
155
|
end
|
158
156
|
end
|
159
157
|
end
|
160
|
-
end
|
158
|
+
end
|
data/lib/version.rb
CHANGED
metadata
CHANGED
@@ -1,50 +1,46 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongo_mapper_tree
|
3
|
-
version: !ruby/object:Gem::Version
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
4
5
|
prerelease:
|
5
|
-
version: 0.0.1
|
6
6
|
platform: ruby
|
7
|
-
authors:
|
8
|
-
-
|
7
|
+
authors:
|
8
|
+
- Joel Junström
|
9
|
+
- Arvid Andersson
|
9
10
|
autorequire:
|
10
11
|
bindir: bin
|
11
12
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
dependencies:
|
16
|
-
- !ruby/object:Gem::Dependency
|
13
|
+
date: 2012-02-27 00:00:00.000000000 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
17
16
|
name: mongo_mapper
|
18
|
-
requirement: &
|
17
|
+
requirement: &70110048249320 !ruby/object:Gem::Requirement
|
19
18
|
none: false
|
20
|
-
requirements:
|
19
|
+
requirements:
|
21
20
|
- - ~>
|
22
|
-
- !ruby/object:Gem::Version
|
23
|
-
version: 0.
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 0.11.0
|
24
23
|
type: :runtime
|
25
24
|
prerelease: false
|
26
|
-
version_requirements: *
|
27
|
-
- !ruby/object:Gem::Dependency
|
25
|
+
version_requirements: *70110048249320
|
26
|
+
- !ruby/object:Gem::Dependency
|
28
27
|
name: shoulda
|
29
|
-
requirement: &
|
28
|
+
requirement: &70110048247860 !ruby/object:Gem::Requirement
|
30
29
|
none: false
|
31
|
-
requirements:
|
30
|
+
requirements:
|
32
31
|
- - ~>
|
33
|
-
- !ruby/object:Gem::Version
|
34
|
-
version:
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2.10'
|
35
34
|
type: :development
|
36
35
|
prerelease: false
|
37
|
-
version_requirements: *
|
36
|
+
version_requirements: *70110048247860
|
38
37
|
description: An Acts As Tree like implementation for MongoMapper based on mongo_mapper_acts_as_tree
|
39
|
-
email:
|
40
|
-
-
|
38
|
+
email:
|
39
|
+
- bender@oktavilla.se
|
41
40
|
executables: []
|
42
|
-
|
43
41
|
extensions: []
|
44
|
-
|
45
42
|
extra_rdoc_files: []
|
46
|
-
|
47
|
-
files:
|
43
|
+
files:
|
48
44
|
- lib/locale/en.yml
|
49
45
|
- lib/mongo_mapper/plugins/tree.rb
|
50
46
|
- lib/mongo_mapper_tree.rb
|
@@ -58,41 +54,37 @@ files:
|
|
58
54
|
- test/test_tree.rb
|
59
55
|
- LICENSE
|
60
56
|
- README.rdoc
|
61
|
-
has_rdoc: true
|
62
57
|
homepage: http://github.com/Oktavilla/mongo_mapper_tree
|
63
58
|
licenses: []
|
64
|
-
|
65
59
|
post_install_message:
|
66
60
|
rdoc_options: []
|
67
|
-
|
68
|
-
require_paths:
|
61
|
+
require_paths:
|
69
62
|
- lib
|
70
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
64
|
none: false
|
72
|
-
requirements:
|
73
|
-
- -
|
74
|
-
- !ruby/object:Gem::Version
|
75
|
-
|
76
|
-
segments:
|
65
|
+
requirements:
|
66
|
+
- - ! '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
segments:
|
77
70
|
- 0
|
78
|
-
|
79
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
hash: -1182322477251881257
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
80
73
|
none: false
|
81
|
-
requirements:
|
82
|
-
- -
|
83
|
-
- !ruby/object:Gem::Version
|
84
|
-
|
85
|
-
segments:
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
segments:
|
86
79
|
- 0
|
87
|
-
|
80
|
+
hash: -1182322477251881257
|
88
81
|
requirements: []
|
89
|
-
|
90
82
|
rubyforge_project:
|
91
|
-
rubygems_version: 1.
|
83
|
+
rubygems_version: 1.8.15
|
92
84
|
signing_key:
|
93
85
|
specification_version: 3
|
94
86
|
summary: An Acts As Tree like implementation for MongoMapper
|
95
|
-
test_files:
|
87
|
+
test_files:
|
96
88
|
- test/helper.rb
|
97
89
|
- test/models/category.rb
|
98
90
|
- test/models/ordered_category.rb
|