mongo_mapper_acts_as_tree 0.2 → 0.3

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.
@@ -12,8 +12,13 @@ module MongoMapper
12
12
  configuration = { :foreign_key => :parent_id, :order => nil, :counter_cache => nil }
13
13
  configuration.update(options) if options.is_a?(Hash)
14
14
 
15
+ # automatically create needed keys if they don't already exist
16
+ key configuration[:foreign_key], ObjectId unless keys.key?(configuration[:foreign_key])
17
+ key configuration[:foreign_key].to_s.pluralize.to_sym, Array unless keys.key?(configuration[:foreign_key].to_s.pluralize.to_sym)
18
+
15
19
  belongs_to :parent, :class_name => name, :foreign_key => configuration[:foreign_key], :counter_cache => configuration[:counter_cache]
16
20
  many :children, :class_name => name, :foreign_key => configuration[:foreign_key], :order => configuration[:order], :dependent => :destroy
21
+ before_save :set_parents
17
22
 
18
23
  class_eval <<-EOV
19
24
  def self.roots
@@ -23,6 +28,23 @@ module MongoMapper
23
28
  def self.root
24
29
  where("#{configuration[:foreign_key]}".to_sym => nil).sort("#{configuration[:order]}").first
25
30
  end
31
+
32
+ def set_parents
33
+ self.#{configuration[:foreign_key].to_s.pluralize} = parent.#{configuration[:foreign_key].to_s.pluralize}.dup << #{configuration[:foreign_key]} if parent?
34
+ end
35
+
36
+ def ancestors
37
+ self.class.where(:id => { '$in' => self.#{configuration[:foreign_key].to_s.pluralize} }).all.reverse || []
38
+ end
39
+
40
+ def root
41
+ self.class.find(self.#{configuration[:foreign_key].to_s.pluralize}.first) || self
42
+ end
43
+
44
+ def descendants
45
+ self.class.where('#{configuration[:foreign_key].to_s.pluralize}' => self.id).all
46
+ end
47
+
26
48
  EOV
27
49
  end
28
50
  end
@@ -31,32 +53,11 @@ module MongoMapper
31
53
 
32
54
  # --------------------------------------------------------------------------------
33
55
  module InstanceMethods
34
- # Returns list of ancestors, starting from parent until root.
35
- #
36
- # subchild1.ancestors # => [child1, root]
37
- def ancestors
38
- node, nodes = self, []
39
- nodes << node = node.parent while node.parent?
40
- nodes
41
- end
42
-
43
- # Returns the root node of the tree.
44
- def root
45
- node = self
46
- node = node.parent while node.parent?
47
- node
48
- end
49
56
 
50
- # Returns all siblings of the current node.
51
- #
52
- # subchild1.siblings # => [subchild2]
53
57
  def siblings
54
58
  self_and_siblings - [self]
55
59
  end
56
60
 
57
- # Returns all siblings and a reference to the current node.
58
- #
59
- # subchild1.self_and_siblings # => [subchild1, subchild2]
60
61
  def self_and_siblings
61
62
  parent? ? parent.children : self.class.roots
62
63
  end
@@ -2,7 +2,7 @@
2
2
  module MongoMapper
3
3
  module Plugins
4
4
  module ActsAsTree
5
- Version = '0.2'
5
+ Version = '0.3'
6
6
  end
7
7
  end
8
8
  end
@@ -2,41 +2,23 @@ require 'test_helper'
2
2
 
3
3
 
4
4
 
5
- # SETUP TEST
6
-
7
- # class ActiveSupport::TestCase
8
- # def assert_queries(num = 1)
9
- # $query_count = 0
10
- # yield
11
- # ensure
12
- # assert_equal num, $query_count, "#{$query_count} instead of #{num} queries were executed."
13
- # end
14
- #
15
- # def assert_no_queries(&block)
16
- # assert_queries(0, &block)
17
- # end
18
- # end
19
-
20
-
21
-
22
5
  # SETUP CLASSES
23
6
 
24
7
  class Mixin
25
8
  include MongoMapper::Document
26
9
  plugin MongoMapper::Plugins::ActsAsTree
27
- key :parent_id, ObjectId
28
10
  end
29
11
 
30
12
  class TreeMixin < Mixin
31
- acts_as_tree :foreign_key => :parent_id, :order => :id
13
+ acts_as_tree
32
14
  end
33
15
 
34
16
  class TreeMixinWithoutOrder < Mixin
35
- acts_as_tree :foreign_key => :parent_id
17
+ acts_as_tree
36
18
  end
37
19
 
38
20
  class RecursivelyCascadedTreeMixin < Mixin
39
- acts_as_tree :foreign_key => :parent_id
21
+ acts_as_tree
40
22
  has_one :first_child, :class_name => 'RecursivelyCascadedTreeMixin', :foreign_key => :parent_id
41
23
  end
42
24
 
@@ -100,6 +82,14 @@ class TreeTest < ActiveSupport::TestCase
100
82
  assert_equal [], @root3.ancestors
101
83
  end
102
84
 
85
+ def test_descendants
86
+ assert_equal [@root_child1, @child1_child, @root_child2], @root1.descendants
87
+ assert_equal [@child1_child], @root_child1.descendants
88
+ assert_equal [], @root_child2.descendants
89
+ assert_equal [], @root2.descendants
90
+ assert_equal [], @root3.descendants
91
+ end
92
+
103
93
  def test_root
104
94
  assert_equal @root1, TreeMixin.root
105
95
  assert_equal @root1, @root1.root
data/test/test_helper.rb CHANGED
@@ -4,6 +4,7 @@ $:.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
4
4
  require 'mongo_mapper'
5
5
  require 'mongo_mapper/plugins/acts_as_tree'
6
6
  require 'test/unit'
7
+ require 'ruby-debug'
7
8
 
8
9
 
9
10
 
metadata CHANGED
@@ -1,12 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongo_mapper_acts_as_tree
3
3
  version: !ruby/object:Gem::Version
4
- hash: 15
4
+ hash: 13
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 2
9
- version: "0.2"
8
+ - 3
9
+ version: "0.3"
10
10
  platform: ruby
11
11
  authors:
12
12
  - Tomas Celizna
@@ -14,12 +14,12 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-02-18 00:00:00 +01:00
17
+ date: 2011-02-24 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
+ type: :runtime
21
22
  name: mongo_mapper
22
- prerelease: false
23
23
  requirement: &id001 !ruby/object:Gem::Requirement
24
24
  none: false
25
25
  requirements:
@@ -29,11 +29,11 @@ dependencies:
29
29
  segments:
30
30
  - 0
31
31
  version: "0"
32
- type: :runtime
32
+ prerelease: false
33
33
  version_requirements: *id001
34
34
  - !ruby/object:Gem::Dependency
35
+ type: :development
35
36
  name: rake
36
- prerelease: false
37
37
  requirement: &id002 !ruby/object:Gem::Requirement
38
38
  none: false
39
39
  requirements:
@@ -43,7 +43,7 @@ dependencies:
43
43
  segments:
44
44
  - 0
45
45
  version: "0"
46
- type: :development
46
+ prerelease: false
47
47
  version_requirements: *id002
48
48
  description:
49
49
  email: