mongo_mapper_acts_as_tree 0.3.1 → 0.3.2
Sign up to get free protection for your applications and to get access to all the features.
@@ -9,16 +9,18 @@ module MongoMapper
|
|
9
9
|
# --------------------------------------------------------------------------------
|
10
10
|
module ClassMethods
|
11
11
|
def acts_as_tree(options = {})
|
12
|
-
configuration = { :foreign_key => :parent_id, :order => nil
|
12
|
+
configuration = { :foreign_key => :parent_id, :order => nil }
|
13
13
|
configuration.update(options) if options.is_a?(Hash)
|
14
14
|
|
15
15
|
# automatically create needed keys if they don't already exist
|
16
16
|
key configuration[:foreign_key], ObjectId unless keys.key?(configuration[:foreign_key])
|
17
17
|
key configuration[:foreign_key].to_s.pluralize.to_sym, Array unless keys.key?(configuration[:foreign_key].to_s.pluralize.to_sym)
|
18
18
|
|
19
|
-
belongs_to :parent, :class_name => name, :foreign_key => configuration[:foreign_key]
|
19
|
+
belongs_to :parent, :class_name => name, :foreign_key => configuration[:foreign_key]
|
20
20
|
many :children, :class_name => name, :foreign_key => configuration[:foreign_key], :order => configuration[:order], :dependent => :destroy
|
21
|
+
|
21
22
|
before_save :set_parents
|
23
|
+
after_save :update_descendants
|
22
24
|
|
23
25
|
class_eval <<-EOV
|
24
26
|
def self.roots
|
@@ -65,6 +67,13 @@ module MongoMapper
|
|
65
67
|
def self_and_siblings
|
66
68
|
parent? ? parent.children : self.class.roots
|
67
69
|
end
|
70
|
+
|
71
|
+
# after save, update descendants, so they have correct parent_ids values
|
72
|
+
def update_descendants
|
73
|
+
descendants.each do |descendant|
|
74
|
+
descendant.save unless (descendant.parent_ids - [self.id] == self.parent_ids)
|
75
|
+
end
|
76
|
+
end
|
68
77
|
end
|
69
78
|
|
70
79
|
end
|
data/test/acts_as_tree_test.rb
CHANGED
@@ -129,58 +129,18 @@ class TreeTest < ActiveSupport::TestCase
|
|
129
129
|
assert_equal [@root_child1, @root_child2], @root_child2.self_and_siblings
|
130
130
|
assert_equal [@root1, @root2, @root3], @root2.self_and_siblings
|
131
131
|
assert_equal [@root1, @root2, @root3], @root3.self_and_siblings
|
132
|
-
end
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
# class TreeTestWithEagerLoading < Test::Unit::TestCase
|
139
|
-
#
|
140
|
-
# def setup
|
141
|
-
# teardown_db
|
142
|
-
# setup_db
|
143
|
-
# @root1 = TreeMixin.create!
|
144
|
-
# @root_child1 = TreeMixin.create! :parent_id => @root1.id
|
145
|
-
# @child1_child = TreeMixin.create! :parent_id => @root_child1.id
|
146
|
-
# @root_child2 = TreeMixin.create! :parent_id => @root1.id
|
147
|
-
# @root2 = TreeMixin.create!
|
148
|
-
# @root3 = TreeMixin.create!
|
149
|
-
#
|
150
|
-
# @rc1 = RecursivelyCascadedTreeMixin.create!
|
151
|
-
# @rc2 = RecursivelyCascadedTreeMixin.create! :parent_id => @rc1.id
|
152
|
-
# @rc3 = RecursivelyCascadedTreeMixin.create! :parent_id => @rc2.id
|
153
|
-
# @rc4 = RecursivelyCascadedTreeMixin.create! :parent_id => @rc3.id
|
154
|
-
# end
|
155
|
-
#
|
156
|
-
# def test_eager_association_loading
|
157
|
-
# roots = TreeMixin.find(:all, :include => :children, :conditions => "mixins.parent_id IS NULL", :order => "mixins.id")
|
158
|
-
# assert_equal [@root1, @root2, @root3], roots
|
159
|
-
# assert_no_queries do
|
160
|
-
# assert_equal 2, roots[0].children.size
|
161
|
-
# assert_equal 0, roots[1].children.size
|
162
|
-
# assert_equal 0, roots[2].children.size
|
163
|
-
# end
|
164
|
-
# end
|
165
|
-
#
|
166
|
-
# def test_eager_association_loading_with_recursive_cascading_three_levels_has_many
|
167
|
-
# root_node = RecursivelyCascadedTreeMixin.find(:first, :include => { :children => { :children => :children } }, :order => 'mixins.id')
|
168
|
-
# assert_equal @rc4, assert_no_queries { root_node.children.first.children.first.children.first }
|
169
|
-
# end
|
170
|
-
#
|
171
|
-
# def test_eager_association_loading_with_recursive_cascading_three_levels_has_one
|
172
|
-
# root_node = RecursivelyCascadedTreeMixin.find(:first, :include => { :first_child => { :first_child => :first_child } }, :order => 'mixins.id')
|
173
|
-
# assert_equal @rc4, assert_no_queries { root_node.first_child.first_child.first_child }
|
174
|
-
# end
|
175
|
-
#
|
176
|
-
# def test_eager_association_loading_with_recursive_cascading_three_levels_belongs_to
|
177
|
-
# leaf_node = RecursivelyCascadedTreeMixin.find(:first, :include => { :parent => { :parent => :parent } }, :order => 'mixins.id DESC')
|
178
|
-
# assert_equal @rc1, assert_no_queries { leaf_node.parent.parent.parent }
|
179
|
-
# end
|
180
|
-
# end
|
181
|
-
|
182
|
-
|
132
|
+
end
|
133
|
+
|
134
|
+
def test_change_of_parent_id
|
135
|
+
@root_child1.parent = @root2
|
136
|
+
@root_child1.save
|
183
137
|
|
138
|
+
assert_equal [@root_child1], @root2.reload.children
|
139
|
+
assert_equal [@root_child1, @child1_child], @root2.reload.descendants
|
140
|
+
assert_equal [@root2, @root_child1], @child1_child.reload.ancestors
|
141
|
+
assert_equal [@root_child2], @root1.reload.descendants
|
142
|
+
end
|
143
|
+
end
|
184
144
|
|
185
145
|
|
186
146
|
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongo_mapper_acts_as_tree
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 3
|
9
|
-
-
|
10
|
-
version: 0.3.
|
9
|
+
- 2
|
10
|
+
version: 0.3.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Tomas Celizna
|