mongoid_acts_as_tree 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,6 @@
1
+ *0.1.4 (July 5th 2010)*
2
+ *parent_id attribute accessor acts as expected
3
+
1
4
  *0.1.3 (June 15th, 2010)*
2
5
  *patch from joshuabowers (http://github.com/saks/mongoid_acts_as_tree/commit/a98bb62746b71692853a91de7eb35ac55c19bf11)
3
6
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.3
1
+ 0.1.4
@@ -29,6 +29,13 @@ module Mongoid
29
29
  field path_field, :type => Array, :default => [], :index => true
30
30
  field depth_field, :type => Integer, :default => 0
31
31
 
32
+ self.class_eval do
33
+ define_method "#{parent_id_field}=" do | new_parent_id |
34
+ new_parent = self.class.find new_parent_id
35
+ new_parent.children << self
36
+ end
37
+ end
38
+
32
39
  after_save :move_children
33
40
  validate :will_save_tree
34
41
  before_destroy :destroy_descendants
@@ -64,11 +71,11 @@ module Mongoid
64
71
 
65
72
  def fix_position
66
73
  if parent.nil?
67
- self[parent_id_field] = nil
74
+ self.write_attribute parent_id_field, nil
68
75
  self[path_field] = []
69
76
  self[depth_field] = 0
70
77
  else
71
- self[parent_id_field] = parent._id
78
+ self.write_attribute parent_id_field, parent._id
72
79
  self[path_field] = parent[path_field] + [parent._id]
73
80
  self[depth_field] = parent[depth_field] + 1
74
81
  self.save
@@ -181,7 +188,7 @@ module Mongoid
181
188
  if object.descendants.include? @parent
182
189
  object.instance_variable_set :@_cyclic, true
183
190
  else
184
- object[object.parent_id_field] = @parent._id
191
+ object.write_attribute object.parent_id_field, @parent._id
185
192
  object[object.path_field] = @parent[@parent.path_field] + [@parent._id]
186
193
  object[object.depth_field] = @parent[@parent.depth_field] + 1
187
194
  object.instance_variable_set :@_will_move, true
@@ -201,7 +208,7 @@ module Mongoid
201
208
  object_or_id
202
209
  end
203
210
 
204
- object[object.parent_id_field] = nil
211
+ object.write_attribute object.parent_id_field, nil
205
212
  object[object.path_field] = []
206
213
  object[object.depth_field] = 0
207
214
  object.save
@@ -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.3"
8
+ s.version = "0.1.4"
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-06-15}
12
+ s.date = %q{2010-07-05}
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 = [
data/test/test_tree.rb CHANGED
@@ -53,6 +53,37 @@ class TestMongoidActsAsTree < Test::Unit::TestCase
53
53
  assert eql_arrays?(Category.roots, [@root_1, @root_2])
54
54
  end
55
55
 
56
+ should "assign parent_id" do
57
+ child = Category.create :name => 'child'
58
+ parent = Category.create :name => 'parent'
59
+
60
+ child.parent_id = parent.id
61
+ child.save
62
+
63
+ assert_equal parent.children.first.id, child.id
64
+ assert_equal parent.id, child.parent_id
65
+ assert parent.children.include? child
66
+
67
+ assert_equal 1, child.depth
68
+ assert_equal [parent.id], child.path
69
+
70
+ more_deep_child = Category.create :name => 'more deep child'
71
+ more_deep_child.parent_id = child.id
72
+
73
+ assert_equal child.children.first.id, more_deep_child.id
74
+ assert_equal child.id, more_deep_child.parent_id
75
+ assert child.children.include? more_deep_child
76
+
77
+ assert_equal 2, more_deep_child.depth
78
+ assert_equal [parent.id, child.id], more_deep_child.path
79
+
80
+ assert parent.descendants.include? child
81
+ assert parent.descendants.include? more_deep_child
82
+
83
+ assert more_deep_child.ancestors.include? child
84
+ assert more_deep_child.ancestors.include? parent
85
+ end
86
+
56
87
  context "node" do
57
88
  should "have a root" do
58
89
  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
- - 3
9
- version: 0.1.3
8
+ - 4
9
+ version: 0.1.4
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-06-15 00:00:00 +03:00
17
+ date: 2010-07-05 00:00:00 +03:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency