mongo_mapper_tree 0.0.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -29,6 +29,8 @@ This adds class_attributes called parent_id_field, path_field, depth_field, tree
29
29
 
30
30
  Check test_tree.rb and test_search_class.rb for examples.
31
31
 
32
+ For best performance add a index on the field called path_field.
33
+
32
34
  == Note on Patches/Pull Requests
33
35
 
34
36
  * Fork the project.
@@ -10,114 +10,112 @@ module MongoMapper
10
10
  end
11
11
  end
12
12
 
13
- module InstanceMethods
14
- def tree_search_class
15
- self.class.tree_search_class
16
- end
13
+ def tree_search_class
14
+ self.class.tree_search_class
15
+ end
17
16
 
18
- def will_save_tree
19
- if parent && self.descendants.include?(parent)
20
- errors.add(:base, :cyclic)
21
- end
17
+ def will_save_tree
18
+ if parent && self.descendants.include?(parent)
19
+ errors.add(:base, :cyclic)
22
20
  end
21
+ end
23
22
 
24
- def fix_position(opts = {})
25
- if parent.nil?
26
- self[parent_id_field] = nil
27
- self[path_field] = []
28
- self[depth_field] = 0
29
- elsif !!opts[:force] || self.changes.include?(parent_id_field)
30
- @_will_move = true
31
- self[path_field] = parent[path_field] + [parent._id]
32
- self[depth_field] = parent[depth_field] + 1
33
- end
23
+ def fix_position(opts = {})
24
+ if parent.nil?
25
+ self[parent_id_field] = nil
26
+ self[path_field] = []
27
+ self[depth_field] = 0
28
+ elsif !!opts[:force] || self.changes.include?(parent_id_field)
29
+ @_will_move = true
30
+ self[path_field] = parent[path_field] + [parent._id]
31
+ self[depth_field] = parent[depth_field] + 1
34
32
  end
33
+ end
35
34
 
36
- def fix_position!
37
- fix_position(:force => true)
38
- save
39
- end
35
+ def fix_position!
36
+ fix_position(:force => true)
37
+ save
38
+ end
40
39
 
41
- def root?
42
- self[parent_id_field].nil?
43
- end
40
+ def root?
41
+ self[parent_id_field].nil?
42
+ end
44
43
 
45
- def root
46
- self[path_field].first.nil? ? self : tree_search_class.find(self[path_field].first)
47
- end
44
+ def root
45
+ self[path_field].first.nil? ? self : tree_search_class.find(self[path_field].first)
46
+ end
48
47
 
49
- def ancestors
50
- return [] if root?
51
- tree_search_class.find(self[path_field])
52
- end
48
+ def ancestors
49
+ return [] if root?
50
+ tree_search_class.find(self[path_field])
51
+ end
53
52
 
54
- def self_and_ancestors
55
- ancestors << self
56
- end
53
+ def self_and_ancestors
54
+ ancestors << self
55
+ end
57
56
 
58
- def siblings
59
- tree_search_class.where({
60
- :_id => { "$ne" => self._id },
61
- parent_id_field => self[parent_id_field]
62
- }).sort(tree_order).all
63
- end
57
+ def siblings
58
+ tree_search_class.where({
59
+ :_id => { "$ne" => self._id },
60
+ parent_id_field => self[parent_id_field]
61
+ }).sort(tree_order).all
62
+ end
64
63
 
65
- def self_and_siblings
66
- tree_search_class.where({
67
- parent_id_field => self[parent_id_field]
68
- }).sort(tree_order).all
69
- end
64
+ def self_and_siblings
65
+ tree_search_class.where({
66
+ parent_id_field => self[parent_id_field]
67
+ }).sort(tree_order).all
68
+ end
70
69
 
71
- def children
72
- tree_search_class.where(parent_id_field => self._id).sort(tree_order).all
73
- end
70
+ def children
71
+ tree_search_class.where(parent_id_field => self._id).sort(tree_order).all
72
+ end
74
73
 
75
- def descendants
76
- return [] if new_record?
77
- tree_search_class.where(path_field => self._id).sort(tree_order).all
78
- end
74
+ def descendants
75
+ return [] if new_record?
76
+ tree_search_class.where(path_field => self._id).sort(tree_order).all
77
+ end
79
78
 
80
- def self_and_descendants
81
- [self] + self.descendants
82
- end
79
+ def self_and_descendants
80
+ [self] + self.descendants
81
+ end
83
82
 
84
- def is_ancestor_of?(other)
85
- other[path_field].include?(self._id)
86
- end
83
+ def is_ancestor_of?(other)
84
+ other[path_field].include?(self._id)
85
+ end
87
86
 
88
- def is_or_is_ancestor_of?(other)
89
- (other == self) or is_ancestor_of?(other)
90
- end
87
+ def is_or_is_ancestor_of?(other)
88
+ (other == self) or is_ancestor_of?(other)
89
+ end
91
90
 
92
- def is_descendant_of?(other)
93
- self[path_field].include?(other._id)
94
- end
91
+ def is_descendant_of?(other)
92
+ self[path_field].include?(other._id)
93
+ end
95
94
 
96
- def is_or_is_descendant_of?(other)
97
- (other == self) or is_descendant_of?(other)
98
- end
95
+ def is_or_is_descendant_of?(other)
96
+ (other == self) or is_descendant_of?(other)
97
+ end
99
98
 
100
- def is_sibling_of?(other)
101
- (other != self) and (other[parent_id_field] == self[parent_id_field])
102
- end
99
+ def is_sibling_of?(other)
100
+ (other != self) and (other[parent_id_field] == self[parent_id_field])
101
+ end
103
102
 
104
- def is_or_is_sibling_of?(other)
105
- (other == self) or is_sibling_of?(other)
106
- end
103
+ def is_or_is_sibling_of?(other)
104
+ (other == self) or is_sibling_of?(other)
105
+ end
107
106
 
108
- def move_children
109
- if @_will_move
110
- @_will_move = false
111
- self.children.each do |child|
112
- child.fix_position!
113
- end
114
- @_will_move = true
107
+ def move_children
108
+ if @_will_move
109
+ @_will_move = false
110
+ self.children.each do |child|
111
+ child.fix_position!
115
112
  end
113
+ @_will_move = true
116
114
  end
115
+ end
117
116
 
118
- def destroy_descendants
119
- tree_search_class.destroy(self.descendants.map(&:_id))
120
- end
117
+ def destroy_descendants
118
+ tree_search_class.destroy(self.descendants.map(&:_id))
121
119
  end
122
120
 
123
121
  included do
@@ -145,7 +143,7 @@ module MongoMapper
145
143
  class_attribute :tree_order
146
144
 
147
145
  key parent_id_field, ObjectId
148
- key path_field, Array, :default => [], :index => true
146
+ key path_field, Array, :default => []
149
147
  key depth_field, Integer, :default => 0
150
148
 
151
149
  belongs_to :parent, :class => tree_search_class
@@ -157,4 +155,4 @@ module MongoMapper
157
155
  end
158
156
  end
159
157
  end
160
- end
158
+ end
data/lib/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  # encoding: UTF-8
2
2
  module MongoMapperTree
3
- Version = '0.0.1'
4
- end
3
+ Version = '1.0.0'
4
+ end
metadata CHANGED
@@ -1,50 +1,46 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: mongo_mapper_tree
3
- version: !ruby/object:Gem::Version
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
4
5
  prerelease:
5
- version: 0.0.1
6
6
  platform: ruby
7
- authors:
8
- - "Joel Junstr\xC3\xB6m"
7
+ authors:
8
+ - Joel Junström
9
+ - Arvid Andersson
9
10
  autorequire:
10
11
  bindir: bin
11
12
  cert_chain: []
12
-
13
- date: 2011-08-05 00:00:00 +02:00
14
- default_executable:
15
- dependencies:
16
- - !ruby/object:Gem::Dependency
13
+ date: 2012-02-27 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
17
16
  name: mongo_mapper
18
- requirement: &id001 !ruby/object:Gem::Requirement
17
+ requirement: &70110048249320 !ruby/object:Gem::Requirement
19
18
  none: false
20
- requirements:
19
+ requirements:
21
20
  - - ~>
22
- - !ruby/object:Gem::Version
23
- version: 0.9.1
21
+ - !ruby/object:Gem::Version
22
+ version: 0.11.0
24
23
  type: :runtime
25
24
  prerelease: false
26
- version_requirements: *id001
27
- - !ruby/object:Gem::Dependency
25
+ version_requirements: *70110048249320
26
+ - !ruby/object:Gem::Dependency
28
27
  name: shoulda
29
- requirement: &id002 !ruby/object:Gem::Requirement
28
+ requirement: &70110048247860 !ruby/object:Gem::Requirement
30
29
  none: false
31
- requirements:
30
+ requirements:
32
31
  - - ~>
33
- - !ruby/object:Gem::Version
34
- version: "2.10"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.10'
35
34
  type: :development
36
35
  prerelease: false
37
- version_requirements: *id002
36
+ version_requirements: *70110048247860
38
37
  description: An Acts As Tree like implementation for MongoMapper based on mongo_mapper_acts_as_tree
39
- email:
40
- - joel.junstrom@oktavilla.se
38
+ email:
39
+ - bender@oktavilla.se
41
40
  executables: []
42
-
43
41
  extensions: []
44
-
45
42
  extra_rdoc_files: []
46
-
47
- files:
43
+ files:
48
44
  - lib/locale/en.yml
49
45
  - lib/mongo_mapper/plugins/tree.rb
50
46
  - lib/mongo_mapper_tree.rb
@@ -58,41 +54,37 @@ files:
58
54
  - test/test_tree.rb
59
55
  - LICENSE
60
56
  - README.rdoc
61
- has_rdoc: true
62
57
  homepage: http://github.com/Oktavilla/mongo_mapper_tree
63
58
  licenses: []
64
-
65
59
  post_install_message:
66
60
  rdoc_options: []
67
-
68
- require_paths:
61
+ require_paths:
69
62
  - lib
70
- required_ruby_version: !ruby/object:Gem::Requirement
63
+ required_ruby_version: !ruby/object:Gem::Requirement
71
64
  none: false
72
- requirements:
73
- - - ">="
74
- - !ruby/object:Gem::Version
75
- hash: -4220793794743588163
76
- segments:
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ segments:
77
70
  - 0
78
- version: "0"
79
- required_rubygems_version: !ruby/object:Gem::Requirement
71
+ hash: -1182322477251881257
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
73
  none: false
81
- requirements:
82
- - - ">="
83
- - !ruby/object:Gem::Version
84
- hash: -4220793794743588163
85
- segments:
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ segments:
86
79
  - 0
87
- version: "0"
80
+ hash: -1182322477251881257
88
81
  requirements: []
89
-
90
82
  rubyforge_project:
91
- rubygems_version: 1.6.2
83
+ rubygems_version: 1.8.15
92
84
  signing_key:
93
85
  specification_version: 3
94
86
  summary: An Acts As Tree like implementation for MongoMapper
95
- test_files:
87
+ test_files:
96
88
  - test/helper.rb
97
89
  - test/models/category.rb
98
90
  - test/models/ordered_category.rb