mongoid_tree 0.3.2 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,4 +1,9 @@
1
1
  source :rubygems
2
+
3
+ if File.exist?(file = File.expand_path('../../Gemfile',__FILE__))
4
+ instance_eval(File.read(file))
5
+ end
6
+
2
7
  gem "jeweler"
3
8
  gem "mongoid", "= 2.0.0.beta.16"
4
9
  # Currently used MongoDB version : 1.4.3
@@ -6,6 +6,7 @@ GEM
6
6
  builder (~> 2.1.2)
7
7
  i18n (~> 0.4.1)
8
8
  activesupport (3.0.0.rc)
9
+ awesome_print (0.2.1)
9
10
  bson (1.0.4)
10
11
  builder (2.1.2)
11
12
  cucumber (0.8.5)
@@ -48,15 +49,18 @@ GEM
48
49
  trollop (1.16.2)
49
50
  tzinfo (0.3.22)
50
51
  will_paginate (3.0.pre2)
52
+ wirble (0.1.3)
51
53
  yard (0.5.8)
52
54
 
53
55
  PLATFORMS
54
56
  ruby
55
57
 
56
58
  DEPENDENCIES
59
+ awesome_print
57
60
  cucumber (>= 0.8.5)
58
61
  factory_girl
59
62
  jeweler
60
63
  mongoid (= 2.0.0.beta.16)
61
64
  rspec (>= 2.0.0.beta.19)
65
+ wirble
62
66
  yard
@@ -6,6 +6,17 @@ 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. Screencasts
10
+
11
+ I've made a screencast demonstrating our tree gem, where I go over the basic methods provided:
12
+ * "Demo Youtube":http://www.youtube.com/watch?v=TcARGLkBzHw
13
+ * "Demo screencast.com":http://www.screencast.com/t/N2FlYWFlYz
14
+
15
+ and a "Making Of" screencast, mostly about ruby metaprogramming
16
+ * "Making Of Youtube":http://www.youtube.com/watch?v=PzjWFrfO7gc
17
+ * "Making Of screencast.com":http://www.screencast.com/t/M2QxNDY3M
18
+
19
+
9
20
  h2. Installation
10
21
 
11
22
  Install as Gem
@@ -15,11 +26,11 @@ Install as Gem
15
26
 
16
27
  via Gemfile
17
28
  <code>
18
- gem 'mongoid_tree', '0.3.1'
29
+ gem 'mongoid_tree', '0.3.3'
19
30
  </code>
20
31
 
21
32
  h2. Usage
22
-
33
+
23
34
  mongoid_tree can be included as a module
24
35
 
25
36
  <pre><code>
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.2
1
+ 0.3.3
@@ -6,11 +6,14 @@ module Mongoid
6
6
 
7
7
  included do
8
8
  references_many :children,
9
- :class_name => self.name,
10
- :stored_as => :array,
11
- :inverse_of => :parents,
12
- :dependent => :destroy do
9
+ :class_name => self.name,
10
+ :stored_as => :array,
11
+ :inverse_of => :parents,
12
+ :dependent => :destroy do
13
13
  def <<(*objects)
14
+ # in this version I did not call super, but instead copy the code from the mongoid gem in here
15
+ # I had some persistence fails on saved objects so I'm trying it this way now
16
+ @target = @target.entries
14
17
  objects.flatten.each_with_index do |object, index|
15
18
  reverse_key = reverse_key(object)
16
19
  if object.position == nil
@@ -20,16 +23,37 @@ module Mongoid
20
23
  # Stores the parents path into it's own path array.
21
24
  #raise @parent.inspect
22
25
  object.send(reverse_key).concat(@parent.send(reverse_key))
23
- end
24
- super(objects)
26
+ # object.save unless @parent.new_record?
27
+ # First set the documents id on the parent array of ids.
28
+ @parent.send(@foreign_key) << object.id
29
+ # Then we need to set the parent's id on the documents array of ids
30
+ # to get the inverse side of the association as well. Note, need a
31
+ # clean way to handle this with new documents - we want to set the
32
+ # actual objects as well, but dont want to get in an infinite loop
33
+ # while doing so.
34
+
35
+ if inverse?
36
+ reverse_key = reverse_key(object)
37
+ case inverse_of(object).macro
38
+ when :references_many
39
+ object.send(reverse_key) << @parent.id
40
+ when :referenced_in
41
+ object.send("#{reverse_key}=", @parent.id)
42
+ end
43
+ end
44
+ @target << object
45
+ object.save unless @parent.new_record?
25
46
 
47
+ end
48
+ @parent.save unless @parent.new_record?
49
+ # super(objects)
26
50
  end
27
51
  end
28
52
 
29
53
  references_many :parents,
30
- :class_name => self.name,
31
- :stored_as => :array,
32
- :inverse_of => :children
54
+ :class_name => self.name,
55
+ :stored_as => :array,
56
+ :inverse_of => :children
33
57
 
34
58
  # This stores the position in the children array of the parent object.
35
59
  # Makes it easier to flatten / export / import a tree
@@ -80,7 +104,7 @@ module Mongoid
80
104
  return result
81
105
  end
82
106
  alias :bfs :breadth_first
83
-
107
+
84
108
  def insert_before( new_child )
85
109
  new_child.position = self.position
86
110
  self.parent.children.each do |child|
@@ -95,12 +119,12 @@ module Mongoid
95
119
  new_child.position = self.position + 1
96
120
  self.parent.children.each do |child|
97
121
  if child.position >= new_child.position
98
- child.update_attributes(:position => child.position + 1)
122
+ child.update_attributes(:position => child.position + 1)
99
123
  end
100
124
  end
101
125
  self.parent.children << new_child
102
126
  end
103
-
127
+
104
128
  def move_to(target_node)
105
129
  # unhinge - I was getting a nil on another implementation, so this is a bit longer but works
106
130
  child_ids_array = self.parent.child_ids.clone
@@ -112,18 +136,18 @@ module Mongoid
112
136
  # recurse through subtree
113
137
  self.rebuild_paths
114
138
  end
115
-
139
+
116
140
  def rebuild_paths
117
141
  self.update_path
118
142
  self.children.each do |child|
119
143
  child.rebuild_paths
120
144
  end
121
145
  end
122
-
146
+
123
147
  def update_path
124
148
  self.update_attributes(:parent_ids => self.parent.parent_ids + [self.parent.id])
125
149
  end
126
-
150
+
127
151
  end
128
152
  end
129
153
  end
@@ -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.2"
8
+ s.version = "0.3.3"
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-18}
12
+ s.date = %q{2010-08-26}
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 = [
@@ -54,6 +54,7 @@ Gem::Specification.new do |s|
54
54
  "spec/factories/node.rb",
55
55
  "spec/models/node.rb",
56
56
  "spec/mongoid_tree_spec.rb",
57
+ "spec/on_saved_spec.rb",
57
58
  "spec/spec_helper.rb"
58
59
  ]
59
60
  s.homepage = %q{http://github.com/ticktricktrack/mongoid_tree}
@@ -65,6 +66,7 @@ Gem::Specification.new do |s|
65
66
  "spec/factories/node.rb",
66
67
  "spec/models/node.rb",
67
68
  "spec/mongoid_tree_spec.rb",
69
+ "spec/on_saved_spec.rb",
68
70
  "spec/spec_helper.rb"
69
71
  ]
70
72
 
@@ -0,0 +1,33 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "MongoidTree" do
4
+ context "on a saved node" do
5
+
6
+ before do
7
+ @parent = Factory.create(:node)
8
+ @parent.new_record?.should be(false)
9
+ @child = Factory.create(:node)
10
+ @child.new_record?.should be(false)
11
+ @parent.children << @child
12
+ end
13
+
14
+ it "should be add a saved node to children" do
15
+
16
+ @parent.children.first.should eq(@child)
17
+ end
18
+
19
+ context "when reloading the orm objects" do
20
+ it "should be add a saved node to children" do
21
+ @parent.reload.children.first.should eq(@child.reload)
22
+ end
23
+ end
24
+
25
+ context "when fetching the objects to from the database" do
26
+ it "should be add a saved node to children with reload" do
27
+ Node.find(@parent.id).child_ids.first.should eq(@child.id)
28
+ Node.find(@child.id).parent_ids.first.should eq(@parent.id)
29
+ end
30
+ end
31
+
32
+ end
33
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 3
8
- - 2
9
- version: 0.3.2
8
+ - 3
9
+ version: 0.3.3
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-18 00:00:00 +02:00
17
+ date: 2010-08-26 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -81,6 +81,7 @@ files:
81
81
  - spec/factories/node.rb
82
82
  - spec/models/node.rb
83
83
  - spec/mongoid_tree_spec.rb
84
+ - spec/on_saved_spec.rb
84
85
  - spec/spec_helper.rb
85
86
  has_rdoc: true
86
87
  homepage: http://github.com/ticktricktrack/mongoid_tree
@@ -118,4 +119,5 @@ test_files:
118
119
  - spec/factories/node.rb
119
120
  - spec/models/node.rb
120
121
  - spec/mongoid_tree_spec.rb
122
+ - spec/on_saved_spec.rb
121
123
  - spec/spec_helper.rb