acts_as_tree 2.1.0 → 2.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +11 -0
- data/gemfiles/rails-3.0.gemfile +2 -1
- data/gemfiles/rails-3.1.gemfile +2 -1
- data/gemfiles/rails-3.2.gemfile +2 -1
- data/gemfiles/rails-4.0.gemfile +1 -1
- data/lib/acts_as_tree.rb +30 -21
- data/lib/acts_as_tree/version.rb +1 -1
- data/test/acts_as_tree_test.rb +24 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1195db0f542f5b2a66b53ed35a6a3d0e821f282f
|
4
|
+
data.tar.gz: 4564ce6846efa1aeb4f03dc632a1e8f151273757
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4fd0449d5a36b43d11738b6a3d7097a69977e08464530d374b55dd6120bd65f4baeaae3f4122726e609cab2168383def9be5e8c9f6be3f42760e665032105e54
|
7
|
+
data.tar.gz: aec46f7ee97a3731221c21f625efea8bbf7fc86f5ded9e8e7abe77305722711171beaf61d61b412a62ee4e2640980a5c8956a604ef62160ae44830b89a0c165f
|
data/README.md
CHANGED
@@ -59,6 +59,14 @@ In your view you could traverse the tree using
|
|
59
59
|
<% end %>
|
60
60
|
```
|
61
61
|
|
62
|
+
You also could use walk\_tree as an instance method such as:
|
63
|
+
|
64
|
+
```erb
|
65
|
+
<% Page.first.walk_tree do |page, level| %>
|
66
|
+
<%= link_to "#{'-'*level}#{page.name}", page_path(page) %><br />
|
67
|
+
<% end %>
|
68
|
+
```
|
69
|
+
|
62
70
|
## Compatibility
|
63
71
|
|
64
72
|
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.
|
@@ -66,6 +74,9 @@ We no longer support Ruby 1.8 or versions of Rails/ActiveRecord older than 3.0.
|
|
66
74
|
Moving forward we will do our best to support the latest versions of ActiveRecord and Ruby.
|
67
75
|
|
68
76
|
## Change Log
|
77
|
+
* 2.2.0 - June 15, 2015
|
78
|
+
* Added TreeWalker.walk\_tree instance method. See #32, #37, #38 -- felixbuenemann, genewoo
|
79
|
+
* Fix tests on rails 3.x. See #36 -- marshall-lee
|
69
80
|
* 2.1.0 - September 25, 2014
|
70
81
|
* Added TreeWalker. See #30 -- 545ch4
|
71
82
|
* 2.0.0 - July 3, 2014
|
data/gemfiles/rails-3.0.gemfile
CHANGED
data/gemfiles/rails-3.1.gemfile
CHANGED
data/gemfiles/rails-3.2.gemfile
CHANGED
data/gemfiles/rails-4.0.gemfile
CHANGED
data/lib/acts_as_tree.rb
CHANGED
@@ -185,30 +185,39 @@ module ActsAsTree
|
|
185
185
|
# <%= link_to "#{' '*level}#{page.name}", page_path(page) %><br />
|
186
186
|
# <% end %>
|
187
187
|
#
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
end
|
204
|
-
else
|
205
|
-
block.call node, level
|
206
|
-
node.children.where(options[:where]).each do |child|
|
207
|
-
walk_tree options, level + 1, child, &block
|
208
|
-
end
|
188
|
+
# There is also a walk_tree instance method that starts walking from
|
189
|
+
# the node it is called on.
|
190
|
+
#
|
191
|
+
def walk_tree(options = {}, &block)
|
192
|
+
algorithm = options.fetch :algorithm, :dfs
|
193
|
+
where = options.fetch :where, {}
|
194
|
+
send("walk_tree_#{algorithm}", where, &block)
|
195
|
+
end
|
196
|
+
|
197
|
+
def self.extended(mod)
|
198
|
+
mod.class_eval do
|
199
|
+
def walk_tree(options = {}, &block)
|
200
|
+
algorithm = options.fetch :algorithm, :dfs
|
201
|
+
where = options.fetch :where, {}
|
202
|
+
self.class.send("walk_tree_#{algorithm}", where, self, &block)
|
209
203
|
end
|
210
204
|
end
|
211
205
|
end
|
206
|
+
|
207
|
+
private
|
208
|
+
|
209
|
+
def walk_tree_bfs(where = {}, node = nil, level = -1, &block)
|
210
|
+
nodes = (node.nil? ? roots : node.children).where(where)
|
211
|
+
nodes.each { |child| block.call child, level + 1 }
|
212
|
+
nodes.each { |child| walk_tree_bfs where, child, level + 1, &block }
|
213
|
+
end
|
214
|
+
|
215
|
+
def walk_tree_dfs(where = {}, node = nil, level = -1, &block)
|
216
|
+
block.call node, level unless level == -1
|
217
|
+
nodes = (node.nil? ? roots : node.children).where(where)
|
218
|
+
nodes.each { |child| walk_tree_dfs where, child, level + 1, &block }
|
219
|
+
end
|
220
|
+
|
212
221
|
end
|
213
222
|
|
214
223
|
module InstanceMethods
|
data/lib/acts_as_tree/version.rb
CHANGED
data/test/acts_as_tree_test.rb
CHANGED
@@ -24,7 +24,12 @@ class MiniTest::Unit::TestCase
|
|
24
24
|
end
|
25
25
|
}
|
26
26
|
|
27
|
-
|
27
|
+
begin
|
28
|
+
subscribed = ActiveSupport::Notifications.subscribe("sql.active_record", &counter_f)
|
29
|
+
result = block.call
|
30
|
+
ensure
|
31
|
+
ActiveSupport::Notifications.unsubscribe subscribed
|
32
|
+
end
|
28
33
|
|
29
34
|
[count, result]
|
30
35
|
end
|
@@ -267,8 +272,10 @@ class TreeTest < MiniTest::Unit::TestCase
|
|
267
272
|
|
268
273
|
def test_tree_walker
|
269
274
|
assert_equal false, TreeMixin.respond_to?(:walk_tree)
|
275
|
+
assert_equal false, TreeMixin.new.respond_to?(:walk_tree)
|
270
276
|
TreeMixin.extend ActsAsTree::TreeWalker
|
271
277
|
assert_equal true, TreeMixin.respond_to?(:walk_tree)
|
278
|
+
assert_equal true, TreeMixin.new.respond_to?(:walk_tree)
|
272
279
|
|
273
280
|
walk_tree_dfs_output = <<-END.gsub(/^\s+/, '')
|
274
281
|
1
|
@@ -281,6 +288,14 @@ class TreeTest < MiniTest::Unit::TestCase
|
|
281
288
|
END
|
282
289
|
assert_equal walk_tree_dfs_output, capture_stdout { TreeMixin.walk_tree{|elem, level| puts "#{'-'*level}#{elem.id}"} }
|
283
290
|
|
291
|
+
walk_tree_dfs_sub_output = <<-END.gsub(/^\s+/, '')
|
292
|
+
2
|
293
|
+
-3
|
294
|
+
--4
|
295
|
+
5
|
296
|
+
END
|
297
|
+
assert_equal walk_tree_dfs_sub_output, capture_stdout { @root1.walk_tree{|elem, level| puts "#{'-'*level}#{elem.id}"} }
|
298
|
+
|
284
299
|
walk_tree_bfs_output = <<-END.gsub(/^\s+/, '')
|
285
300
|
1
|
286
301
|
6
|
@@ -291,6 +306,14 @@ class TreeTest < MiniTest::Unit::TestCase
|
|
291
306
|
---4
|
292
307
|
END
|
293
308
|
assert_equal walk_tree_bfs_output, capture_stdout { TreeMixin.walk_tree(:algorithm => :bfs){|elem, level| puts "#{'-'*level}#{elem.id}"} }
|
309
|
+
|
310
|
+
walk_tree_bfs_sub_output = <<-END.gsub(/^\s+/, '')
|
311
|
+
2
|
312
|
+
5
|
313
|
+
-3
|
314
|
+
--4
|
315
|
+
END
|
316
|
+
assert_equal walk_tree_bfs_sub_output, capture_stdout { @root1.walk_tree(:algorithm => :bfs){|elem, level| puts "#{'-'*level}#{elem.id}"} }
|
294
317
|
end
|
295
318
|
end
|
296
319
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acts_as_tree
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Erik Dahlstrand
|
@@ -12,7 +12,7 @@ authors:
|
|
12
12
|
autorequire:
|
13
13
|
bindir: bin
|
14
14
|
cert_chain: []
|
15
|
-
date:
|
15
|
+
date: 2015-06-14 00:00:00.000000000 Z
|
16
16
|
dependencies:
|
17
17
|
- !ruby/object:Gem::Dependency
|
18
18
|
name: activerecord
|
@@ -116,7 +116,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
116
116
|
version: '0'
|
117
117
|
requirements: []
|
118
118
|
rubyforge_project:
|
119
|
-
rubygems_version: 2.
|
119
|
+
rubygems_version: 2.4.6
|
120
120
|
signing_key:
|
121
121
|
specification_version: 4
|
122
122
|
summary: Provides a simple tree behaviour to active_record models.
|