locomotive_mongoid_acts_as_tree 0.1.5.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/CHANGELOG ADDED
@@ -0,0 +1,27 @@
1
+ *0.1.4 (July 5th 2010)*
2
+ *parent_id attribute accessor acts as expected
3
+
4
+ *0.1.3 (June 15th, 2010)*
5
+ *patch from joshuabowers (http://github.com/saks/mongoid_acts_as_tree/commit/a98bb62746b71692853a91de7eb35ac55c19bf11)
6
+
7
+ *0.1.2 (April 5th, 2010)*
8
+
9
+ *Dependencies updated
10
+
11
+ *0.1.1 (April 5th, 2010)*
12
+
13
+ *Imroved #children.delete()
14
+
15
+ *Add #children.clear, #children=, #children.replace
16
+
17
+ *Ruby1.8.7p174 compatible
18
+
19
+ *Ruby1.9.1p243 compatible
20
+
21
+
22
+ *0.1.0 (April 4th, 2010)*
23
+
24
+ *Support for Mongoid instead of mongo-mapper
25
+
26
+ *Proxy object #children added with methods: <<, delete
27
+
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Jakob Vidmar
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,48 @@
1
+ = mongoid-acts_as_tree
2
+
3
+ This is an implementation of a tree structure for Mongoid.
4
+
5
+ == Installation
6
+
7
+ Install as gem
8
+
9
+ gem install mongoid_acts_as_tree
10
+
11
+ == Usage
12
+
13
+ Enable the tree functionality by declaring acts_as_tree on your model
14
+
15
+ class Category
16
+ include Mongoid::Document
17
+ include Mongoid::Acts::Tree
18
+
19
+ field :name, :type => String
20
+
21
+ acts_as_tree
22
+ end
23
+
24
+ The method accepts :parent_id_field, :path_field, :depth_field, :order as a hash.
25
+
26
+ :parent_id_field, :path_field, :depth_field => override the default field names
27
+ :order => control the order (format ['value', 'asc'] or [['field_1', 'asc'], ['field_2', 'desc']])
28
+
29
+ Check the test_tree.rb for examples.
30
+
31
+ == About bugs
32
+
33
+ Use it. If you find any bugs, contact me (if possible with a test case) or patch it yourself (see next section).
34
+
35
+ == Note on Patches/Pull Requests
36
+
37
+ * Fork the project.
38
+ * Make your feature addition or bug fix.
39
+ * Add tests for it. This is important so I don't break it in a
40
+ future version unintentionally.
41
+ * Commit, do not mess with rakefile, version, or history.
42
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
43
+ * Send me a pull request. Bonus points for topic branches.
44
+
45
+ == Copyright
46
+
47
+ Copyright (c) 2009 Jakob Vidmar. See LICENSE for details.
48
+
data/Rakefile ADDED
@@ -0,0 +1,48 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/gempackagetask'
4
+
5
+ gemspec = eval(File.read('locomotive_mongoid_acts_as_tree.gemspec'))
6
+ Rake::GemPackageTask.new(gemspec) do |pkg|
7
+ pkg.gem_spec = gemspec
8
+ end
9
+
10
+ desc "build the gem and release it to rubygems.org"
11
+ task :release => :gem do
12
+ sh "gem push pkg/locomotive_mongoid_acts_as_tree-#{gemspec.version}.gem"
13
+ end
14
+
15
+ require 'rake/testtask'
16
+ Rake::TestTask.new(:test) do |test|
17
+ test.libs << 'lib' << 'test'
18
+ test.pattern = 'test/**/test_*.rb'
19
+ test.verbose = true
20
+ end
21
+
22
+ begin
23
+ require 'rcov/rcovtask'
24
+ Rcov::RcovTask.new do |test|
25
+ test.libs << 'test'
26
+ test.pattern = 'test/**/test_*.rb'
27
+ test.verbose = true
28
+ end
29
+ rescue LoadError
30
+ task :rcov do
31
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
32
+ end
33
+ end
34
+
35
+ task :test => :check_dependencies
36
+
37
+ task :default => :test
38
+
39
+ require 'rake/rdoctask'
40
+ Rake::RDocTask.new do |rdoc|
41
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
42
+
43
+ rdoc.rdoc_dir = 'rdoc'
44
+ rdoc.title = "mongoid_acts_as_tree #{version}"
45
+ rdoc.rdoc_files.include('README*')
46
+ rdoc.rdoc_files.include('lib/**/*.rb')
47
+ end
48
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.5.1
@@ -0,0 +1,275 @@
1
+ require "mongoid"
2
+
3
+ module Mongoid
4
+ module Acts
5
+ module Tree
6
+ def self.included(model)
7
+ model.class_eval do
8
+ extend InitializerMethods
9
+ end
10
+ end
11
+
12
+ module InitializerMethods
13
+ def acts_as_tree(options = {})
14
+ options = {
15
+ :parent_id_field => "parent_id",
16
+ :path_field => "path",
17
+ :depth_field => "depth"
18
+ }.merge(options)
19
+
20
+ write_inheritable_attribute :acts_as_tree_options, options
21
+ class_inheritable_reader :acts_as_tree_options
22
+
23
+ include InstanceMethods
24
+ include Fields
25
+ extend Fields
26
+ extend ClassMethods
27
+
28
+ field parent_id_field, :type => BSON::ObjectId
29
+ field path_field, :type => Array, :default => [], :index => true
30
+ field depth_field, :type => Integer, :default => 0
31
+
32
+ self.class_eval do
33
+ define_method "#{parent_id_field}=" do | new_parent_id |
34
+ if new_parent_id.present?
35
+ new_parent = self.class.find new_parent_id
36
+ new_parent.children.push self, false
37
+ else
38
+ self.write_attribute parent_id_field, nil
39
+ self[path_field] = []
40
+ self[depth_field] = 0
41
+ end
42
+ end
43
+ end
44
+
45
+ after_save :move_children
46
+ validate :will_save_tree
47
+ before_destroy :destroy_descendants
48
+ end
49
+ end
50
+
51
+ module ClassMethods
52
+ def roots
53
+ self.where(parent_id_field => nil).order_by tree_order
54
+ end
55
+ end
56
+
57
+ module InstanceMethods
58
+ def [](field_name)
59
+ self.send field_name
60
+ end
61
+
62
+ def []=(field_name, value)
63
+ self.send "#{field_name}=", value
64
+ end
65
+
66
+ def ==(other)
67
+ return true if other.equal?(self)
68
+ return true if other.instance_of?(self.class) and other._id == self._id
69
+ false
70
+ end
71
+
72
+ def will_save_tree
73
+ if @_cyclic
74
+ errors.add(:base, "Can't be children of a descendant")
75
+ end
76
+ end
77
+
78
+ def fix_position
79
+ if parent.nil?
80
+ self.write_attribute parent_id_field, nil
81
+ self[path_field] = []
82
+ self[depth_field] = 0
83
+ else
84
+ self.write_attribute parent_id_field, parent._id
85
+ self[path_field] = parent[path_field] + [parent._id]
86
+ self[depth_field] = parent[depth_field] + 1
87
+ self.save
88
+ end
89
+ end
90
+
91
+ def parent
92
+ @_parent or (self[parent_id_field].nil? ? nil : self.class.find(self[parent_id_field]))
93
+ end
94
+
95
+ def root?
96
+ self[parent_id_field].nil?
97
+ end
98
+
99
+ def root
100
+ self[path_field].first.nil? ? self : self.class.find(self[path_field].first)
101
+ end
102
+
103
+ def ancestors
104
+ return [] if root?
105
+ self.class.where(:_id.in => self[path_field]).order_by(depth_field)
106
+ end
107
+
108
+ def self_and_ancestors
109
+ ancestors << self
110
+ end
111
+
112
+ def siblings
113
+ self.class.where(:_id.ne => self._id, parent_id_field => self[parent_id_field]).order_by tree_order
114
+ end
115
+
116
+ def self_and_siblings
117
+ self.class.where(parent_id_field => self[parent_id_field]).order_by tree_order
118
+ end
119
+
120
+ def children
121
+ Children.new self
122
+ end
123
+
124
+ def children=(new_children_list)
125
+ self.children.clear
126
+ new_children_list.each do | child |
127
+ self.children << child
128
+ end
129
+ end
130
+
131
+ alias replace children=
132
+
133
+ def descendants
134
+ # workorund for mongoid unexpected behavior
135
+ _new_record_var = self.instance_variable_get(:@new_record)
136
+ _new_record = _new_record_var != false
137
+
138
+ return [] if _new_record
139
+ self.class.all_in(path_field => [self._id]).order_by tree_order
140
+ end
141
+
142
+ def self_and_descendants
143
+ [self] + self.descendants
144
+ end
145
+
146
+ def is_ancestor_of?(other)
147
+ other[path_field].include?(self._id)
148
+ end
149
+
150
+ def is_or_is_ancestor_of?(other)
151
+ (other == self) or is_ancestor_of?(other)
152
+ end
153
+
154
+ def is_descendant_of?(other)
155
+ self[path_field].include?(other._id)
156
+ end
157
+
158
+ def is_or_is_descendant_of?(other)
159
+ (other == self) or is_descendant_of?(other)
160
+ end
161
+
162
+ def is_sibling_of?(other)
163
+ (other != self) and (other[parent_id_field] == self[parent_id_field])
164
+ end
165
+
166
+ def is_or_is_sibling_of?(other)
167
+ (other == self) or is_sibling_of?(other)
168
+ end
169
+
170
+ def move_children
171
+
172
+ if @_will_move
173
+ @_will_move = false
174
+ self.children.each do | child |
175
+ child.fix_position
176
+ child.save
177
+ end
178
+ @_will_move = true
179
+ end
180
+ end
181
+
182
+ def destroy_descendants
183
+ self.descendants.each &:destroy
184
+ end
185
+ end
186
+
187
+ #proxy class
188
+ class Children < Array
189
+ #TODO: improve accessors to options to eliminate object[object.parent_id_field]
190
+
191
+ def initialize(owner)
192
+ @parent = owner
193
+ self.concat find_children_for_owner.to_a
194
+ end
195
+
196
+ #Add new child to list of object children
197
+ def <<(object, will_save=true)
198
+ if object.descendants.include? @parent
199
+ object.instance_variable_set :@_cyclic, true
200
+ else
201
+ object.write_attribute object.parent_id_field, @parent._id
202
+ object[object.path_field] = @parent[@parent.path_field] + [@parent._id]
203
+ object[object.depth_field] = @parent[@parent.depth_field] + 1
204
+ object.instance_variable_set :@_will_move, true
205
+ object.save if will_save
206
+ end
207
+
208
+ super(object)
209
+ end
210
+
211
+ def build(attributes)
212
+ child = @parent.class.new(attributes)
213
+ self.push child
214
+ child
215
+ end
216
+
217
+ alias create build
218
+
219
+ alias push <<
220
+
221
+ #Deletes object only from children list.
222
+ #To delete object use <tt>object.destroy</tt>.
223
+ def delete(object_or_id)
224
+ object = case object_or_id
225
+ when String, BSON::ObjectId
226
+ @parent.class.find object_or_id
227
+ else
228
+ object_or_id
229
+ end
230
+
231
+ object.write_attribute object.parent_id_field, nil
232
+ object[object.path_field] = []
233
+ object[object.depth_field] = 0
234
+ object.save
235
+
236
+ super(object)
237
+ end
238
+
239
+ #Clear children list
240
+ def clear
241
+ self.each do | child |
242
+ @parent.children.delete child
243
+ end
244
+ end
245
+
246
+ private
247
+
248
+ def find_children_for_owner
249
+ @parent.class.where(@parent.parent_id_field => @parent.id).
250
+ order_by @parent.tree_order
251
+ end
252
+
253
+ end
254
+
255
+ module Fields
256
+ def parent_id_field
257
+ acts_as_tree_options[:parent_id_field]
258
+ end
259
+
260
+ def path_field
261
+ acts_as_tree_options[:path_field]
262
+ end
263
+
264
+ def depth_field
265
+ acts_as_tree_options[:depth_field]
266
+ end
267
+
268
+ def tree_order
269
+ acts_as_tree_options[:order] or []
270
+ end
271
+ end
272
+ end
273
+ end
274
+ end
275
+
@@ -0,0 +1,68 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{locomotive_mongoid_acts_as_tree}
8
+ s.version = "0.1.5.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Jakob Vidmar, Aliaksandr Rahalevich"]
12
+ s.date = %q{2010-10-13}
13
+ s.description = %q{Port of the old, venerable ActsAsTree with a bit of a twist}
14
+ s.email = ["saksmlz@gmail.com", "didier@nocoffee.fr"]
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "CHANGELOG",
23
+ "LICENSE",
24
+ "README.rdoc",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "lib/mongoid_acts_as_tree.rb",
28
+ "locomotive_mongoid_acts_as_tree.gemspec",
29
+ "mongoid_acts_as_tree.gemspec",
30
+ "test/helper.rb",
31
+ "test/models/category.rb",
32
+ "test/models/ordered_category.rb",
33
+ "test/test_order.rb",
34
+ "test/test_tree.rb"
35
+ ]
36
+ s.homepage = %q{http://github.com/saks/mongoid_acts_as_tree}
37
+ s.rdoc_options = ["--charset=UTF-8"]
38
+ s.require_paths = ["lib"]
39
+ s.rubygems_version = %q{1.3.7}
40
+ s.summary = %q{ActsAsTree plugin for Mongoid}
41
+ s.test_files = [
42
+ "test/helper.rb",
43
+ "test/models/category.rb",
44
+ "test/models/ordered_category.rb",
45
+ "test/test_order.rb",
46
+ "test/test_tree.rb"
47
+ ]
48
+
49
+ if s.respond_to? :specification_version then
50
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
51
+ s.specification_version = 3
52
+
53
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
54
+ s.add_runtime_dependency(%q<mongoid>, ["<= 2.0.0.beta.19"])
55
+ s.add_runtime_dependency(%q<bson>, [">= 0.20.1"])
56
+ s.add_development_dependency(%q<shoulda>, [">= 2.10.2"])
57
+ else
58
+ s.add_dependency(%q<mongoid>, ["<= 2.0.0.beta.19"])
59
+ s.add_dependency(%q<bson>, [">= 0.20.1"])
60
+ s.add_dependency(%q<shoulda>, [">= 2.10.2"])
61
+ end
62
+ else
63
+ s.add_dependency(%q<mongoid>, ["<= 2.0.0.beta.19"])
64
+ s.add_dependency(%q<bson>, [">= 0.20.1"])
65
+ s.add_dependency(%q<shoulda>, [">= 2.10.2"])
66
+ end
67
+ end
68
+
@@ -0,0 +1,67 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{mongoid_acts_as_tree}
8
+ s.version = "0.1.5.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Jakob Vidmar, Aliaksandr Rahalevich"]
12
+ s.date = %q{2010-10-13}
13
+ s.description = %q{Port of the old, venerable ActsAsTree with a bit of a twist}
14
+ s.email = ["saksmlz@gmail.com", "didier@nocoffee.fr"]
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "CHANGELOG",
23
+ "LICENSE",
24
+ "README.rdoc",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "lib/mongoid_acts_as_tree.rb",
28
+ "mongoid_acts_as_tree.gemspec",
29
+ "test/helper.rb",
30
+ "test/models/category.rb",
31
+ "test/models/ordered_category.rb",
32
+ "test/test_order.rb",
33
+ "test/test_tree.rb"
34
+ ]
35
+ s.homepage = %q{http://github.com/saks/mongoid_acts_as_tree}
36
+ s.rdoc_options = ["--charset=UTF-8"]
37
+ s.require_paths = ["lib"]
38
+ s.rubygems_version = %q{1.3.7}
39
+ s.summary = %q{ActsAsTree plugin for Mongoid}
40
+ s.test_files = [
41
+ "test/helper.rb",
42
+ "test/models/category.rb",
43
+ "test/models/ordered_category.rb",
44
+ "test/test_order.rb",
45
+ "test/test_tree.rb"
46
+ ]
47
+
48
+ if s.respond_to? :specification_version then
49
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
50
+ s.specification_version = 3
51
+
52
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
53
+ s.add_runtime_dependency(%q<mongoid>, ["<= 2.0.0.beta.19"])
54
+ s.add_runtime_dependency(%q<bson>, [">= 0.20.1"])
55
+ s.add_development_dependency(%q<shoulda>, [">= 2.10.2"])
56
+ else
57
+ s.add_dependency(%q<mongoid>, ["<= 2.0.0.beta.19"])
58
+ s.add_dependency(%q<bson>, [">= 0.20.1"])
59
+ s.add_dependency(%q<shoulda>, [">= 2.10.2"])
60
+ end
61
+ else
62
+ s.add_dependency(%q<mongoid>, ["<= 2.0.0.beta.19"])
63
+ s.add_dependency(%q<bson>, [">= 0.20.1"])
64
+ s.add_dependency(%q<shoulda>, [">= 2.10.2"])
65
+ end
66
+ end
67
+
data/test/helper.rb ADDED
@@ -0,0 +1,33 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'mongoid'
8
+
9
+ Mongoid.configure.master = Mongo::Connection.new.db('acts_as_tree-test')
10
+
11
+ Dir["#{File.dirname(__FILE__)}/models/*.rb"].each {|file| require file}
12
+
13
+ class Test::Unit::TestCase
14
+ # Drop all columns after each test case.
15
+ def teardown
16
+ Mongoid.database.collections.each do |coll|
17
+ coll.remove
18
+ end
19
+ end
20
+
21
+ # Make sure that each test case has a teardown
22
+ # method to clear the db after each test.
23
+ def inherited(base)
24
+ base.define_method teardown do
25
+ super
26
+ end
27
+ end
28
+
29
+ def eql_arrays?(first, second)
30
+ first.map{|i| i._id}.to_set == second.map{|i| i._id}.to_set
31
+ end
32
+ end
33
+
@@ -0,0 +1,12 @@
1
+ require "mongoid"
2
+ require "mongoid_acts_as_tree"
3
+
4
+ class Category
5
+ include Mongoid::Document
6
+ include Mongoid::Acts::Tree
7
+
8
+ field :name, :type => String
9
+
10
+ acts_as_tree
11
+ end
12
+
@@ -0,0 +1,13 @@
1
+ require "mongoid"
2
+ require "mongoid_acts_as_tree"
3
+
4
+ class OrderedCategory
5
+ include Mongoid::Document
6
+ include Mongoid::Acts::Tree
7
+
8
+ field :name, :type => String
9
+ field :value, :type => Integer
10
+
11
+ acts_as_tree :order => [['value', 'asc']]
12
+ end
13
+
@@ -0,0 +1,35 @@
1
+ require 'helper'
2
+ require 'set'
3
+
4
+ class TestMongoidActsAsTree < Test::Unit::TestCase
5
+ context "Ordered tree" do
6
+ setup do
7
+ @root_1 = OrderedCategory.create(:name => "Root 1", :value => 2)
8
+ @child_1 = OrderedCategory.create(:name => "Child 1", :value => 1)
9
+ @child_2 = OrderedCategory.create(:name => "Child 2", :value => 9)
10
+ @child_2_1 = OrderedCategory.create(:name => "Child 2.1", :value => 2)
11
+
12
+ @child_3 = OrderedCategory.create(:name => "Child 3", :value => 5)
13
+ @root_2 = OrderedCategory.create(:name => "Root 2", :value => 1)
14
+
15
+ @root_1.children << @child_1
16
+ @root_1.children << @child_2
17
+ @root_1.children << @child_3
18
+
19
+ @child_2.children << @child_2_1
20
+ end
21
+
22
+ should "be in order" do
23
+ assert_equal OrderedCategory.roots.to_a, [@root_2, @root_1]
24
+ assert_equal @root_1.children, [@child_1, @child_3, @child_2]
25
+
26
+ assert_equal @root_1.descendants, [@child_1, @child_2_1, @child_3, @child_2]
27
+ assert_equal @root_1.self_and_descendants, [@root_1, @child_1, @child_2_1, @child_3, @child_2]
28
+
29
+ assert_equal @child_2.siblings, [@child_1, @child_3]
30
+ assert_equal @child_2.self_and_siblings, [@child_1, @child_3, @child_2]
31
+ assert_equal @root_1.self_and_siblings, [@root_2, @root_1]
32
+ end
33
+ end
34
+ end
35
+
data/test/test_tree.rb ADDED
@@ -0,0 +1,237 @@
1
+ require 'helper'
2
+ require 'set'
3
+
4
+ $verbose = false
5
+
6
+ class TestMongoidActsAsTree < Test::Unit::TestCase
7
+ context "Tree" do
8
+ setup do
9
+ @root_1 = Category.create(:name => "Root 1")
10
+ @child_1 = Category.create(:name => "Child 1")
11
+ @child_2 = Category.create(:name => "Child 2")
12
+ @child_2_1 = Category.create(:name => "Child 2.1")
13
+
14
+ @child_3 = Category.create(:name => "Child 3")
15
+ @root_2 = Category.create(:name => "Root 2")
16
+
17
+ @root_1.children << @child_1
18
+ @root_1.children << @child_2
19
+ @root_1.children << @child_3
20
+
21
+ @child_2.children << @child_2_1
22
+ end
23
+
24
+ should "add child via create or build" do
25
+ @root_1.children.build :name => "Child 2.2"
26
+ assert Category.where(:name => "Child 2.2").first.parent == @root_1
27
+ end
28
+
29
+ should "add child via <<" do
30
+ child = Category.create(:name => "Child 2.2")
31
+ @root_1.children << child
32
+ assert child.parent == @root_1
33
+ end
34
+
35
+ should "delete child" do
36
+ @root_1.children.delete @child_1
37
+ assert_equal(2, @root_1.children.size)
38
+ @root_1.children.delete @child_2.id
39
+ assert_equal(@child_3, @root_1.children.first)
40
+ end
41
+
42
+ should "clear children list" do
43
+ @root_1.children.clear
44
+ assert_equal([], @root_1.children)
45
+ end
46
+
47
+ should "replace children list" do
48
+ new_children_list = [Category.create(:name => "test 1"), Category.create(:name => "test 2")]
49
+
50
+ @root_1.children = new_children_list
51
+ assert_equal(new_children_list, @root_1.children)
52
+
53
+ @root_1.children = []
54
+ assert_equal([], @root_1.children)
55
+ end
56
+
57
+ should "have roots" do
58
+ assert eql_arrays?(Category.roots, [@root_1, @root_2])
59
+ end
60
+
61
+ should "assign parent_id" do
62
+ child = Category.create :name => 'child'
63
+ parent = Category.create :name => 'parent'
64
+
65
+ child.parent_id = parent.id
66
+ child.save
67
+
68
+ assert_equal parent.children.first.id, child.id
69
+ assert_equal parent.id, child.parent_id
70
+ assert parent.children.include? child
71
+
72
+ assert_equal 1, child.depth
73
+ assert_equal [parent.id], child.path
74
+
75
+ more_deep_child = Category.new(
76
+ :name => 'more deep child',
77
+ :parent_id => child.id
78
+ )
79
+
80
+ assert more_deep_child.new_record?
81
+ more_deep_child.save
82
+ assert !more_deep_child.new_record?
83
+
84
+ assert_equal child.children.first.id, more_deep_child.id
85
+ assert_equal child.id, more_deep_child.parent_id
86
+ assert child.children.include? more_deep_child
87
+
88
+ assert_equal 2, more_deep_child.depth
89
+ assert_equal [parent.id, child.id], more_deep_child.path
90
+
91
+ assert parent.descendants.include? child
92
+ assert parent.descendants.include? more_deep_child
93
+
94
+ assert more_deep_child.ancestors.include? child
95
+ assert more_deep_child.ancestors.include? parent
96
+ end
97
+
98
+ should "assign blank parent_id" do
99
+ @child_1.parent_id = ''
100
+ @child_1.save
101
+
102
+ assert_nil @child_1.reload.parent_id
103
+ assert_equal 0, @child_1.depth
104
+ assert_equal [], @child_1.path
105
+
106
+ @child_1.parent_id = nil
107
+ @child_1.save
108
+
109
+ assert_nil @child_1.reload.parent_id
110
+ assert_equal 0, @child_1.depth
111
+ assert_equal [], @child_1.path
112
+ end
113
+
114
+ context "node" do
115
+ should "have a root" do
116
+ assert_equal @root_1.root, @root_1
117
+ assert_not_equal @root_1.root, @root_2.root
118
+ assert_equal @root_1, @child_2_1.root
119
+ end
120
+
121
+ should "have ancestors" do
122
+ assert_equal @root_1.ancestors, []
123
+ assert_equal @child_2.ancestors, [@root_1]
124
+ assert_equal @child_2_1.ancestors, [@root_1, @child_2]
125
+ assert_equal @root_1.self_and_ancestors, [@root_1]
126
+ assert_equal @child_2.self_and_ancestors, [@root_1, @child_2]
127
+ assert_equal @child_2_1.self_and_ancestors, [@root_1, @child_2, @child_2_1]
128
+ end
129
+
130
+ should "have siblings" do
131
+ assert eql_arrays?(@root_1.siblings, [@root_2])
132
+ assert eql_arrays?(@child_2.siblings, [@child_1, @child_3])
133
+ assert eql_arrays?(@child_2_1.siblings, [])
134
+ assert eql_arrays?(@root_1.self_and_siblings, [@root_1, @root_2])
135
+ assert eql_arrays?(@child_2.self_and_siblings, [@child_1, @child_2, @child_3])
136
+ assert eql_arrays?(@child_2_1.self_and_siblings, [@child_2_1])
137
+ end
138
+
139
+ should "set depth" do
140
+ assert_equal 0, @root_1.depth
141
+ assert_equal 1, @child_1.depth
142
+ assert_equal 2, @child_2_1.depth
143
+ end
144
+
145
+ should "have children" do
146
+ assert @child_2_1.children.empty?
147
+ assert eql_arrays?(@root_1.children, [@child_1, @child_2, @child_3])
148
+ end
149
+
150
+ should "have descendants" do
151
+ assert eql_arrays?(@root_1.descendants, [@child_1, @child_2, @child_3, @child_2_1])
152
+ assert eql_arrays?(@child_2.descendants, [@child_2_1])
153
+ assert @child_2_1.descendants.empty?
154
+ assert eql_arrays?(@root_1.self_and_descendants, [@root_1, @child_1, @child_2, @child_3, @child_2_1])
155
+ assert eql_arrays?(@child_2.self_and_descendants, [@child_2, @child_2_1])
156
+ assert eql_arrays?(@child_2_1.self_and_descendants, [@child_2_1])
157
+ end
158
+
159
+ should "be able to tell if ancestor" do
160
+ assert @root_1.is_ancestor_of?(@child_1)
161
+ assert !@root_2.is_ancestor_of?(@child_2_1)
162
+ assert !@child_2.is_ancestor_of?(@child_2)
163
+
164
+ assert @root_1.is_or_is_ancestor_of?(@child_1)
165
+ assert !@root_2.is_or_is_ancestor_of?(@child_2_1)
166
+ assert @child_2.is_or_is_ancestor_of?(@child_2)
167
+ end
168
+
169
+ should "be able to tell if descendant" do
170
+ assert !@root_1.is_descendant_of?(@child_1)
171
+ assert @child_1.is_descendant_of?(@root_1)
172
+ assert !@child_2.is_descendant_of?(@child_2)
173
+
174
+ assert !@root_1.is_or_is_descendant_of?(@child_1)
175
+ assert @child_1.is_or_is_descendant_of?(@root_1)
176
+ assert @child_2.is_or_is_descendant_of?(@child_2)
177
+ end
178
+
179
+ should "be able to tell if sibling" do
180
+ assert !@root_1.is_sibling_of?(@child_1)
181
+ assert !@child_1.is_sibling_of?(@child_1)
182
+ assert !@child_2.is_sibling_of?(@child_2)
183
+
184
+ assert !@root_1.is_or_is_sibling_of?(@child_1)
185
+ assert @child_1.is_or_is_sibling_of?(@child_2)
186
+ assert @child_2.is_or_is_sibling_of?(@child_2)
187
+ end
188
+
189
+ context "when moving" do
190
+ should "recalculate path and depth" do
191
+ @child_2.children << @child_3
192
+ @child_3.save
193
+
194
+ assert @child_2.is_or_is_ancestor_of?(@child_3)
195
+ assert @child_3.is_or_is_descendant_of?(@child_2)
196
+ assert @child_2.children.include?(@child_3)
197
+ assert @child_2.descendants.include?(@child_3)
198
+ assert @child_2_1.is_or_is_sibling_of?(@child_3)
199
+ assert_equal 2, @child_3.depth
200
+ end
201
+
202
+ should "move children on save" do
203
+ @root_2.children << @child_2
204
+
205
+ @child_2_1.reload
206
+
207
+ assert @root_2.is_or_is_ancestor_of?(@child_2_1)
208
+ assert @child_2_1.is_or_is_descendant_of?(@root_2)
209
+ assert @root_2.descendants.include?(@child_2_1)
210
+ end
211
+
212
+ should "check against cyclic graph" do
213
+ @child_2_1.children << @root_1
214
+ assert !@root_1.save
215
+ end
216
+ end
217
+
218
+ should "destroy descendants when destroyed" do
219
+ @child_2.destroy
220
+ assert_nil Category.where(:id => @child_2_1._id).first
221
+ end
222
+ end
223
+
224
+ context "root node" do
225
+ should "not have a parent" do
226
+ assert_nil @root_1.parent
227
+ end
228
+ end
229
+
230
+ context "child_node" do
231
+ should "have a parent" do
232
+ assert_equal @child_2, @child_2_1.parent
233
+ end
234
+ end
235
+ end
236
+ end
237
+
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: locomotive_mongoid_acts_as_tree
3
+ version: !ruby/object:Gem::Version
4
+ hash: 81
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 5
10
+ - 1
11
+ version: 0.1.5.1
12
+ platform: ruby
13
+ authors:
14
+ - Jakob Vidmar, Aliaksandr Rahalevich
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2010-10-13 00:00:00 +02:00
20
+ default_executable:
21
+ dependencies:
22
+ - !ruby/object:Gem::Dependency
23
+ name: mongoid
24
+ prerelease: false
25
+ requirement: &id001 !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - <=
29
+ - !ruby/object:Gem::Version
30
+ hash: 62196421
31
+ segments:
32
+ - 2
33
+ - 0
34
+ - 0
35
+ - beta
36
+ - 19
37
+ version: 2.0.0.beta.19
38
+ type: :runtime
39
+ version_requirements: *id001
40
+ - !ruby/object:Gem::Dependency
41
+ name: bson
42
+ prerelease: false
43
+ requirement: &id002 !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ hash: 77
49
+ segments:
50
+ - 0
51
+ - 20
52
+ - 1
53
+ version: 0.20.1
54
+ type: :runtime
55
+ version_requirements: *id002
56
+ - !ruby/object:Gem::Dependency
57
+ name: shoulda
58
+ prerelease: false
59
+ requirement: &id003 !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ hash: 35
65
+ segments:
66
+ - 2
67
+ - 10
68
+ - 2
69
+ version: 2.10.2
70
+ type: :development
71
+ version_requirements: *id003
72
+ description: Port of the old, venerable ActsAsTree with a bit of a twist
73
+ email:
74
+ - saksmlz@gmail.com
75
+ - didier@nocoffee.fr
76
+ executables: []
77
+
78
+ extensions: []
79
+
80
+ extra_rdoc_files:
81
+ - LICENSE
82
+ - README.rdoc
83
+ files:
84
+ - .document
85
+ - .gitignore
86
+ - CHANGELOG
87
+ - LICENSE
88
+ - README.rdoc
89
+ - Rakefile
90
+ - VERSION
91
+ - lib/mongoid_acts_as_tree.rb
92
+ - locomotive_mongoid_acts_as_tree.gemspec
93
+ - mongoid_acts_as_tree.gemspec
94
+ - test/helper.rb
95
+ - test/models/category.rb
96
+ - test/models/ordered_category.rb
97
+ - test/test_order.rb
98
+ - test/test_tree.rb
99
+ has_rdoc: true
100
+ homepage: http://github.com/saks/mongoid_acts_as_tree
101
+ licenses: []
102
+
103
+ post_install_message:
104
+ rdoc_options:
105
+ - --charset=UTF-8
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ none: false
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ hash: 3
114
+ segments:
115
+ - 0
116
+ version: "0"
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ none: false
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ hash: 3
123
+ segments:
124
+ - 0
125
+ version: "0"
126
+ requirements: []
127
+
128
+ rubyforge_project:
129
+ rubygems_version: 1.3.7
130
+ signing_key:
131
+ specification_version: 3
132
+ summary: ActsAsTree plugin for Mongoid
133
+ test_files:
134
+ - test/helper.rb
135
+ - test/models/category.rb
136
+ - test/models/ordered_category.rb
137
+ - test/test_order.rb
138
+ - test/test_tree.rb