From 32bb131630c95c6c464fa1ecbf1e53ab6f80f5f4 Mon Sep 17 00:00:00 2001 From: markhgbrewster Date: Wed, 11 Jan 2017 19:00:01 +0000 Subject: [PATCH 1/3] fix typos --- lib/acts_as_tree.rb | 8 ++++---- test/acts_as_tree_test.rb | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/acts_as_tree.rb b/lib/acts_as_tree.rb index 54b255b..e66bcec 100644 --- a/lib/acts_as_tree.rb +++ b/lib/acts_as_tree.rb @@ -127,7 +127,7 @@ def self.roots # Returns a hash of all nodes grouped by their level in the tree structure. # - # Class.generations { 0=> [root1, root2], 1=> [root1child1, root1child2, root2child1, root2child2] } + # Class.generations # => { 0=> [root1, root2], 1=> [root1child1, root1child2, root2child1, root2child2] } def self.generations all.group_by{ |node| node.level } end @@ -290,21 +290,21 @@ def self_and_siblings # Returns all the nodes at the same level in the tree as the current node. # - # root1child1.generation [root1child2, root2child1, root2child2] + # root1child1.generation # => [root1child2, root2child1, root2child2] def generation self_and_generation - [self] end # Returns a reference to the current node and all the nodes at the same level as it in the tree. # - # root1child1.generation [root1child1, root1child2, root2child1, root2child2] + # root1child1.self_and_generation # => [root1child1, root1child2, root2child1, root2child2] def self_and_generation self.class.select {|node| node.level == self.level } end # Returns the level (depth) of the current node # - # root1child1.level 1 + # root1child1.level # => 1 def level self.ancestors.size end diff --git a/test/acts_as_tree_test.rb b/test/acts_as_tree_test.rb index de57564..afbb31b 100644 --- a/test/acts_as_tree_test.rb +++ b/test/acts_as_tree_test.rb @@ -541,7 +541,7 @@ def test_nullify end end -class GenertaionMethods < ActsAsTreeTestCase +class GenerationMethods < ActsAsTreeTestCase def setup setup_db From 4ac1e5c81cb8942734fc49e1f548de8b22a73caa Mon Sep 17 00:00:00 2001 From: markhgbrewster Date: Wed, 11 Jan 2017 19:08:34 +0000 Subject: [PATCH 2/3] rename level method tree_level --- lib/acts_as_tree.rb | 12 +++++++----- test/acts_as_tree_test.rb | 10 +++++----- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/lib/acts_as_tree.rb b/lib/acts_as_tree.rb index e66bcec..6d2b75f 100644 --- a/lib/acts_as_tree.rb +++ b/lib/acts_as_tree.rb @@ -129,9 +129,10 @@ def self.roots # # Class.generations # => { 0=> [root1, root2], 1=> [root1child1, root1child2, root2child1, root2child2] } def self.generations - all.group_by{ |node| node.level } + all.group_by{ |node| node.tree_level } end + if configuration[:counter_cache] after_update :update_parents_counter_cache @@ -299,13 +300,14 @@ def generation # # root1child1.self_and_generation # => [root1child1, root1child2, root2child1, root2child2] def self_and_generation - self.class.select {|node| node.level == self.level } + self.class.select {|node| node.tree_level == self.tree_level } end - # Returns the level (depth) of the current node + # Returns the level (depth) of the current node # - # root1child1.level # => 1 - def level + # root1child1.tree_level # => 1 + + def tree_level self.ancestors.size end diff --git a/test/acts_as_tree_test.rb b/test/acts_as_tree_test.rb index afbb31b..dcbb623 100644 --- a/test/acts_as_tree_test.rb +++ b/test/acts_as_tree_test.rb @@ -586,11 +586,11 @@ def test_self_and_generation assert_equal [@child1_child_child], @child1_child_child.self_and_generation end - def test_level - assert_equal 0, @root1.level - assert_equal 1, @root_child1.level - assert_equal 2, @child1_child.level - assert_equal 3, @child1_child_child.level + def test_tree_level + assert_equal 0, @root1.tree_level + assert_equal 1, @root_child1.tree_level + assert_equal 2, @child1_child.tree_level + assert_equal 3, @child1_child_child.tree_level end end From 7d7bf6eb9ed2dc6a5bc0e1991bec6b9447c483ab Mon Sep 17 00:00:00 2001 From: markhgbrewster Date: Wed, 11 Jan 2017 21:38:49 +0000 Subject: [PATCH 3/3] alias level to tree level --- lib/acts_as_tree.rb | 14 +++++++++++++- test/acts_as_tree_test.rb | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/lib/acts_as_tree.rb b/lib/acts_as_tree.rb index 6d2b75f..211b0dc 100644 --- a/lib/acts_as_tree.rb +++ b/lib/acts_as_tree.rb @@ -306,11 +306,23 @@ def self_and_generation # Returns the level (depth) of the current node # # root1child1.tree_level # => 1 - def tree_level self.ancestors.size end + # Returns the level (depth) of the current node unless level is a column on the node. + # Allows backwards compatibility with older versions of the gem. + # Allows integration with apps using level as a column name. + # + # root1child1.level # => 1 + def level + if self.class.column_names.include?('level') + super + else + tree_level + end + end + # Returns children (without subchildren) and current node itself. # # root.self_and_children # => [root, child1] diff --git a/test/acts_as_tree_test.rb b/test/acts_as_tree_test.rb index dcbb623..035290e 100644 --- a/test/acts_as_tree_test.rb +++ b/test/acts_as_tree_test.rb @@ -60,6 +60,12 @@ def setup_db(options = {}) t.column :children_count, :integer, default: 0 if options[:counter_cache] t.timestamps null: false end + + create_table :level_mixins, force: true do |t| + t.column :level, :string + t.column :parent_id, :integer + t.timestamps null: false + end end # Fix broken reset_column_information in some activerecord versions. @@ -75,10 +81,23 @@ class Mixin < ActiveRecord::Base include ActsAsTree end +class LevelMixin < ActiveRecord::Base + include ActsAsTree + acts_as_tree foreign_key: "parent_id", order: "id" +end + class TreeMixin < Mixin acts_as_tree foreign_key: "parent_id", order: "id" end +class TreeMixinWithLevelMethod < Mixin + acts_as_tree foreign_key: "parent_id", order: "id" + + def level + 'Has Level Method' + end +end + class TreeMixinWithoutOrder < Mixin acts_as_tree foreign_key: "parent_id" end @@ -555,6 +574,8 @@ def setup @root2_child2 = TreeMixin.create! parent_id: @root2.id @root2_child1_child = TreeMixin.create! parent_id: @root2_child1.id @root3 = TreeMixin.create! + @level_column = LevelMixin.create! level: 'Has Level Column' + @level_method = TreeMixinWithLevelMethod.create! end def test_generations @@ -592,5 +613,17 @@ def test_tree_level assert_equal 2, @child1_child.tree_level assert_equal 3, @child1_child_child.tree_level end + + def test_level + assert_equal 0, @root1.level + assert_equal 1, @root_child1.level + assert_equal 2, @child1_child.level + assert_equal 3, @child1_child_child.level + end + + def test_alias_tree_level + assert_equal 'Has Level Method', @level_method.level + assert_equal 'Has Level Column', @level_column.level + end end