Skip to content

Commit bb66282

Browse files
committed
fixed ancestor/descendant relationships to be habtm
added test and doc for leaves (class scoped and instance methods)
1 parent 82b9224 commit bb66282

File tree

4 files changed

+26
-18
lines changed

4 files changed

+26
-18
lines changed

README.rdoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ Note that the other columns will be null if nodes are created, other than auto-g
105105
[tag.root?] returns true if this is a root node
106106
[tag.child?] returns true if this is a child node. It has a parent.
107107
[tag.leaf?] returns true if this is a leaf node. It has no children.
108+
[tag.leaves] returns an array of all the nodes in self_and_descendants that are leaves.
108109
[tag.level] returns the level, or "generation", for this node in the tree. A root node = 0
109110
[tag.parent] returns the node's immediate parent
110111
[tag.children] returns an array of immediate children (just those in the next level).

lib/closure_tree/acts_as_tree.rb

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,23 @@ def acts_as_tree options = {}
3333
:foreign_key => parent_column_name,
3434
:before_add => :add_child
3535

36-
has_many :ancestors_hierarchy,
37-
:class_name => hierarchy_class_name,
38-
:foreign_key => "descendant_id"
39-
40-
has_many :ancestors, :through => :ancestors_hierarchy,
41-
:order => "generations asc"
42-
43-
has_many :descendants_hierarchy,
44-
:class_name => hierarchy_class_name,
45-
:foreign_key => "ancestor_id"
46-
47-
has_many :descendants, :through => :descendants_hierarchy,
48-
:order => "generations asc"
36+
has_and_belongs_to_many :ancestors,
37+
:class_name => base_class.to_s,
38+
:join_table => hierarchy_table_name,
39+
:foreign_key => "descendant_id",
40+
:association_foreign_key => "ancestor_id",
41+
:order => "generations asc"
42+
43+
has_and_belongs_to_many :descendants,
44+
:class_name => base_class.to_s,
45+
:join_table => hierarchy_table_name,
46+
:foreign_key => "ancestor_id",
47+
:association_foreign_key => "descendant_id",
48+
:order => "generations asc"
4949

5050
scope :roots, where(parent_column_name => nil)
5151

52-
scope :leaves, includes(:descendants_hierarchy).where("#{hierarchy_table_name}.descendant_id is null")
52+
scope :leaves, includes(:descendants).where("#{hierarchy_table_name}.descendant_id is null")
5353
end
5454
end
5555

@@ -80,7 +80,8 @@ def leaf?
8080
end
8181

8282
def leaves
83-
self.class.scoped.includes(:descendants_hierarchy).where("#{hierarchy_table_name}.descendant_id is null and #{hierarchy_table_name}.ancestor_id = #{id}")
83+
return [self] if leaf?
84+
Tag.leaves.includes(:ancestors).where("ancestors_tags.id = ?", self.id)
8485
end
8586

8687
# Returns true if this node has a parent, and is not a root.
@@ -215,8 +216,6 @@ def rebuild_node_and_children node
215216
# Mixed into both classes and instances to provide easy access to the column names
216217
module Columns
217218

218-
protected
219-
220219
def parent_column_name
221220
closure_tree_options[:parent_column_name]
222221
end

lib/closure_tree/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module ClosureTree
2-
VERSION = "1.0.0.beta4" unless defined?(::ClosureTree::VERSION)
2+
VERSION = "1.0.0.beta5" unless defined?(::ClosureTree::VERSION)
33
end

test/dummy/test/unit/tag_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,5 +106,13 @@ def test_root
106106
assert_equal tags(:grandparent), tags(:parent).root
107107
assert_equal tags(:grandparent), tags(:child).root
108108
end
109+
110+
def test_leaves
111+
assert Tag.leaves.include? tags(:child)
112+
assert Tag.leaves.select{|t|!t.leaf?}.empty?
113+
assert_equal [tags(:child)], tags(:grandparent).leaves
114+
assert_equal [tags(:child)], tags(:parent).leaves
115+
assert_equal [tags(:child)], tags(:child).leaves
116+
end
109117
end
110118

0 commit comments

Comments
 (0)