mongoid-tree 1.0.4 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: caf4b036f06eefaca63f1c88fd04f4acb8d17173
4
- data.tar.gz: 32a71b36f7e617c50a0bed809a5cc0229abe6ce6
3
+ metadata.gz: 5137d0df514633dff660618929df94658352c576
4
+ data.tar.gz: cc8bc0884e0d028b762d364ede0fca32b07e1e53
5
5
  SHA512:
6
- metadata.gz: 1a57abc31fc56609eb406ed5278870945e48b666652dbf2cb824ce71f9cd0d0ba104945bbab8cfa2222e343e06b159d484dd14bc30facc3ec09fe4a8284cb71d
7
- data.tar.gz: dbfd2350fbdeae186273be215fffbaca14a747b14ca07cb5200ae75a169964a47e706d9b8e9238b1f092ddc2a279d75d596e9bc83df8621323382578e3349e1f
6
+ metadata.gz: 6533082d31a625ccb70529cdd4391ce9f0a934239efe3c45c1299a9e2762db2d4af26933a2b48bc3852b982b1f9a63174e486dedd714fba872a58e46f4f60699
7
+ data.tar.gz: cd4b110278a8b0cf46be05a721eafc9428e0a3260e7434b72668ceea2a4f2327051967df4739c78f678a60fb19cc8b76f8c6223e73591b6f49faf5b3222bffd8
data/README.md CHANGED
@@ -23,6 +23,9 @@ You might want to remove the `:require => 'mongoid/tree'` option and explicitly
23
23
 
24
24
  bundle install
25
25
 
26
+ ### Upgrade from mongoid-tree 1.0.x
27
+
28
+ To fix issues with the ordering of ancestors, mongoid-tree 1.1 introduces a new depth field to the documents that include the Mongoid::Tree module. In case your project uses its own depth field, you can now rely on mongoid-tree to handle this.
26
29
 
27
30
  ## Usage
28
31
 
data/lib/mongoid/tree.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require 'active_support/concern'
2
+
1
3
  module Mongoid
2
4
  ##
3
5
  # = Mongoid::Tree
@@ -90,6 +92,9 @@ module Mongoid
90
92
  field :parent_ids, :type => Array, :default => []
91
93
  index :parent_ids => 1
92
94
 
95
+ field :depth, :type => Integer
96
+ index :depth => 1
97
+
93
98
  set_callback :save, :after, :rearrange_children, :if => :rearrange_children?
94
99
  set_callback :validation, :before do
95
100
  run_callbacks(:rearrange) { rearrange }
@@ -222,6 +227,18 @@ module Mongoid
222
227
  #
223
228
  # @return [Array<BSON::ObjectId>] The ids of the document's ancestors
224
229
 
230
+ ##
231
+ # Returns the depth of this document (number of ancestors)
232
+ #
233
+ # @example
234
+ # Node.root.depth # => 0
235
+ # Node.root.children.first.depth # => 1
236
+ #
237
+ # @return [Fixnum] Depth of this document
238
+ def depth
239
+ super || parent_ids.count
240
+ end
241
+
225
242
  ##
226
243
  # Is this document a root node (has no parent)?
227
244
  #
@@ -238,18 +255,6 @@ module Mongoid
238
255
  children.empty?
239
256
  end
240
257
 
241
- ##
242
- # Returns the depth of this document (number of ancestors)
243
- #
244
- # @example
245
- # Node.root.depth # => 0
246
- # Node.root.children.first.depth # => 1
247
- #
248
- # @return [Fixnum] Depth of this document
249
- def depth
250
- parent_ids.count
251
- end
252
-
253
258
  ##
254
259
  # Returns this document's root node. Returns `self` if the
255
260
  # current document is a root node
@@ -272,13 +277,7 @@ module Mongoid
272
277
  #
273
278
  # @return [Mongoid::Criteria] Mongoid criteria to retrieve the documents ancestors
274
279
  def ancestors
275
- if parent_ids.any?
276
- base_class.and({
277
- '$or' => parent_ids.map { |id| { :_id => id } }
278
- })
279
- else
280
- base_class.where(:_id.in => [])
281
- end
280
+ base_class.where(:_id.in => parent_ids).asc(:depth)
282
281
  end
283
282
 
284
283
  ##
@@ -428,6 +427,8 @@ module Mongoid
428
427
  self.parent_ids = []
429
428
  end
430
429
 
430
+ self.depth = parent_ids.size
431
+
431
432
  rearrange_children! if self.parent_ids_changed?
432
433
  end
433
434
 
@@ -29,6 +29,13 @@ describe Mongoid::Tree do
29
29
  expect(Node.index_options).to have_key(:parent_ids => 1)
30
30
  end
31
31
 
32
+ it "should store the depth as Integer with index" do
33
+ f = Node.fields['depth']
34
+ expect(f).to be
35
+ expect(f.options[:type]).to eq(Integer)
36
+ expect(Node.index_options).to have_key(:depth => 1)
37
+ end
38
+
32
39
  describe 'when new' do
33
40
  it "should not require a saved parent when adding children" do
34
41
  root = Node.new(:name => 'root'); child = Node.new(:name => 'child')
@@ -254,6 +261,15 @@ describe Mongoid::Tree do
254
261
  expect(node(:child).depth).to eq(1)
255
262
  expect(node(:subchild).depth).to eq(2)
256
263
  end
264
+
265
+ it "should be updated when the nodes ancestors change" do
266
+ node(:child).update_attributes!(:parent => nil)
267
+
268
+ puts node(:child).inspect
269
+
270
+ expect(node(:child).reload.depth).to eq(0)
271
+ expect(node(:subchild).depth).to eq(1)
272
+ end
257
273
  end
258
274
 
259
275
  describe '#root' do
@@ -5,7 +5,7 @@ class Node
5
5
 
6
6
  field :name
7
7
 
8
- attr_accessible :name
8
+ attr_accessible :name, :parent
9
9
  end
10
10
 
11
11
  class SubclassedNode < Node
metadata CHANGED
@@ -1,20 +1,20 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid-tree
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Benedikt Deicke
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-20 00:00:00.000000000 Z
11
+ date: 2014-06-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mongoid
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - <=
17
+ - - <
18
18
  - !ruby/object:Gem::Version
19
19
  version: '4.0'
20
20
  - - '>='
@@ -24,7 +24,7 @@ dependencies:
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
- - - <=
27
+ - - <
28
28
  - !ruby/object:Gem::Version
29
29
  version: '4.0'
30
30
  - - '>='