closure_tree 1.0.0.beta3 → 1.0.0.beta5

Sign up to get free protection for your applications and to get access to all the features.
@@ -105,6 +105,7 @@ Note that the other columns will be null if nodes are created, other than auto-g
105
105
  [tag.root?] returns true if this is a root node
106
106
  [tag.child?] returns true if this is a child node. It has a parent.
107
107
  [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.
108
109
  [tag.level] returns the level, or "generation", for this node in the tree. A root node = 0
109
110
  [tag.parent] returns the node's immediate parent
110
111
  [tag.children] returns an array of immediate children (just those in the next level).
@@ -33,23 +33,23 @@ module ClosureTree #:nodoc:
33
33
  :foreign_key => parent_column_name,
34
34
  :before_add => :add_child
35
35
 
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"
49
49
 
50
50
  scope :roots, where(parent_column_name => nil)
51
51
 
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")
53
53
  end
54
54
  end
55
55
 
@@ -69,13 +69,19 @@ module ClosureTree #:nodoc:
69
69
  parent_id.nil?
70
70
  end
71
71
 
72
+ # Returns self if +root?+ or the root ancestor
73
+ def root
74
+ root? ? self : ancestors.last
75
+ end
76
+
72
77
  # Returns true if this node has no children.
73
78
  def leaf?
74
79
  children.empty?
75
80
  end
76
81
 
77
82
  def leaves
78
- 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)
79
85
  end
80
86
 
81
87
  # Returns true if this node has a parent, and is not a root.
@@ -210,8 +216,6 @@ module ClosureTree #:nodoc:
210
216
  # Mixed into both classes and instances to provide easy access to the column names
211
217
  module Columns
212
218
 
213
- protected
214
-
215
219
  def parent_column_name
216
220
  closure_tree_options[:parent_column_name]
217
221
  end
@@ -1,3 +1,3 @@
1
1
  module ClosureTree
2
- VERSION = "1.0.0.beta3" unless defined?(::ClosureTree::VERSION)
2
+ VERSION = "1.0.0.beta5" unless defined?(::ClosureTree::VERSION)
3
3
  end
@@ -100,5 +100,19 @@ class TagTest < ActiveSupport::TestCase
100
100
  assert_equal [tags(:california), tags(:united_states), tags(:places)], city.ancestors
101
101
  assert_equal [city, tags(:california), tags(:united_states), tags(:places)], city.self_and_ancestors
102
102
  end
103
+
104
+ def test_root
105
+ assert_equal tags(:grandparent), tags(:grandparent).root
106
+ assert_equal tags(:grandparent), tags(:parent).root
107
+ assert_equal tags(:grandparent), tags(:child).root
108
+ 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
103
117
  end
104
118
 
metadata CHANGED
@@ -1,38 +1,35 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: closure_tree
3
- version: !ruby/object:Gem::Version
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0.beta5
4
5
  prerelease: 6
5
- version: 1.0.0.beta3
6
6
  platform: ruby
7
7
  authors: []
8
-
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
-
13
- date: 2011-05-26 00:00:00 -07:00
11
+ date: 2011-05-29 00:00:00.000000000 -07:00
14
12
  default_executable:
15
- dependencies:
16
- - !ruby/object:Gem::Dependency
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
17
15
  name: activerecord
18
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &2160266260 !ruby/object:Gem::Requirement
19
17
  none: false
20
- requirements:
21
- - - ">="
22
- - !ruby/object:Gem::Version
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
23
21
  version: 3.0.0
24
22
  type: :runtime
25
23
  prerelease: false
26
- version_requirements: *id001
27
- description: " A mostly-API-compatible replacement for the acts_as_tree and awesome_nested_set gems,\n but with much better mutation performance thanks to the Closure Tree storage algorithm\n"
24
+ version_requirements: *2160266260
25
+ description: ! " A mostly-API-compatible replacement for the acts_as_tree and awesome_nested_set
26
+ gems,\n but with much better mutation performance thanks to the Closure Tree storage
27
+ algorithm\n"
28
28
  email:
29
29
  executables: []
30
-
31
30
  extensions: []
32
-
33
31
  extra_rdoc_files: []
34
-
35
- files:
32
+ files:
36
33
  - lib/closure_tree/acts_as_tree.rb
37
34
  - lib/closure_tree/version.rb
38
35
  - lib/closure_tree.rb
@@ -62,35 +59,32 @@ files:
62
59
  has_rdoc: true
63
60
  homepage:
64
61
  licenses: []
65
-
66
62
  post_install_message:
67
63
  rdoc_options: []
68
-
69
- require_paths:
64
+ require_paths:
70
65
  - lib
71
- required_ruby_version: !ruby/object:Gem::Requirement
66
+ required_ruby_version: !ruby/object:Gem::Requirement
72
67
  none: false
73
- requirements:
74
- - - ">="
75
- - !ruby/object:Gem::Version
76
- hash: 2257595380188246970
77
- segments:
68
+ requirements:
69
+ - - ! '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ segments:
78
73
  - 0
79
- version: "0"
80
- required_rubygems_version: !ruby/object:Gem::Requirement
74
+ hash: 1822137668731567771
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
76
  none: false
82
- requirements:
83
- - - ">"
84
- - !ruby/object:Gem::Version
77
+ requirements:
78
+ - - ! '>'
79
+ - !ruby/object:Gem::Version
85
80
  version: 1.3.1
86
81
  requirements: []
87
-
88
82
  rubyforge_project:
89
83
  rubygems_version: 1.6.2
90
84
  signing_key:
91
85
  specification_version: 3
92
86
  summary: Hierarchies for ActiveRecord models using a Closure Tree storage algorithm
93
- test_files:
87
+ test_files:
94
88
  - test/dummy/Rakefile
95
89
  - test/dummy/app/models/.gitkeep
96
90
  - test/dummy/app/models/tag.rb