mongoid_acts_as_tree 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +1 -0
- data/VERSION +1 -1
- data/lib/mongoid_acts_as_tree.rb +175 -175
- data/mongoid_acts_as_tree.gemspec +7 -4
- metadata +25 -6
data/Rakefile
CHANGED
@@ -11,6 +11,7 @@ begin
|
|
11
11
|
gem.homepage = "http://github.com/saks/mongoid_acts_as_tree"
|
12
12
|
gem.authors = ["Jakob Vidmar, Aliaksandr Rahalevich"]
|
13
13
|
gem.add_dependency("mongoid", "<= 2.0.0")
|
14
|
+
gem.add_dependency("bson", ">= 0.20.1")
|
14
15
|
|
15
16
|
gem.add_development_dependency "shoulda", ">=2.10.2"
|
16
17
|
end
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.2
|
data/lib/mongoid_acts_as_tree.rb
CHANGED
@@ -1,171 +1,171 @@
|
|
1
1
|
require "mongoid"
|
2
2
|
|
3
3
|
module Mongoid
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
4
|
+
module Acts
|
5
|
+
module Tree
|
6
|
+
def self.included(model)
|
7
|
+
model.class_eval do
|
8
|
+
extend InitializerMethods
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
module InitializerMethods
|
13
|
+
def acts_as_tree(options = {})
|
14
|
+
options = {
|
15
|
+
:parent_id_field => "parent_id",
|
16
|
+
:path_field => "path",
|
17
|
+
:depth_field => "depth"
|
18
|
+
}.merge(options)
|
19
|
+
|
20
|
+
write_inheritable_attribute :acts_as_tree_options, options
|
21
|
+
class_inheritable_reader :acts_as_tree_options
|
22
|
+
|
23
|
+
include InstanceMethods
|
24
|
+
include Fields
|
25
|
+
extend Fields
|
26
|
+
extend ClassMethods
|
27
|
+
|
28
|
+
field parent_id_field, :type => BSON::ObjectID
|
29
|
+
field path_field, :type => Array, :default => [], :index => true
|
30
|
+
field depth_field, :type => Integer, :default => 0
|
31
|
+
|
32
|
+
after_save :move_children
|
33
|
+
validate :will_save_tree
|
34
|
+
before_destroy :destroy_descendants
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
module ClassMethods
|
39
|
+
def roots
|
40
|
+
self.where(parent_id_field => nil).order_by tree_order
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
module InstanceMethods
|
45
|
+
def [](field_name)
|
46
|
+
self.send field_name
|
47
|
+
end
|
48
|
+
|
49
|
+
def []=(field_name, value)
|
50
|
+
self.send "#{field_name}=", value
|
51
|
+
end
|
52
|
+
|
53
|
+
def ==(other)
|
54
|
+
return true if other.equal?(self)
|
55
|
+
return true if other.instance_of?(self.class) and other._id == self._id
|
56
|
+
false
|
57
|
+
end
|
58
|
+
|
59
|
+
def will_save_tree
|
60
|
+
if @_cyclic
|
61
|
+
errors.add(:base, "Can't be children of a descendant")
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def fix_position
|
66
|
+
if parent.nil?
|
67
|
+
self[parent_id_field] = nil
|
68
|
+
self[path_field] = []
|
69
|
+
self[depth_field] = 0
|
70
|
+
else
|
71
|
+
self[parent_id_field] = parent._id
|
72
|
+
self[path_field] = parent[path_field] + [parent._id]
|
73
|
+
self[depth_field] = parent[depth_field] + 1
|
74
|
+
self.save
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def parent
|
79
|
+
@_parent or (self[parent_id_field].nil? ? nil : self.class.find(self[parent_id_field]))
|
80
|
+
end
|
81
|
+
|
82
|
+
def root?
|
83
|
+
self[parent_id_field].nil?
|
84
|
+
end
|
85
|
+
|
86
|
+
def root
|
87
|
+
self[path_field].first.nil? ? self : self.class.find(self[path_field].first)
|
88
|
+
end
|
89
|
+
|
90
|
+
def ancestors
|
91
|
+
return [] if root?
|
92
|
+
self.class.find(self[path_field])
|
93
|
+
end
|
94
|
+
|
95
|
+
def self_and_ancestors
|
96
|
+
ancestors << self
|
97
|
+
end
|
98
|
+
|
99
|
+
def siblings
|
100
|
+
self.class.where(:_id.ne => self._id, parent_id_field => self[parent_id_field]).order_by tree_order
|
101
|
+
end
|
102
|
+
|
103
|
+
def self_and_siblings
|
104
|
+
self.class.where(parent_id_field => self[parent_id_field]).order_by tree_order
|
105
|
+
end
|
106
|
+
|
107
|
+
def children
|
108
|
+
Children.new self
|
109
|
+
end
|
110
|
+
|
111
|
+
def children=(new_children_list)
|
112
|
+
self.children.clear
|
113
|
+
new_children_list.each do | child |
|
114
|
+
self.children << child
|
115
|
+
end
|
116
|
+
end
|
117
117
|
|
118
118
|
alias replace children=
|
119
119
|
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
120
|
+
def descendants
|
121
|
+
return [] if new_record?
|
122
|
+
self.class.where(path_field => self._id).order_by tree_order
|
123
|
+
end
|
124
124
|
|
125
|
-
|
126
|
-
|
127
|
-
|
125
|
+
def self_and_descendants
|
126
|
+
[self] + self.descendants
|
127
|
+
end
|
128
128
|
|
129
|
-
|
130
|
-
|
131
|
-
|
129
|
+
def is_ancestor_of?(other)
|
130
|
+
other[path_field].include?(self._id)
|
131
|
+
end
|
132
132
|
|
133
|
-
|
134
|
-
|
135
|
-
|
133
|
+
def is_or_is_ancestor_of?(other)
|
134
|
+
(other == self) or is_ancestor_of?(other)
|
135
|
+
end
|
136
136
|
|
137
|
-
|
138
|
-
|
139
|
-
|
137
|
+
def is_descendant_of?(other)
|
138
|
+
self[path_field].include?(other._id)
|
139
|
+
end
|
140
140
|
|
141
|
-
|
142
|
-
|
143
|
-
|
141
|
+
def is_or_is_descendant_of?(other)
|
142
|
+
(other == self) or is_descendant_of?(other)
|
143
|
+
end
|
144
144
|
|
145
|
-
|
146
|
-
|
147
|
-
|
145
|
+
def is_sibling_of?(other)
|
146
|
+
(other != self) and (other[parent_id_field] == self[parent_id_field])
|
147
|
+
end
|
148
148
|
|
149
|
-
|
150
|
-
|
151
|
-
|
149
|
+
def is_or_is_sibling_of?(other)
|
150
|
+
(other == self) or is_sibling_of?(other)
|
151
|
+
end
|
152
152
|
|
153
|
-
|
153
|
+
def move_children
|
154
154
|
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
155
|
+
if @_will_move
|
156
|
+
@_will_move = false
|
157
|
+
self.children.each do | child |
|
158
|
+
child.fix_position
|
159
|
+
child.save
|
160
|
+
end
|
161
|
+
@_will_move = true
|
162
|
+
end
|
163
|
+
end
|
164
164
|
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
165
|
+
def destroy_descendants
|
166
|
+
self.descendants.each &:destroy
|
167
|
+
end
|
168
|
+
end
|
169
169
|
|
170
170
|
#proxy class
|
171
171
|
class Children < Array
|
@@ -181,11 +181,11 @@ module Mongoid
|
|
181
181
|
if object.descendants.include? @parent
|
182
182
|
object.instance_variable_set :@_cyclic, true
|
183
183
|
else
|
184
|
-
|
185
|
-
|
186
|
-
|
184
|
+
object[object.parent_id_field] = @parent._id
|
185
|
+
object[object.path_field] = @parent[@parent.path_field] + [@parent._id]
|
186
|
+
object[object.depth_field] = @parent[@parent.depth_field] + 1
|
187
187
|
object.instance_variable_set :@_will_move, true
|
188
|
-
|
188
|
+
object.save
|
189
189
|
end
|
190
190
|
|
191
191
|
super(object)
|
@@ -225,24 +225,24 @@ module Mongoid
|
|
225
225
|
|
226
226
|
end
|
227
227
|
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
228
|
+
module Fields
|
229
|
+
def parent_id_field
|
230
|
+
acts_as_tree_options[:parent_id_field]
|
231
|
+
end
|
232
|
+
|
233
|
+
def path_field
|
234
|
+
acts_as_tree_options[:path_field]
|
235
|
+
end
|
236
|
+
|
237
|
+
def depth_field
|
238
|
+
acts_as_tree_options[:depth_field]
|
239
|
+
end
|
240
|
+
|
241
|
+
def tree_order
|
242
|
+
acts_as_tree_options[:order] or []
|
243
|
+
end
|
244
|
+
end
|
245
|
+
end
|
246
|
+
end
|
247
247
|
end
|
248
248
|
|
@@ -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.
|
8
|
+
s.version = "0.1.2"
|
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-04
|
12
|
+
s.date = %q{2010-05-04}
|
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 = [
|
@@ -35,7 +35,7 @@ Gem::Specification.new do |s|
|
|
35
35
|
s.homepage = %q{http://github.com/saks/mongoid_acts_as_tree}
|
36
36
|
s.rdoc_options = ["--charset=UTF-8"]
|
37
37
|
s.require_paths = ["lib"]
|
38
|
-
s.rubygems_version = %q{1.3.6}
|
38
|
+
s.rubygems_version = %q{1.3.6.1}
|
39
39
|
s.summary = %q{ActsAsTree plugin for Mongoid}
|
40
40
|
s.test_files = [
|
41
41
|
"test/test_tree.rb",
|
@@ -49,15 +49,18 @@ Gem::Specification.new do |s|
|
|
49
49
|
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
50
50
|
s.specification_version = 3
|
51
51
|
|
52
|
-
if Gem::Version.new(Gem::
|
52
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
53
53
|
s.add_runtime_dependency(%q<mongoid>, ["<= 2.0.0"])
|
54
|
+
s.add_runtime_dependency(%q<bson>, [">= 0.20.1"])
|
54
55
|
s.add_development_dependency(%q<shoulda>, [">= 2.10.2"])
|
55
56
|
else
|
56
57
|
s.add_dependency(%q<mongoid>, ["<= 2.0.0"])
|
58
|
+
s.add_dependency(%q<bson>, [">= 0.20.1"])
|
57
59
|
s.add_dependency(%q<shoulda>, [">= 2.10.2"])
|
58
60
|
end
|
59
61
|
else
|
60
62
|
s.add_dependency(%q<mongoid>, ["<= 2.0.0"])
|
63
|
+
s.add_dependency(%q<bson>, [">= 0.20.1"])
|
61
64
|
s.add_dependency(%q<shoulda>, [">= 2.10.2"])
|
62
65
|
end
|
63
66
|
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 0.1.
|
8
|
+
- 2
|
9
|
+
version: 0.1.2
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Jakob Vidmar, Aliaksandr Rahalevich
|
@@ -14,13 +14,14 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-04
|
17
|
+
date: 2010-05-04 00:00:00 +03:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: mongoid
|
22
22
|
prerelease: false
|
23
23
|
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
24
25
|
requirements:
|
25
26
|
- - <=
|
26
27
|
- !ruby/object:Gem::Version
|
@@ -32,9 +33,25 @@ dependencies:
|
|
32
33
|
type: :runtime
|
33
34
|
version_requirements: *id001
|
34
35
|
- !ruby/object:Gem::Dependency
|
35
|
-
name:
|
36
|
+
name: bson
|
36
37
|
prerelease: false
|
37
38
|
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
segments:
|
44
|
+
- 0
|
45
|
+
- 20
|
46
|
+
- 1
|
47
|
+
version: 0.20.1
|
48
|
+
type: :runtime
|
49
|
+
version_requirements: *id002
|
50
|
+
- !ruby/object:Gem::Dependency
|
51
|
+
name: shoulda
|
52
|
+
prerelease: false
|
53
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
38
55
|
requirements:
|
39
56
|
- - ">="
|
40
57
|
- !ruby/object:Gem::Version
|
@@ -44,7 +61,7 @@ dependencies:
|
|
44
61
|
- 2
|
45
62
|
version: 2.10.2
|
46
63
|
type: :development
|
47
|
-
version_requirements: *
|
64
|
+
version_requirements: *id003
|
48
65
|
description: Port of the old, venerable ActsAsTree with a bit of a twist
|
49
66
|
email: saksmlz@gmail.com
|
50
67
|
executables: []
|
@@ -79,6 +96,7 @@ rdoc_options:
|
|
79
96
|
require_paths:
|
80
97
|
- lib
|
81
98
|
required_ruby_version: !ruby/object:Gem::Requirement
|
99
|
+
none: false
|
82
100
|
requirements:
|
83
101
|
- - ">="
|
84
102
|
- !ruby/object:Gem::Version
|
@@ -86,6 +104,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
86
104
|
- 0
|
87
105
|
version: "0"
|
88
106
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
|
+
none: false
|
89
108
|
requirements:
|
90
109
|
- - ">="
|
91
110
|
- !ruby/object:Gem::Version
|
@@ -95,7 +114,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
95
114
|
requirements: []
|
96
115
|
|
97
116
|
rubyforge_project:
|
98
|
-
rubygems_version: 1.3.6
|
117
|
+
rubygems_version: 1.3.6.1
|
99
118
|
signing_key:
|
100
119
|
specification_version: 3
|
101
120
|
summary: ActsAsTree plugin for Mongoid
|