acts_as_tree 2.1.0 → 2.2.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: 0010c309598e23c144cafccabd21527bcd4b52e0
4
- data.tar.gz: dd8825aff3d508e09c8d5f28240e010af7d19716
3
+ metadata.gz: 1195db0f542f5b2a66b53ed35a6a3d0e821f282f
4
+ data.tar.gz: 4564ce6846efa1aeb4f03dc632a1e8f151273757
5
5
  SHA512:
6
- metadata.gz: 2fed1a5bf286379948f3b11797f46d03b4db877b648f9a28221495e6cde387d75fe6905db99ea84a947c9df72560cc3d1de718cce269e491fd50f6daf195262e
7
- data.tar.gz: 0b08f66b4c2ff06faaa9129c662ad4e55f1286dc357bfe435ffe720ee760cd289d482c4dca5b27d5e1cbc173cbb1eb8b46b60ac759eb492d35c8692fa94c6692
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
@@ -1,6 +1,7 @@
1
1
  source "https://rubygems.org"
2
2
 
3
- gem "rails", "~> 3.0"
3
+ gem "rails", "~> 3.0.0"
4
4
  gem "acts_as_tree", path: "../"
5
+ gem "i18n", "< 0.7"
5
6
 
6
7
  gemspec path: "../"
@@ -1,6 +1,7 @@
1
1
  source "https://rubygems.org"
2
2
 
3
- gem "rails", "~> 3.1"
3
+ gem "rails", "~> 3.1.0"
4
4
  gem "acts_as_tree", path: "../"
5
+ gem "i18n", "< 0.7"
5
6
 
6
7
  gemspec path: "../"
@@ -1,6 +1,7 @@
1
1
  source "https://rubygems.org"
2
2
 
3
- gem "rails", "~> 3.2"
3
+ gem "rails", "~> 3.2.0"
4
4
  gem "acts_as_tree", path: "../"
5
+ gem "i18n", "< 0.7"
5
6
 
6
7
  gemspec path: "../"
@@ -1,6 +1,6 @@
1
1
  source "https://rubygems.org"
2
2
 
3
- gem "rails", "~> 4.0"
3
+ gem "rails", "~> 4.0.0"
4
4
  gem "acts_as_tree", path: "../"
5
5
 
6
6
  gemspec path: "../"
@@ -185,30 +185,39 @@ module ActsAsTree
185
185
  # <%= link_to "#{' '*level}#{page.name}", page_path(page) %><br />
186
186
  # <% end %>
187
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
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
@@ -1,3 +1,3 @@
1
1
  module ActsAsTree
2
- VERSION = "2.1.0"
2
+ VERSION = "2.2.0"
3
3
  end
@@ -24,7 +24,12 @@ class MiniTest::Unit::TestCase
24
24
  end
25
25
  }
26
26
 
27
- result = ActiveSupport::Notifications.subscribed(counter_f, "sql.active_record", &block)
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.1.0
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: 2014-09-25 00:00:00.000000000 Z
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.2.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.