mongoid-tree 0.2.0 → 0.2.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/Gemfile.lock ADDED
@@ -0,0 +1,56 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ mongoid-tree (0.2.1)
5
+ mongoid (>= 2.0.0.beta.14)
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ activemodel (3.0.0.rc)
11
+ activesupport (= 3.0.0.rc)
12
+ builder (~> 2.1.2)
13
+ i18n (~> 0.4.1)
14
+ activesupport (3.0.0.rc)
15
+ autotest (4.3.2)
16
+ bson (1.0.4)
17
+ bson_ext (1.0.4)
18
+ builder (2.1.2)
19
+ diff-lcs (1.1.2)
20
+ haml (2.2.24)
21
+ hanna (0.1.12)
22
+ haml (~> 2.2.8)
23
+ rake (~> 0.8.2)
24
+ rdoc (~> 2.3.0)
25
+ i18n (0.4.1)
26
+ mongo (1.0.6)
27
+ bson (>= 1.0.4)
28
+ mongoid (2.0.0.beta.14)
29
+ activemodel (= 3.0.0.rc)
30
+ bson (= 1.0.4)
31
+ mongo (= 1.0.6)
32
+ tzinfo (= 0.3.22)
33
+ will_paginate (~> 3.0.pre)
34
+ rake (0.8.7)
35
+ rdoc (2.3.0)
36
+ rspec (2.0.0.beta.19)
37
+ rspec-core (= 2.0.0.beta.19)
38
+ rspec-expectations (= 2.0.0.beta.19)
39
+ rspec-mocks (= 2.0.0.beta.19)
40
+ rspec-core (2.0.0.beta.19)
41
+ rspec-expectations (2.0.0.beta.19)
42
+ diff-lcs (>= 1.1.2)
43
+ rspec-mocks (2.0.0.beta.19)
44
+ tzinfo (0.3.22)
45
+ will_paginate (3.0.pre2)
46
+
47
+ PLATFORMS
48
+ ruby
49
+
50
+ DEPENDENCIES
51
+ autotest (>= 4.3.2)
52
+ bson_ext (>= 1.0.4)
53
+ hanna (>= 0.1.12)
54
+ mongoid (>= 2.0.0.beta.14)
55
+ mongoid-tree!
56
+ rspec (>= 2.0.0.beta.18)
data/README.rdoc CHANGED
@@ -4,7 +4,7 @@ A tree structure for Mongoid documents using the materialized path pattern
4
4
 
5
5
  == Requirements
6
6
 
7
- * mongoid (>= 2.0.0.beta9)
7
+ * mongoid (>= 2.0.0.beta.14)
8
8
 
9
9
  == Install
10
10
 
data/lib/mongoid/tree.rb CHANGED
@@ -63,15 +63,18 @@ module Mongoid # :nodoc:
63
63
 
64
64
  included do
65
65
  references_many :children, :class_name => self.name, :foreign_key => :parent_id, :inverse_of => :parent
66
- referenced_in :parent, :class_name => self.name, :inverse_of => :children
66
+ referenced_in :parent, :class_name => self.name, :inverse_of => :children, :index => true
67
67
 
68
68
  field :parent_ids, :type => Array, :default => []
69
+ index :parent_ids
69
70
 
70
71
  set_callback :save, :after, :rearrange_children, :if => :rearrange_children?
71
72
  set_callback :validation, :before do
72
73
  run_callbacks(:rearrange) { rearrange }
73
74
  end
74
75
 
76
+ validate :position_in_tree
77
+
75
78
  define_model_callbacks :rearrange, :only => [:before, :after]
76
79
  end
77
80
 
@@ -242,5 +245,10 @@ module Mongoid # :nodoc:
242
245
  @rearrange_children = false
243
246
  self.children.find(:all).each { |c| c.save }
244
247
  end
248
+
249
+ def position_in_tree
250
+ errors.add(:parent_id, :invalid) if self.parent_ids.include?(self.id)
251
+ end
252
+
245
253
  end
246
254
  end
@@ -2,13 +2,13 @@ require 'spec_helper'
2
2
 
3
3
  describe Mongoid::Tree do
4
4
 
5
- it "should reference many children as inverse of parent" do
5
+ it "should reference many children as inverse of parent with index" do
6
6
  a = Node.associations['children']
7
7
  a.should_not be_nil
8
8
  a.association.should == Mongoid::Associations::ReferencesMany
9
9
  a.options.class_name.should == 'Node'
10
10
  a.options.foreign_key.should == 'parent_id'
11
- a.options.inverse_of.should == :parent
11
+ Node.index_options.should have_key('parent_id')
12
12
  end
13
13
 
14
14
  it "should be referenced in one parent as inverse of children" do
@@ -17,13 +17,15 @@ describe Mongoid::Tree do
17
17
  a.association.should == Mongoid::Associations::ReferencedIn
18
18
  a.options.class_name.should == 'Node'
19
19
  a.options.inverse_of.should == :children
20
+ a.options.index.should be_true
20
21
  end
21
22
 
22
- it "should store parent_ids as Array with [] as default" do
23
+ it "should store parent_ids as Array with [] as default with index" do
23
24
  f = Node.fields['parent_ids']
24
25
  f.should_not be_nil
25
26
  f.options[:type].should == Array
26
27
  f.options[:default].should == []
28
+ Node.index_options.should have_key(:parent_ids)
27
29
  end
28
30
 
29
31
  describe 'when saved' do
@@ -79,6 +81,13 @@ describe Mongoid::Tree do
79
81
  root.save
80
82
  end
81
83
 
84
+ it "should prevent cycles" do
85
+ child = node(:child)
86
+ child.parent = node(:subchild)
87
+ child.should_not be_valid
88
+ child.errors[:parent_id].should_not be_nil
89
+ end
90
+
82
91
  end
83
92
 
84
93
  describe 'utility methods' do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid-tree
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 0
10
- version: 0.2.0
9
+ - 1
10
+ version: 0.2.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Benedikt Deicke
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-07-27 00:00:00 +02:00
18
+ date: 2010-07-29 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -26,13 +26,14 @@ dependencies:
26
26
  requirements:
27
27
  - - ">="
28
28
  - !ruby/object:Gem::Version
29
- hash: -1848230043
29
+ hash: 62196479
30
30
  segments:
31
31
  - 2
32
32
  - 0
33
33
  - 0
34
- - beta9
35
- version: 2.0.0.beta9
34
+ - beta
35
+ - 14
36
+ version: 2.0.0.beta.14
36
37
  type: :runtime
37
38
  version_requirements: *id001
38
39
  - !ruby/object:Gem::Dependency
@@ -107,6 +108,7 @@ files:
107
108
  - README.rdoc
108
109
  - Rakefile
109
110
  - Gemfile
111
+ - Gemfile.lock
110
112
  - .rspec
111
113
  has_rdoc: true
112
114
  homepage: http://github.com/benedikt/mongoid-tree