ramdiv-mongo_mapper_acts_as_tree 0.0.0 → 0.0.1
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 +1 -1
- data/lib/mongo_mapper_acts_as_tree.rb +21 -17
- data/ramdiv-mongo_mapper_acts_as_tree.gemspec +1 -1
- data/test/test_order.rb +1 -1
- data/test/test_tree.rb +8 -2
- metadata +1 -1
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.1
|
@@ -25,19 +25,20 @@ module MongoMapper
|
|
25
25
|
extend Fields
|
26
26
|
extend ClassMethods
|
27
27
|
|
28
|
-
key parent_id_field,
|
29
|
-
key path_field,
|
28
|
+
key parent_id_field, ObjectId
|
29
|
+
key path_field, Array, :default => []
|
30
30
|
key depth_field, Integer, :default => 0
|
31
31
|
|
32
32
|
after_save :move_children
|
33
33
|
before_save :will_save_tree
|
34
|
-
|
34
|
+
before_destroy :destroy_descendants
|
35
|
+
ensure_index([path_field, 1])
|
35
36
|
end
|
36
37
|
end
|
37
38
|
|
38
39
|
module ClassMethods
|
39
40
|
def roots
|
40
|
-
self.
|
41
|
+
self.all(parent_id_field => nil, :order => tree_order)
|
41
42
|
end
|
42
43
|
end
|
43
44
|
|
@@ -67,17 +68,17 @@ module MongoMapper
|
|
67
68
|
def fix_position
|
68
69
|
if parent.nil?
|
69
70
|
self[parent_id_field] = nil
|
70
|
-
self[path_field] =
|
71
|
+
self[path_field] = []
|
71
72
|
self[depth_field] = 0
|
72
73
|
else
|
73
|
-
self[parent_id_field] = parent.
|
74
|
-
self[path_field] = parent[path_field] +
|
74
|
+
self[parent_id_field] = parent._id
|
75
|
+
self[path_field] = parent[path_field] + [parent._id]
|
75
76
|
self[depth_field] = parent[depth_field] + 1
|
76
77
|
end
|
77
78
|
end
|
78
79
|
|
79
80
|
def parent
|
80
|
-
@_parent or (self[parent_id_field] ? self.class.find(self[parent_id_field])
|
81
|
+
@_parent or (self[parent_id_field].nil? ? nil : self.class.find(self[parent_id_field]))
|
81
82
|
end
|
82
83
|
|
83
84
|
def root?
|
@@ -85,12 +86,12 @@ module MongoMapper
|
|
85
86
|
end
|
86
87
|
|
87
88
|
def root
|
88
|
-
self.class.find(self[path_field].
|
89
|
+
self[path_field].first.nil? ? self : self.class.find(self[path_field].first)
|
89
90
|
end
|
90
91
|
|
91
92
|
def ancestors
|
92
93
|
return [] if root?
|
93
|
-
self.class.find(self[path_field]
|
94
|
+
self.class.find(self[path_field])
|
94
95
|
end
|
95
96
|
|
96
97
|
def self_and_ancestors
|
@@ -98,21 +99,20 @@ module MongoMapper
|
|
98
99
|
end
|
99
100
|
|
100
101
|
def siblings
|
101
|
-
self.class.
|
102
|
+
self.class.all(:_id => {"$ne" => self._id}, parent_id_field => self[parent_id_field], :order => tree_order)
|
102
103
|
end
|
103
104
|
|
104
105
|
def self_and_siblings
|
105
|
-
self.class.
|
106
|
+
self.class.all(parent_id_field => self[parent_id_field], :order => tree_order)
|
106
107
|
end
|
107
108
|
|
108
109
|
def children
|
109
|
-
self.class.
|
110
|
+
self.class.all(parent_id_field => self._id.to_s, :order => tree_order)
|
110
111
|
end
|
111
112
|
|
112
113
|
def descendents
|
113
114
|
return [] if new_record?
|
114
|
-
|
115
|
-
self.class.collection.find({path_field => /#{self._id}/}, {:sort => sorting_options}).map{|i| self.class.new(i)} or []
|
115
|
+
self.class.all(path_field => self._id, :order => tree_order)
|
116
116
|
end
|
117
117
|
|
118
118
|
def self_and_descendents
|
@@ -120,7 +120,7 @@ module MongoMapper
|
|
120
120
|
end
|
121
121
|
|
122
122
|
def is_ancestor_of?(other)
|
123
|
-
|
123
|
+
other[path_field].include?(self._id)
|
124
124
|
end
|
125
125
|
|
126
126
|
def is_or_is_ancestor_of?(other)
|
@@ -128,7 +128,7 @@ module MongoMapper
|
|
128
128
|
end
|
129
129
|
|
130
130
|
def is_descendant_of?(other)
|
131
|
-
|
131
|
+
self[path_field].include?(other._id)
|
132
132
|
end
|
133
133
|
|
134
134
|
def is_or_is_descendant_of?(other)
|
@@ -153,6 +153,10 @@ module MongoMapper
|
|
153
153
|
@_will_move = true
|
154
154
|
end
|
155
155
|
end
|
156
|
+
|
157
|
+
def destroy_descendants
|
158
|
+
self.class.destroy(self.descendents.map(&:_id))
|
159
|
+
end
|
156
160
|
end
|
157
161
|
|
158
162
|
module Fields
|
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{ramdiv-mongo_mapper_acts_as_tree}
|
8
|
-
s.version = "0.0.
|
8
|
+
s.version = "0.0.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Jakob Vidmar"]
|
data/test/test_order.rb
CHANGED
@@ -16,7 +16,7 @@ class TestMongomapperActsAsTree < Test::Unit::TestCase
|
|
16
16
|
assert_equal OrderedCategory.roots, [@root_2, @root_1]
|
17
17
|
|
18
18
|
assert_equal @root_1.children, [@child_1, @child_3, @child_2]
|
19
|
-
|
19
|
+
|
20
20
|
assert_equal @root_1.descendents, [@child_1, @child_2_1, @child_3, @child_2]
|
21
21
|
assert_equal @root_1.self_and_descendents, [@root_1, @child_1, @child_2_1, @child_3, @child_2]
|
22
22
|
|
data/test/test_tree.rb
CHANGED
@@ -12,10 +12,11 @@ class TestMongomapperActsAsTree < Test::Unit::TestCase
|
|
12
12
|
@root_2 = Category.create(:name => "Root 2")
|
13
13
|
end
|
14
14
|
|
15
|
+
|
15
16
|
should "have roots" do
|
16
17
|
assert eql_arrays?(Category.roots, [@root_1, @root_2])
|
17
18
|
end
|
18
|
-
|
19
|
+
|
19
20
|
context "node" do
|
20
21
|
should "have a root" do
|
21
22
|
assert_equal @root_1.root, @root_1
|
@@ -111,7 +112,7 @@ class TestMongomapperActsAsTree < Test::Unit::TestCase
|
|
111
112
|
|
112
113
|
@child_2.save
|
113
114
|
@child_2_1.reload
|
114
|
-
|
115
|
+
|
115
116
|
assert @root_2.is_or_is_ancestor_of?(@child_2_1)
|
116
117
|
assert @child_2_1.is_or_is_descendant_of?(@root_2)
|
117
118
|
assert @root_2.descendents.include?(@child_2_1)
|
@@ -122,6 +123,11 @@ class TestMongomapperActsAsTree < Test::Unit::TestCase
|
|
122
123
|
assert !@root_1.save
|
123
124
|
end
|
124
125
|
end
|
126
|
+
|
127
|
+
should "destroy descendents when destroyed" do
|
128
|
+
@child_2.destroy
|
129
|
+
assert_nil Category.find(@child_2_1._id)
|
130
|
+
end
|
125
131
|
end
|
126
132
|
|
127
133
|
context "root node" do
|