mongoid_acts_as_tree 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -15,7 +15,7 @@ begin
15
15
 
16
16
  gem.add_development_dependency "shoulda", ">=2.10.2"
17
17
  end
18
- Jeweler::GemcutterTasks.new
18
+ # Jeweler::GemcutterTasks.new
19
19
  rescue LoadError
20
20
  puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
21
21
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.5
1
+ 0.1.6
@@ -14,7 +14,8 @@ module Mongoid
14
14
  options = {
15
15
  :parent_id_field => "parent_id",
16
16
  :path_field => "path",
17
- :depth_field => "depth"
17
+ :depth_field => "depth",
18
+ :class => self
18
19
  }.merge(options)
19
20
 
20
21
  write_inheritable_attribute :acts_as_tree_options, options
@@ -25,14 +26,20 @@ module Mongoid
25
26
  extend Fields
26
27
  extend ClassMethods
27
28
 
28
- field parent_id_field, :type => BSON::ObjectID
29
+ field parent_id_field, :type => BSON::ObjectId
29
30
  field path_field, :type => Array, :default => [], :index => true
30
31
  field depth_field, :type => Integer, :default => 0
31
32
 
32
33
  self.class_eval do
33
34
  define_method "#{parent_id_field}=" do | new_parent_id |
34
- new_parent = self.class.find new_parent_id
35
- new_parent.children.push self, false
35
+ if new_parent_id.present?
36
+ new_parent = acts_as_tree_options[:class].find new_parent_id
37
+ new_parent.children.push self, false
38
+ else
39
+ self.write_attribute parent_id_field, nil
40
+ self[path_field] = []
41
+ self[depth_field] = 0
42
+ end
36
43
  end
37
44
  end
38
45
 
@@ -59,7 +66,7 @@ module Mongoid
59
66
 
60
67
  def ==(other)
61
68
  return true if other.equal?(self)
62
- return true if other.instance_of?(self.class) and other._id == self._id
69
+ return true if other.kind_of?(acts_as_tree_options[:class]) and other._id == self._id
63
70
  false
64
71
  end
65
72
 
@@ -83,7 +90,7 @@ module Mongoid
83
90
  end
84
91
 
85
92
  def parent
86
- @_parent or (self[parent_id_field].nil? ? nil : self.class.find(self[parent_id_field]))
93
+ @_parent or (self[parent_id_field].nil? ? nil : acts_as_tree_options[:class].find(self[parent_id_field]))
87
94
  end
88
95
 
89
96
  def root?
@@ -91,12 +98,12 @@ module Mongoid
91
98
  end
92
99
 
93
100
  def root
94
- self[path_field].first.nil? ? self : self.class.find(self[path_field].first)
101
+ self[path_field].first.nil? ? self : acts_as_tree_options[:class].find(self[path_field].first)
95
102
  end
96
103
 
97
104
  def ancestors
98
105
  return [] if root?
99
- self.class.where(:_id.in => self[path_field]).order_by(depth_field)
106
+ acts_as_tree_options[:class].where(:_id.in => self[path_field]).order_by(depth_field)
100
107
  end
101
108
 
102
109
  def self_and_ancestors
@@ -104,11 +111,11 @@ module Mongoid
104
111
  end
105
112
 
106
113
  def siblings
107
- self.class.where(:_id.ne => self._id, parent_id_field => self[parent_id_field]).order_by tree_order
114
+ acts_as_tree_options[:class].where(:_id.ne => self._id, parent_id_field => self[parent_id_field]).order_by tree_order
108
115
  end
109
116
 
110
117
  def self_and_siblings
111
- self.class.where(parent_id_field => self[parent_id_field]).order_by tree_order
118
+ acts_as_tree_options[:class].where(parent_id_field => self[parent_id_field]).order_by tree_order
112
119
  end
113
120
 
114
121
  def children
@@ -125,8 +132,12 @@ module Mongoid
125
132
  alias replace children=
126
133
 
127
134
  def descendants
128
- return [] if new_record?
129
- self.class.where(path_field => self._id).order_by tree_order
135
+ # workorund for mongoid unexpected behavior
136
+ _new_record_var = self.instance_variable_get(:@new_record)
137
+ _new_record = _new_record_var != false
138
+
139
+ return [] if _new_record
140
+ acts_as_tree_options[:class].all_in(path_field => [self._id]).order_by tree_order
130
141
  end
131
142
 
132
143
  def self_and_descendants
@@ -198,13 +209,21 @@ module Mongoid
198
209
  super(object)
199
210
  end
200
211
 
212
+ def build(attributes)
213
+ child = @parent.class.new(attributes)
214
+ self.push child
215
+ child
216
+ end
217
+
218
+ alias create build
219
+
201
220
  alias push <<
202
221
 
203
222
  #Deletes object only from children list.
204
223
  #To delete object use <tt>object.destroy</tt>.
205
224
  def delete(object_or_id)
206
225
  object = case object_or_id
207
- when String
226
+ when String, BSON::ObjectId
208
227
  @parent.class.find object_or_id
209
228
  else
210
229
  object_or_id
@@ -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.5"
8
+ s.version = "0.1.6"
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-07-05}
12
+ s.date = %q{2010-11-02}
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 = [
@@ -29,6 +29,7 @@ Gem::Specification.new do |s|
29
29
  "test/helper.rb",
30
30
  "test/models/category.rb",
31
31
  "test/models/ordered_category.rb",
32
+ "test/models/sub_category.rb",
32
33
  "test/test_order.rb",
33
34
  "test/test_tree.rb"
34
35
  ]
@@ -38,11 +39,12 @@ Gem::Specification.new do |s|
38
39
  s.rubygems_version = %q{1.3.7}
39
40
  s.summary = %q{ActsAsTree plugin for Mongoid}
40
41
  s.test_files = [
41
- "test/test_order.rb",
42
+ "test/models/category.rb",
42
43
  "test/models/ordered_category.rb",
43
- "test/models/category.rb",
44
+ "test/models/sub_category.rb",
44
45
  "test/helper.rb",
45
- "test/test_tree.rb"
46
+ "test/test_tree.rb",
47
+ "test/test_order.rb"
46
48
  ]
47
49
 
48
50
  if s.respond_to? :specification_version then
@@ -0,0 +1,3 @@
1
+ class SubCategory < Category
2
+
3
+ end
data/test/test_tree.rb CHANGED
@@ -9,9 +9,9 @@ class TestMongoidActsAsTree < Test::Unit::TestCase
9
9
  @root_1 = Category.create(:name => "Root 1")
10
10
  @child_1 = Category.create(:name => "Child 1")
11
11
  @child_2 = Category.create(:name => "Child 2")
12
- @child_2_1 = Category.create(:name => "Child 2.1")
12
+ @child_2_1 = SubCategory.create(:name => "Child 2.1")
13
13
 
14
- @child_3 = Category.create(:name => "Child 3")
14
+ @child_3 = SubCategory.create(:name => "Child 3")
15
15
  @root_2 = Category.create(:name => "Root 2")
16
16
 
17
17
  @root_1.children << @child_1
@@ -21,6 +21,11 @@ class TestMongoidActsAsTree < Test::Unit::TestCase
21
21
  @child_2.children << @child_2_1
22
22
  end
23
23
 
24
+ should "add child via create or build" do
25
+ @root_1.children.build :name => "Child 2.2"
26
+ assert Category.where(:name => "Child 2.2").first.parent == @root_1
27
+ end
28
+
24
29
  should "add child via <<" do
25
30
  child = Category.create(:name => "Child 2.2")
26
31
  @root_1.children << child
@@ -90,6 +95,22 @@ class TestMongoidActsAsTree < Test::Unit::TestCase
90
95
  assert more_deep_child.ancestors.include? parent
91
96
  end
92
97
 
98
+ should "assign blank parent_id" do
99
+ @child_1.parent_id = ''
100
+ @child_1.save
101
+
102
+ assert_nil @child_1.reload.parent_id
103
+ assert_equal 0, @child_1.depth
104
+ assert_equal [], @child_1.path
105
+
106
+ @child_1.parent_id = nil
107
+ @child_1.save
108
+
109
+ assert_nil @child_1.reload.parent_id
110
+ assert_equal 0, @child_1.depth
111
+ assert_equal [], @child_1.path
112
+ end
113
+
93
114
  context "node" do
94
115
  should "have a root" do
95
116
  assert_equal @root_1.root, @root_1
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 5
9
- version: 0.1.5
8
+ - 6
9
+ version: 0.1.6
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-07-05 00:00:00 +03:00
17
+ date: 2010-11-02 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -84,6 +84,7 @@ files:
84
84
  - test/helper.rb
85
85
  - test/models/category.rb
86
86
  - test/models/ordered_category.rb
87
+ - test/models/sub_category.rb
87
88
  - test/test_order.rb
88
89
  - test/test_tree.rb
89
90
  has_rdoc: true
@@ -119,8 +120,9 @@ signing_key:
119
120
  specification_version: 3
120
121
  summary: ActsAsTree plugin for Mongoid
121
122
  test_files:
122
- - test/test_order.rb
123
- - test/models/ordered_category.rb
124
123
  - test/models/category.rb
124
+ - test/models/ordered_category.rb
125
+ - test/models/sub_category.rb
125
126
  - test/helper.rb
126
127
  - test/test_tree.rb
128
+ - test/test_order.rb