acts_as_tree 2.0.0 → 2.1.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 780f4a181d2e28b847648e7fcacd7e29e29fbea9
4
- data.tar.gz: db729268df6e10dbbbfc4626b3c279c4f5a38b4a
3
+ metadata.gz: 0010c309598e23c144cafccabd21527bcd4b52e0
4
+ data.tar.gz: dd8825aff3d508e09c8d5f28240e010af7d19716
5
5
  SHA512:
6
- metadata.gz: 4df9150dac385c3ebcc0e365d7c9031bc6e535f874ff7e5118c476fd455e36d3887a943f37c8299fda98851321ca5bac9deb3deb90a8ec52bf90c9e1eda58ab6
7
- data.tar.gz: cabc21ab491173998e506372b08adea1b3ee695ef25cbf4a88ef23e1f34cf9e3d80e54ffe671505a3cb6931df55260baa4b0361ae9b3ea52c1a84e8892cf4e44
6
+ metadata.gz: 2fed1a5bf286379948f3b11797f46d03b4db877b648f9a28221495e6cde387d75fe6905db99ea84a947c9df72560cc3d1de718cce269e491fd50f6daf195262e
7
+ data.tar.gz: 0b08f66b4c2ff06faaa9129c662ad4e55f1286dc357bfe435ffe720ee760cd289d482c4dca5b27d5e1cbc173cbb1eb8b46b60ac759eb492d35c8692fa94c6692
data/README.md CHANGED
@@ -27,7 +27,7 @@ We also have a convenient `TreeView` module you can mixin if you want a little v
27
27
  class Category < ActiveRecord::Base
28
28
  extend ActsAsTree::TreeView
29
29
 
30
- acts_as_tree order: "name"
30
+ acts_as_tree order: 'name'
31
31
  end
32
32
 
33
33
  > Category.tree_view(:name)
@@ -41,6 +41,24 @@ root
41
41
  => nil
42
42
  ```
43
43
 
44
+ And there's a `TreeWalker` module (traversing the tree using depth-first search (default) or breadth-first search) as well. Example given the Model `Page` as
45
+
46
+ ```ruby
47
+ class Page < ActiveRecord::Base
48
+ extend ActsAsTree::TreeWalker
49
+
50
+ acts_as_tree order: 'rank'
51
+ end
52
+ ```
53
+
54
+ In your view you could traverse the tree using
55
+
56
+ ```erb
57
+ <% Page.walk_tree do |page, level| %>
58
+ <%= link_to "#{'-'*level}#{page.name}", page_path(page) %><br />
59
+ <% end %>
60
+ ```
61
+
44
62
  ## Compatibility
45
63
 
46
64
  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.
@@ -48,6 +66,8 @@ We no longer support Ruby 1.8 or versions of Rails/ActiveRecord older than 3.0.
48
66
  Moving forward we will do our best to support the latest versions of ActiveRecord and Ruby.
49
67
 
50
68
  ## Change Log
69
+ * 2.1.0 - September 25, 2014
70
+ * Added TreeWalker. See #30 -- 545ch4
51
71
  * 2.0.0 - July 3, 2014
52
72
  * Renamed Presentation module to TreeView, see #27, #28 -- felixbuenemann
53
73
  * 1.6.1 - May 29, 2014
@@ -166,6 +166,51 @@ module ActsAsTree
166
166
 
167
167
  end
168
168
 
169
+ module TreeWalker
170
+ # Traverse the tree and call a block with the current node and current
171
+ # depth-level.
172
+ #
173
+ # options:
174
+ # algorithm:
175
+ # :dfs for depth-first search (default)
176
+ # :bfs for breadth-first search
177
+ # where: AR where statement to filter certain nodes
178
+ #
179
+ # The given block sets two parameters:
180
+ # first: The current node
181
+ # second: The current depth-level within the tree
182
+ #
183
+ # Example of acts_as_tree for model Page (ERB view):
184
+ # <% Page.walk_tree do |page, level| %>
185
+ # <%= link_to "#{' '*level}#{page.name}", page_path(page) %><br />
186
+ # <% end %>
187
+ #
188
+ def walk_tree(_options = {}, level = 0, node = nil, &block)
189
+ options = {:algorithm => :dfs, :where => {}}.update(_options)
190
+ case options[:algorithm]
191
+ when :bfs
192
+ nodes = (node.nil? ? roots : node.children).where(options[:where])
193
+ nodes.each do |child|
194
+ block.call child, level
195
+ end
196
+ nodes.each do |child|
197
+ walk_tree options, level + 1, child, &block
198
+ end
199
+ else
200
+ if node.nil?
201
+ roots.where(options[:where]).each do |root_node|
202
+ walk_tree options, level, root_node, &block
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
209
+ end
210
+ end
211
+ end
212
+ end
213
+
169
214
  module InstanceMethods
170
215
  # Returns list of ancestors, starting from parent until root.
171
216
  #
@@ -1,3 +1,3 @@
1
1
  module ActsAsTree
2
- VERSION = "2.0.0"
2
+ VERSION = "2.1.0"
3
3
  end
@@ -264,6 +264,34 @@ class TreeTest < MiniTest::Unit::TestCase
264
264
  END
265
265
  assert_equal tree_view_outputs, capture_stdout { TreeMixin.tree_view(:id) }
266
266
  end
267
+
268
+ def test_tree_walker
269
+ assert_equal false, TreeMixin.respond_to?(:walk_tree)
270
+ TreeMixin.extend ActsAsTree::TreeWalker
271
+ assert_equal true, TreeMixin.respond_to?(:walk_tree)
272
+
273
+ walk_tree_dfs_output = <<-END.gsub(/^\s+/, '')
274
+ 1
275
+ -2
276
+ --3
277
+ ---4
278
+ -5
279
+ 6
280
+ 7
281
+ END
282
+ assert_equal walk_tree_dfs_output, capture_stdout { TreeMixin.walk_tree{|elem, level| puts "#{'-'*level}#{elem.id}"} }
283
+
284
+ walk_tree_bfs_output = <<-END.gsub(/^\s+/, '')
285
+ 1
286
+ 6
287
+ 7
288
+ -2
289
+ -5
290
+ --3
291
+ ---4
292
+ END
293
+ assert_equal walk_tree_bfs_output, capture_stdout { TreeMixin.walk_tree(:algorithm => :bfs){|elem, level| puts "#{'-'*level}#{elem.id}"} }
294
+ end
267
295
  end
268
296
 
269
297
  class TestDeepDescendantsPerformance < MiniTest::Unit::TestCase
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.0.0
4
+ version: 2.1.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: 2014-07-03 00:00:00.000000000 Z
15
+ date: 2014-09-25 00:00:00.000000000 Z
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
18
18
  name: activerecord