acts_as_tree 2.5.1 → 2.6.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: 4c39b13dfb04c4ed2870fe0c459880233959cf86
4
- data.tar.gz: 672b23b7db5a373f819dbe33628f48578f8391bd
3
+ metadata.gz: c1348043cad77cbacfb197eb2d96ff6e1ac84be3
4
+ data.tar.gz: 305512fe8ce041f273a65fe951343e8a37555a93
5
5
  SHA512:
6
- metadata.gz: f4538630240dd1896649e93b37e7b9064f87176191e58dde70d6cdb0d3cb2da47bd1ea36d5d8d88706998c3cb1f052b110796a6ee4738141331fd93bff262f8b
7
- data.tar.gz: 6b53454a3d359f8a161f3fd2f0c28bae4958023e59c965fe8c643065c12c1551925613531071aa2ebebd6e350626309c3b805edd0e35f00bdd1594741b40db05
6
+ metadata.gz: 81a7871c4c36509e001435e8161415fe067768bf8080b1ef824e7d95ec2a710075a3974fc141eb7d15f3d87c3b6d5dbe4a4d9b858d1f32a66555b26cd7885b83
7
+ data.tar.gz: 033de96f25e5a03f3e605a6c7940ce440819bf0754154ac24dfac1568c34880107647e7d4164ef102d3ad2b07e7f50d6ea634a997b4de10fd65ffdbf5bc7de94
data/README.md CHANGED
@@ -74,6 +74,8 @@ We no longer support Ruby 1.8 or versions of Rails/ActiveRecord older than 3.0.
74
74
  Moving forward we will do our best to support the latest versions of ActiveRecord and Ruby.
75
75
 
76
76
  ## Change Log
77
+ * 2.6.0 - October 9, 2016
78
+ * Add generations methods, see #56 -- markhgbrewster
77
79
  * 2.5.1 - September 8, 2016
78
80
  * Fix early database connection in acts\_as\_tree, see #55 -- felixbuenemann
79
81
  * 2.5.0 - August 14, 2016
data/lib/acts_as_tree.rb CHANGED
@@ -125,6 +125,13 @@ module ActsAsTree
125
125
  end
126
126
  EOV
127
127
 
128
+ # Returns a hash of all nodes grouped by their level in the tree structure.
129
+ #
130
+ # Class.generations { 0=> [root1, root2], 1=> [root1child1, root1child2, root2child1, root2child2] }
131
+ def self.generations
132
+ all.group_by{ |node| node.level }
133
+ end
134
+
128
135
  if configuration[:counter_cache]
129
136
  after_update :update_parents_counter_cache
130
137
 
@@ -281,6 +288,27 @@ module ActsAsTree
281
288
  parent ? parent.children : self.class.roots
282
289
  end
283
290
 
291
+ # Returns all the nodes at the same level in the tree as the current node.
292
+ #
293
+ # root1child1.generation [root1child2, root2child1, root2child2]
294
+ def generation
295
+ self_and_generation - [self]
296
+ end
297
+
298
+ # Returns a reference to the current node and all the nodes at the same level as it in the tree.
299
+ #
300
+ # root1child1.generation [root1child1, root1child2, root2child1, root2child2]
301
+ def self_and_generation
302
+ self.class.select {|node| node.level == self.level }
303
+ end
304
+
305
+ # Returns the level (depth) of the current node
306
+ #
307
+ # root1child1.level 1
308
+ def level
309
+ self.ancestors.size
310
+ end
311
+
284
312
  # Returns children (without subchildren) and current node itself.
285
313
  #
286
314
  # root.self_and_children # => [root, child1]
@@ -1,3 +1,3 @@
1
1
  module ActsAsTree
2
- VERSION = "2.5.1"
2
+ VERSION = "2.6.0"
3
3
  end
@@ -496,7 +496,6 @@ class TreeTestWithCounterCache < ActsAsTreeTestCase
496
496
  end
497
497
  end
498
498
 
499
-
500
499
  class TreeTestWithTouch < ActsAsTreeTestCase
501
500
  def setup
502
501
  setup_db
@@ -541,3 +540,57 @@ class ExternalTreeTest < TreeTest
541
540
  assert_nil root4_child.reload.external_parent_id
542
541
  end
543
542
  end
543
+
544
+ class GenertaionMethods < ActsAsTreeTestCase
545
+ def setup
546
+ setup_db
547
+
548
+ @root1 = TreeMixin.create!
549
+ @root_child1 = TreeMixin.create! parent_id: @root1.id
550
+ @child1_child = TreeMixin.create! parent_id: @root_child1.id
551
+ @child1_child_child = TreeMixin.create! parent_id: @child1_child.id
552
+ @root_child2 = TreeMixin.create! parent_id: @root1.id
553
+ @root2 = TreeMixin.create!
554
+ @root2_child1 = TreeMixin.create! parent_id: @root2.id
555
+ @root2_child2 = TreeMixin.create! parent_id: @root2.id
556
+ @root2_child1_child = TreeMixin.create! parent_id: @root2_child1.id
557
+ @root3 = TreeMixin.create!
558
+ end
559
+
560
+ def test_generations
561
+ assert_equal(
562
+ {
563
+ 0 => [@root1, @root2, @root3],
564
+ 1 => [@root_child1, @root_child2, @root2_child1, @root2_child2],
565
+ 2 => [@child1_child, @root2_child1_child],
566
+ 3 => [@child1_child_child]
567
+ },
568
+ TreeMixin.generations
569
+ )
570
+ end
571
+
572
+ def test_generation
573
+ assert_equal [@root2, @root3], @root1.generation
574
+ assert_equal [@root_child2, @root2_child1, @root2_child2],
575
+ @root_child1.generation
576
+ assert_equal [@root2_child1_child], @child1_child.generation
577
+ assert_equal [], @child1_child_child.generation
578
+ end
579
+
580
+ def test_self_and_generation
581
+ assert_equal [@root1, @root2, @root3], @root1.self_and_generation
582
+ assert_equal [@root_child1, @root_child2, @root2_child1, @root2_child2],
583
+ @root_child1.self_and_generation
584
+ assert_equal [@child1_child, @root2_child1_child],
585
+ @child1_child.self_and_generation
586
+ assert_equal [@child1_child_child], @child1_child_child.self_and_generation
587
+ end
588
+
589
+ def test_level
590
+ assert_equal 0, @root1.level
591
+ assert_equal 1, @root_child1.level
592
+ assert_equal 2, @child1_child.level
593
+ assert_equal 3, @child1_child_child.level
594
+ end
595
+ end
596
+
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.5.1
4
+ version: 2.6.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: 2016-09-08 00:00:00.000000000 Z
15
+ date: 2016-10-09 00:00:00.000000000 Z
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
18
18
  name: activerecord