closure_tree 3.6.2 → 3.6.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,16 +1,16 @@
1
- # Closure Tree
1
+ # Closure Tree [![Build Status](https://secure.travis-ci.org/mceachen/closure_tree.png?branch=master)](http://travis-ci.org/mceachen/closure_tree)
2
2
 
3
3
  Closure Tree is a mostly-API-compatible replacement for the
4
4
  [ancestry](https://github.com/stefankroes/ancestry),
5
5
  [acts_as_tree](https://github.com/amerine/acts_as_tree) and
6
6
  [awesome_nested_set](https://github.com/collectiveidea/awesome_nested_set/) gems, giving you:
7
7
 
8
- * Best-in-class select performance:
8
+ * __Best-in-class select performance__:
9
9
  * Fetch your whole ancestor lineage in 1 SELECT.
10
10
  * Grab all your descendants: 1 SELECT.
11
11
  * Get all your siblings: 1 SELECT.
12
12
  * Fetch all [7-degrees-of-bacon in a nested hash](#nested-hashes): 1 SELECT.
13
- * Best-in-class mutation performance:
13
+ * __Best-in-class mutation performance__:
14
14
  * 2 SQL INSERTs on node creation
15
15
  * 3 SQL INSERT/UPDATEs on node reparenting
16
16
  * Support for reparenting children (and all their progeny)
@@ -357,10 +357,16 @@ Closure tree is [tested under every combination](http://travis-ci.org/#!/mceache
357
357
 
358
358
  * Ruby 1.8.7 and Ruby 1.9.3
359
359
  * The latest Rails 3.0, 3.1, and 3.2 branches, and
360
- * MySQL, PostgreSQL, and SQLite.
360
+ * MySQL, PostgreSQL, & SQLite.
361
361
 
362
362
  ## Change log
363
363
 
364
+
365
+ ### 3.6.3
366
+
367
+ * Fixed [issue 24](https://github.com/mceachen/closure_tree/issues/24), which optimized ```#hash_tree```
368
+ for roots. Thanks, [Saverio Trioni](https://github.com/rewritten)!
369
+
364
370
  ### 3.6.2
365
371
 
366
372
  * Fixed [issue 23](https://github.com/mceachen/closure_tree/issues/23), which added support for ```#siblings```
@@ -86,8 +86,34 @@ module ClosureTree
86
86
  where(parent_column_name => nil)
87
87
  end
88
88
 
89
+ # Note that options[:limit_depth] defaults to 10. This might be crazy-big, depending on your tree shape.
89
90
  def self.hash_tree(options = {})
90
- roots.inject(ActiveSupport::OrderedHash.new) { |h, ea| h.merge(ea.hash_tree(options)) }
91
+ tree = ActiveSupport::OrderedHash.new
92
+ id_to_hash = {}
93
+ limit_depth = (options[:limit_depth] || 10).to_i
94
+ scope = joins(<<-SQL)
95
+ INNER JOIN (
96
+ SELECT #{primary_key} as root_id
97
+ FROM #{quoted_table_name}
98
+ WHERE #{quoted_parent_column_name} IS NULL
99
+ ) AS roots ON (1 = 1)
100
+ INNER JOIN (
101
+ SELECT ancestor_id, descendant_id, MAX(generations) as max_gen
102
+ FROM #{quoted_hierarchy_table_name}
103
+ GROUP BY 1, 2
104
+ HAVING MAX(generations) <= #{limit_depth - 1}
105
+ ) AS nodes ON (roots.root_id = nodes.ancestor_id) AND
106
+ #{quoted_table_name}.#{primary_key} = nodes.descendant_id
107
+ SQL
108
+ scope.order(append_order("max_gen")).each do |ea|
109
+ h = id_to_hash[ea.id] = ActiveSupport::OrderedHash.new
110
+ if ea.root?
111
+ tree[ea] = h
112
+ else
113
+ id_to_hash[ea.ct_parent_id][ea] = h
114
+ end
115
+ end
116
+ tree
91
117
  end
92
118
 
93
119
  def find_all_by_generation(generation_level)
@@ -257,6 +283,10 @@ module ClosureTree
257
283
  tree
258
284
  end
259
285
 
286
+ def ct_parent_id
287
+ read_attribute(parent_column_sym)
288
+ end
289
+
260
290
  protected
261
291
 
262
292
  def ct_validate
@@ -325,9 +355,6 @@ module ClosureTree
325
355
  scope.select(:id).collect(&:id)
326
356
  end
327
357
 
328
- def ct_parent_id
329
- read_attribute(parent_column_sym)
330
- end
331
358
 
332
359
  # TODO: _parent_id will be removed in the next major version
333
360
  alias :_parent_id :ct_parent_id
@@ -1,3 +1,3 @@
1
1
  module ClosureTree
2
- VERSION = "3.6.2" unless defined?(::ClosureTree::VERSION)
2
+ VERSION = "3.6.3" unless defined?(::ClosureTree::VERSION)
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: closure_tree
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.6.2
4
+ version: 3.6.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-22 00:00:00.000000000 Z
12
+ date: 2012-09-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
@@ -192,7 +192,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
192
192
  version: '0'
193
193
  segments:
194
194
  - 0
195
- hash: -956461018663594337
195
+ hash: -1870454341396568089
196
196
  required_rubygems_version: !ruby/object:Gem::Requirement
197
197
  none: false
198
198
  requirements:
@@ -201,7 +201,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
201
201
  version: '0'
202
202
  segments:
203
203
  - 0
204
- hash: -956461018663594337
204
+ hash: -1870454341396568089
205
205
  requirements: []
206
206
  rubyforge_project:
207
207
  rubygems_version: 1.8.23