acts_as_tree 1.3.0 → 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +27 -4
- data/gemfiles/rails-4.0.gemfile +1 -1
- data/lib/acts_as_tree.rb +48 -0
- data/lib/acts_as_tree/version.rb +1 -1
- data/test/acts_as_tree_test.rb +42 -0
- metadata +13 -21
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6345f6c0aff0309193dc564612c75fc1ea6b267a
|
4
|
+
data.tar.gz: 5d09b92e6dd13c1bf80be0f0b69d1cfb2c45328d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9c048a9ed2b95836a0915e1e95619975ef8bc4a42f7937bdfb258977f3ec4bf9420fc38eb9c5fa21abe7fd359dbb6861ad61d0f003339813c1114610ef5c782a
|
7
|
+
data.tar.gz: 73fe82d6e696819c08633c5850ca1f1a4b588eb3033777df5bbd8331b2e4fc1de2dc636f5da1c5c9fb65fa9a9b8da4fc8740b69877bccd2c67386a199c2e8eb2
|
data/README.md
CHANGED
@@ -2,8 +2,7 @@
|
|
2
2
|
[travis]: (http://travis-ci.org/amerine/acts_as_tree)
|
3
3
|
|
4
4
|
|
5
|
-
ActsAsTree extends ActiveRecord to add simple support for organizing items into
|
6
|
-
parent–children relationships.
|
5
|
+
ActsAsTree extends ActiveRecord to add simple support for organizing items into parent–children relationships.
|
7
6
|
|
8
7
|
## Example
|
9
8
|
|
@@ -24,6 +23,27 @@ root.children # => [child1]
|
|
24
23
|
root.children.first.children.first # => subchild1
|
25
24
|
```
|
26
25
|
|
26
|
+
We also have a convenient `Presentation` module you can mixin if you want a little visual representation of the tree strucuture. Example:
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
class Category < ActiveRecord::Base
|
30
|
+
include ActsAsTree
|
31
|
+
extend ActsAsTree::Presentation
|
32
|
+
|
33
|
+
acts_as_tree order: "name"
|
34
|
+
end
|
35
|
+
|
36
|
+
> Category.tree_view(:name)
|
37
|
+
root
|
38
|
+
|_ child1
|
39
|
+
| |_ subchild1
|
40
|
+
| |_ subchild2
|
41
|
+
|_ child2
|
42
|
+
|_ subchild3
|
43
|
+
|_ subchild4
|
44
|
+
=> nil
|
45
|
+
```
|
46
|
+
|
27
47
|
## Compatibility
|
28
48
|
|
29
49
|
We no longer support Ruby 1.8 or versions of Rails/ActiveRecord older than 3.0. If you're using a version of ActiveRecord older than 3.0 please use 0.1.1.
|
@@ -31,9 +51,12 @@ We no longer support Ruby 1.8 or versions of Rails/ActiveRecord older than 3.0.
|
|
31
51
|
Moving forward we will do our best to support the latest versions of ActiveRecord and Ruby.
|
32
52
|
|
33
53
|
## Change Log
|
54
|
+
* 1.4.0 - June 25th, 2013
|
55
|
+
* `Presentation#tree_view` -- rainchen
|
56
|
+
* `root?` && `leaf?` methods. -- xuanxu
|
34
57
|
* 1.3.0 - March 29th, 2013
|
35
|
-
|
36
|
-
|
58
|
+
* Rails 4.0 Support! -- mischa78
|
59
|
+
* Readme Fixes -- mischa78 & seanhussey
|
37
60
|
* 1.2.0 - October 29th, 2012
|
38
61
|
* Adding new `self_with_ancestors` accessor -- fbuenemann
|
39
62
|
* `roots` is now a scope.
|
data/gemfiles/rails-4.0.gemfile
CHANGED
data/lib/acts_as_tree.rb
CHANGED
@@ -105,6 +105,38 @@ module ActsAsTree
|
|
105
105
|
end
|
106
106
|
EOV
|
107
107
|
end
|
108
|
+
|
109
|
+
end
|
110
|
+
|
111
|
+
module Presentation
|
112
|
+
# show records in a tree view
|
113
|
+
# Example:
|
114
|
+
# root
|
115
|
+
# |_ child1
|
116
|
+
# | |_ subchild1
|
117
|
+
# | |_ subchild2
|
118
|
+
# |_ child2
|
119
|
+
# |_ subchild3
|
120
|
+
# |_ subchild4
|
121
|
+
#
|
122
|
+
def tree_view(label_method = :to_s, node = nil, level = -1)
|
123
|
+
if node.nil?
|
124
|
+
puts "root"
|
125
|
+
nodes = roots
|
126
|
+
else
|
127
|
+
label = "|_ #{node.send(label_method)}"
|
128
|
+
if level == 0
|
129
|
+
puts " #{label}"
|
130
|
+
else
|
131
|
+
puts " |#{" "*level}#{label}"
|
132
|
+
end
|
133
|
+
nodes = node.children
|
134
|
+
end
|
135
|
+
nodes.each do |child|
|
136
|
+
tree_view(label_method, child, level+1)
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
108
140
|
end
|
109
141
|
|
110
142
|
module InstanceMethods
|
@@ -152,6 +184,22 @@ module ActsAsTree
|
|
152
184
|
[self] + self.ancestors
|
153
185
|
end
|
154
186
|
|
187
|
+
# Returns true if node has no parent, false otherwise
|
188
|
+
#
|
189
|
+
# subchild1.root? # => false
|
190
|
+
# root.root? # => true
|
191
|
+
def root?
|
192
|
+
parent.nil?
|
193
|
+
end
|
194
|
+
|
195
|
+
# Returns true if node has no children, false otherwise
|
196
|
+
#
|
197
|
+
# subchild1.leaf? # => true
|
198
|
+
# child1.leaf? # => false
|
199
|
+
def leaf?
|
200
|
+
children.count == 0
|
201
|
+
end
|
202
|
+
|
155
203
|
private
|
156
204
|
|
157
205
|
def update_parents_counter_cache
|
data/lib/acts_as_tree/version.rb
CHANGED
data/test/acts_as_tree_test.rb
CHANGED
@@ -179,6 +179,48 @@ class TreeTest < Test::Unit::TestCase
|
|
179
179
|
assert_nil root4_child.reload.parent_id
|
180
180
|
end
|
181
181
|
|
182
|
+
def test_is_root
|
183
|
+
assert_equal true, @root1.root?
|
184
|
+
assert_equal true, @root2.root?
|
185
|
+
assert_equal true, @root3.root?
|
186
|
+
|
187
|
+
assert_equal false, @root_child1.root?
|
188
|
+
assert_equal false, @child1_child.root?
|
189
|
+
assert_equal false, @child1_child_child.root?
|
190
|
+
assert_equal false, @root_child2.root?
|
191
|
+
end
|
192
|
+
|
193
|
+
def test_is_leaf
|
194
|
+
assert_equal true, @root2.leaf?
|
195
|
+
assert_equal true, @root3.leaf?
|
196
|
+
assert_equal true, @child1_child_child.leaf?
|
197
|
+
assert_equal true, @root_child2.leaf?
|
198
|
+
|
199
|
+
assert_equal false, @root1.leaf?
|
200
|
+
assert_equal false, @root_child1.leaf?
|
201
|
+
assert_equal false, @child1_child.leaf?
|
202
|
+
end
|
203
|
+
|
204
|
+
def test_tree_view
|
205
|
+
assert_equal false, Mixin.respond_to?(:tree_view)
|
206
|
+
Mixin.extend ActsAsTree::Presentation
|
207
|
+
assert_equal true, TreeMixin.respond_to?(:tree_view)
|
208
|
+
|
209
|
+
tree_view_outputs = <<-END.gsub(/^ {6}/, '')
|
210
|
+
root
|
211
|
+
|_ 1
|
212
|
+
| |_ 2
|
213
|
+
| |_ 3
|
214
|
+
| |_ 4
|
215
|
+
| |_ 5
|
216
|
+
|_ 6
|
217
|
+
|_ 7
|
218
|
+
END
|
219
|
+
$stdout.reopen # reinitializes $stdout
|
220
|
+
TreeMixin.tree_view(:id)
|
221
|
+
assert_equal tree_view_outputs, $stdout.string
|
222
|
+
end
|
223
|
+
|
182
224
|
end
|
183
225
|
|
184
226
|
class TreeTestWithEagerLoading < Test::Unit::TestCase
|
metadata
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acts_as_tree
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
5
|
-
prerelease:
|
4
|
+
version: 1.4.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Erik Dahlstrand
|
@@ -12,54 +11,48 @@ authors:
|
|
12
11
|
autorequire:
|
13
12
|
bindir: bin
|
14
13
|
cert_chain: []
|
15
|
-
date: 2013-
|
14
|
+
date: 2013-06-25 00:00:00.000000000 Z
|
16
15
|
dependencies:
|
17
16
|
- !ruby/object:Gem::Dependency
|
18
17
|
name: activerecord
|
19
18
|
requirement: !ruby/object:Gem::Requirement
|
20
|
-
none: false
|
21
19
|
requirements:
|
22
|
-
- -
|
20
|
+
- - '>='
|
23
21
|
- !ruby/object:Gem::Version
|
24
22
|
version: 3.0.0
|
25
23
|
type: :runtime
|
26
24
|
prerelease: false
|
27
25
|
version_requirements: !ruby/object:Gem::Requirement
|
28
|
-
none: false
|
29
26
|
requirements:
|
30
|
-
- -
|
27
|
+
- - '>='
|
31
28
|
- !ruby/object:Gem::Version
|
32
29
|
version: 3.0.0
|
33
30
|
- !ruby/object:Gem::Dependency
|
34
31
|
name: sqlite3
|
35
32
|
requirement: !ruby/object:Gem::Requirement
|
36
|
-
none: false
|
37
33
|
requirements:
|
38
|
-
- -
|
34
|
+
- - '>='
|
39
35
|
- !ruby/object:Gem::Version
|
40
36
|
version: '0'
|
41
37
|
type: :development
|
42
38
|
prerelease: false
|
43
39
|
version_requirements: !ruby/object:Gem::Requirement
|
44
|
-
none: false
|
45
40
|
requirements:
|
46
|
-
- -
|
41
|
+
- - '>='
|
47
42
|
- !ruby/object:Gem::Version
|
48
43
|
version: '0'
|
49
44
|
- !ruby/object:Gem::Dependency
|
50
45
|
name: rdoc
|
51
46
|
requirement: !ruby/object:Gem::Requirement
|
52
|
-
none: false
|
53
47
|
requirements:
|
54
|
-
- -
|
48
|
+
- - '>='
|
55
49
|
- !ruby/object:Gem::Version
|
56
50
|
version: '0'
|
57
51
|
type: :development
|
58
52
|
prerelease: false
|
59
53
|
version_requirements: !ruby/object:Gem::Requirement
|
60
|
-
none: false
|
61
54
|
requirements:
|
62
|
-
- -
|
55
|
+
- - '>='
|
63
56
|
- !ruby/object:Gem::Version
|
64
57
|
version: '0'
|
65
58
|
description: A gem that adds simple support for organizing ActiveRecord models into
|
@@ -89,28 +82,27 @@ files:
|
|
89
82
|
- test/acts_as_tree_test.rb
|
90
83
|
homepage: https://github.com/amerine/acts_as_tree
|
91
84
|
licenses: []
|
85
|
+
metadata: {}
|
92
86
|
post_install_message:
|
93
87
|
rdoc_options:
|
94
88
|
- --charset=UTF-8
|
95
89
|
require_paths:
|
96
90
|
- lib
|
97
91
|
required_ruby_version: !ruby/object:Gem::Requirement
|
98
|
-
none: false
|
99
92
|
requirements:
|
100
|
-
- -
|
93
|
+
- - '>='
|
101
94
|
- !ruby/object:Gem::Version
|
102
95
|
version: '0'
|
103
96
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
|
-
none: false
|
105
97
|
requirements:
|
106
|
-
- -
|
98
|
+
- - '>='
|
107
99
|
- !ruby/object:Gem::Version
|
108
100
|
version: '0'
|
109
101
|
requirements: []
|
110
102
|
rubyforge_project:
|
111
|
-
rubygems_version:
|
103
|
+
rubygems_version: 2.0.0
|
112
104
|
signing_key:
|
113
|
-
specification_version:
|
105
|
+
specification_version: 4
|
114
106
|
summary: Provides a simple tree behaviour to active_record models.
|
115
107
|
test_files:
|
116
108
|
- test/acts_as_tree_test.rb
|