mongoid_tree 0.3.3 → 0.3.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.textile CHANGED
@@ -6,6 +6,12 @@ Initially I thought of an embedded solution, but this will only be possible once
6
6
 
7
7
  However this tree is right now on the top of our priority list, means we will put effort into this and release everything in this public gem, as soon as we implement and test it. It will be fully tested with Cucumber and RSpec.
8
8
 
9
+ h3. Documentation
10
+
11
+ An API Documentation can be found here:
12
+
13
+ "http://rubydoc.info/gems/mongoid_tree/":http://rubydoc.info/gems/mongoid_tree/
14
+
9
15
  h3. Screencasts
10
16
 
11
17
  I've made a screencast demonstrating our tree gem, where I go over the basic methods provided:
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.3
1
+ 0.3.4
data/lib/mongoid_tree.rb CHANGED
@@ -58,31 +58,51 @@ module Mongoid
58
58
  # This stores the position in the children array of the parent object.
59
59
  # Makes it easier to flatten / export / import a tree
60
60
  field :position, :type => Integer
61
- field :depth, :type => Integer
62
61
  end
63
62
 
64
63
  module InstanceMethods
65
-
64
+
65
+
66
66
  def parent
67
- self.parents.last
67
+ parents.last
68
68
  end
69
69
 
70
70
  def depth
71
- self.parents.count
71
+ parent_ids.count
72
+ end
73
+
74
+ def leaf?
75
+ child_ids.empty?
76
+ end
77
+
78
+ def root?
79
+ parent_ids.empty?
80
+ end
81
+
82
+ def root
83
+ base_class.find(parent_ids.first)
84
+ end
85
+
86
+ def ancestors
87
+ base_class.where(:id.in => parent_ids)
88
+ end
89
+
90
+ def ancestors_and_self
91
+ ancestors + [self]
72
92
  end
73
93
 
74
94
  #Comparable
75
95
  def <=> (another_node)
76
- self.position <=> another_node.position
96
+ position <=> another_node.position
77
97
  end
78
98
 
79
99
  # Returns the whole subtree including itself as array
80
100
  def depth_first
81
101
  result = [self]
82
- if self.child_ids.empty?
102
+ if child_ids.empty?
83
103
  return result
84
104
  else
85
- self.children.sort.each do |child|
105
+ children.sort.each do |child|
86
106
  result += child.depth_first
87
107
  end
88
108
  end
@@ -106,40 +126,41 @@ module Mongoid
106
126
  alias :bfs :breadth_first
107
127
 
108
128
  def insert_before( new_child )
109
- new_child.position = self.position
110
- self.parent.children.each do |child|
129
+ new_child.position = position
130
+ parent.children.each do |child|
111
131
  if child.position >= new_child.position
112
132
  child.update_attributes(:position => child.position + 1)
113
133
  end
114
134
  end
115
- self.parent.reload.children << new_child
135
+ parent.children << new_child
116
136
  end
117
137
 
118
138
  def insert_after ( new_child )
119
- new_child.position = self.position + 1
120
- self.parent.children.each do |child|
139
+ new_child.position = position + 1
140
+ parent.children.each do |child|
121
141
  if child.position >= new_child.position
122
142
  child.update_attributes(:position => child.position + 1)
123
143
  end
124
144
  end
125
- self.parent.children << new_child
145
+ parent.children << new_child
126
146
  end
127
147
 
128
148
  def move_to(target_node)
129
149
  # unhinge - I was getting a nil on another implementation, so this is a bit longer but works
130
- child_ids_array = self.parent.child_ids.clone
131
- child_ids_array.delete(self.id)
150
+ child_ids_array = parent.child_ids.clone
151
+ child_ids_array.delete(id)
132
152
  parent.update_attributes(:child_ids => child_ids_array )
133
153
  self.update_attributes(:parent_ids => [])
134
154
  # and append
135
155
  target_node.children << self
136
156
  # recurse through subtree
137
- self.rebuild_paths
157
+ rebuild_paths
138
158
  end
159
+
139
160
 
140
161
  def rebuild_paths
141
- self.update_path
142
- self.children.each do |child|
162
+ update_path
163
+ children.each do |child|
143
164
  child.rebuild_paths
144
165
  end
145
166
  end
@@ -147,6 +168,13 @@ module Mongoid
147
168
  def update_path
148
169
  self.update_attributes(:parent_ids => self.parent.parent_ids + [self.parent.id])
149
170
  end
171
+
172
+ def base_class
173
+ @base_class ||= begin
174
+ parent_classes = self.class.ancestors
175
+ parent_classes[parent_classes.index(Mongoid::Acts::Tree::InstanceMethods) - 1]
176
+ end
177
+ end
150
178
 
151
179
  end
152
180
  end
data/mongoid_tree.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mongoid_tree}
8
- s.version = "0.3.3"
8
+ s.version = "0.3.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Rainer Kuhn"]
12
- s.date = %q{2010-08-26}
12
+ s.date = %q{2010-09-09}
13
13
  s.description = %q{Fully featured tree implementation for Mongoid using materialized paths and relative associations. Featuring Depth and Breadth first search.}
14
14
  s.email = %q{rkuhn@littleweblab.com}
15
15
  s.extra_rdoc_files = [
@@ -29,6 +29,39 @@ describe "MongoidTree" do
29
29
  it "the child should be at position 1" do
30
30
  @parent.children.first.position.should eq(1)
31
31
  end
32
+
33
+ context "on the parent" do
34
+ it "should not be a leaf node" do
35
+ @parent.leaf?.should be(false)
36
+ end
37
+
38
+ it "should have depth 0" do
39
+ @parent.depth.should be(0)
40
+ end
41
+
42
+ it "should be root" do
43
+ @parent.root?.should be(true)
44
+ end
45
+ end
46
+
47
+ context "on the child" do
48
+ it "should be a leaf node" do
49
+ @child.leaf?.should be(true)
50
+ end
51
+
52
+ it "should have depth 1" do
53
+ @child.depth.should be(1)
54
+ end
55
+
56
+ it "should return the parent as root" do
57
+ @child.root.should eq(@parent)
58
+ end
59
+
60
+ it "should not be root" do
61
+ @child.root?.should be(false)
62
+ end
63
+
64
+ end
32
65
 
33
66
  end
34
67
 
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 3
8
- - 3
9
- version: 0.3.3
8
+ - 4
9
+ version: 0.3.4
10
10
  platform: ruby
11
11
  authors:
12
12
  - Rainer Kuhn
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-08-26 00:00:00 +02:00
17
+ date: 2010-09-09 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency