mongoid_acts_as_tree 0.1.0 → 0.1.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/CHANGELOG ADDED
@@ -0,0 +1,17 @@
1
+ *0.1.1 (April 5th, 2010)*
2
+
3
+ *Imroved #children.delete()
4
+
5
+ *Add #children.clear, #children=, #children.replace
6
+
7
+ *Ruby1.8.7p174 compatible
8
+
9
+ *Ruby1.9.1p243 compatible
10
+
11
+
12
+ *0.1.0 (April 4th, 2010)*
13
+
14
+ *Support for Mongoid instead of mongo-mapper
15
+
16
+ *Proxy object #children added with methods: <<, delete
17
+
data/README.rdoc CHANGED
@@ -16,7 +16,7 @@ Enable the tree functionality by declaring acts_as_tree on your model
16
16
  include Mongoid::Document
17
17
  include Mongoid::Acts::Tree
18
18
 
19
- field :name, String
19
+ field :name, :type => String
20
20
 
21
21
  acts_as_tree
22
22
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
@@ -108,6 +108,15 @@ module Mongoid
108
108
  Children.new self
109
109
  end
110
110
 
111
+ def children=(new_children_list)
112
+ self.children.clear
113
+ new_children_list.each do | child |
114
+ self.children << child
115
+ end
116
+ end
117
+
118
+ alias replace children=
119
+
111
120
  def descendants
112
121
  return [] if new_record?
113
122
  self.class.where(path_field => self._id).order_by tree_order
@@ -145,7 +154,7 @@ module Mongoid
145
154
 
146
155
  if @_will_move
147
156
  @_will_move = false
148
- for child in self.children
157
+ self.children.each do | child |
149
158
  child.fix_position
150
159
  child.save
151
160
  end
@@ -160,17 +169,14 @@ module Mongoid
160
169
 
161
170
  #proxy class
162
171
  class Children < Array
172
+ #TODO: improve accessors to options to eliminate object[object.parent_id_field]
163
173
 
164
174
  def initialize(owner)
165
175
  @parent = owner
166
176
  self.concat find_children_for_owner.to_a
167
177
  end
168
178
 
169
- def find_children_for_owner
170
- @parent.class.where(@parent.parent_id_field => @parent.id).
171
- order_by @parent.tree_order
172
- end
173
-
179
+ #Add new child to list of object children
174
180
  def <<(object)
175
181
  if object.descendants.include? @parent
176
182
  object.instance_variable_set :@_cyclic, true
@@ -185,20 +191,38 @@ module Mongoid
185
191
  super(object)
186
192
  end
187
193
 
194
+ #Deletes object only from children list.
195
+ #To delete object use <tt>object.destroy</tt>.
188
196
  def delete(object_or_id)
189
197
  object = case object_or_id
190
198
  when String
191
- @parent.class.where(:id => object_or_id)
199
+ @parent.class.find object_or_id
192
200
  else
193
201
  object_or_id
194
202
  end
195
203
 
196
- object._parent_id = nil
197
- object._parent_ids = (object._parent_ids || []) - [@parent.id]
204
+ object[object.parent_id_field] = nil
205
+ object[object.path_field] = []
206
+ object[object.depth_field] = 0
198
207
  object.save
208
+
199
209
  super(object)
200
210
  end
201
211
 
212
+ #Clear children list
213
+ def clear
214
+ self.each do | child |
215
+ @parent.children.delete child
216
+ end
217
+ end
218
+
219
+ private
220
+
221
+ def find_children_for_owner
222
+ @parent.class.where(@parent.parent_id_field => @parent.id).
223
+ order_by @parent.tree_order
224
+ end
225
+
202
226
  end
203
227
 
204
228
  module Fields
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mongoid_acts_as_tree}
8
- s.version = "0.1.0"
8
+ s.version = "0.1.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, Aliaksandr Rahalevich"]
12
- s.date = %q{2010-04-04}
12
+ s.date = %q{2010-04-05}
13
13
  s.description = %q{Port of the old, venerable ActsAsTree with a bit of a twist}
14
14
  s.email = %q{saksmlz@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -19,6 +19,7 @@ Gem::Specification.new do |s|
19
19
  s.files = [
20
20
  ".document",
21
21
  ".gitignore",
22
+ "CHANGELOG",
22
23
  "LICENSE",
23
24
  "README.rdoc",
24
25
  "Rakefile",
data/test/test_tree.rb CHANGED
@@ -27,6 +27,28 @@ class TestMongoidActsAsTree < Test::Unit::TestCase
27
27
  assert child.parent == @root_1
28
28
  end
29
29
 
30
+ should "delete child" do
31
+ @root_1.children.delete @child_1
32
+ assert_equal(2, @root_1.children.size)
33
+ @root_1.children.delete @child_2.id
34
+ assert_equal(@child_3, @root_1.children.first)
35
+ end
36
+
37
+ should "clear children list" do
38
+ @root_1.children.clear
39
+ assert_equal([], @root_1.children)
40
+ end
41
+
42
+ should "replace children list" do
43
+ new_children_list = [Category.create(:name => "test 1"), Category.create(:name => "test 2")]
44
+
45
+ @root_1.children = new_children_list
46
+ assert_equal(new_children_list, @root_1.children)
47
+
48
+ @root_1.children = []
49
+ assert_equal([], @root_1.children)
50
+ end
51
+
30
52
  should "have roots" do
31
53
  assert eql_arrays?(Category.roots, [@root_1, @root_2])
32
54
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 0
9
- version: 0.1.0
8
+ - 1
9
+ version: 0.1.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Jakob Vidmar, Aliaksandr Rahalevich
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-04-04 00:00:00 +03:00
17
+ date: 2010-04-05 00:00:00 +03:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -57,6 +57,7 @@ extra_rdoc_files:
57
57
  files:
58
58
  - .document
59
59
  - .gitignore
60
+ - CHANGELOG
60
61
  - LICENSE
61
62
  - README.rdoc
62
63
  - Rakefile