closure_tree 3.6.7 → 3.6.8

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.
data/README.md CHANGED
@@ -367,6 +367,11 @@ Closure tree is [tested under every combination](http://travis-ci.org/#!/mceache
367
367
 
368
368
  ## Change log
369
369
 
370
+ ### 3.6.8
371
+
372
+ * [Don Morrison](https://github.com/elskwid) massaged the [#hash_tree](#nested-hashes) query to
373
+ be more efficient.
374
+
370
375
  ### 3.6.7
371
376
 
372
377
  * Added workaround for ActiveRecord::Observer usage pre-db-creation. Addresses
@@ -93,21 +93,26 @@ module ClosureTree
93
93
  tree = ActiveSupport::OrderedHash.new
94
94
  id_to_hash = {}
95
95
  limit_depth = (options[:limit_depth] || 10).to_i
96
- scope = joins(<<-SQL)
97
- INNER JOIN (
98
- SELECT #{primary_key} as root_id
99
- FROM #{quoted_table_name}
100
- WHERE #{quoted_parent_column_name} IS NULL
101
- ) AS roots ON (1 = 1)
96
+
97
+ # Simple join with hierarchy for ancestor, descendant, and generation
98
+ scope = joins(:ancestor_hierarchies)
99
+
100
+ # Deepest generation, within limit, for each descendant
101
+ scope = scope.joins(<<-SQL)
102
102
  INNER JOIN (
103
- SELECT ancestor_id, descendant_id, MAX(generations) as max_gen
103
+ SELECT
104
+ #{quoted_hierarchy_table_name}.descendant_id,
105
+ MAX(#{quoted_hierarchy_table_name}.generations) AS depth
104
106
  FROM #{quoted_hierarchy_table_name}
105
- GROUP BY 1, 2
106
- HAVING MAX(generations) <= #{limit_depth - 1}
107
- ) AS nodes ON (roots.root_id = nodes.ancestor_id) AND
108
- #{quoted_table_name}.#{primary_key} = nodes.descendant_id
107
+ GROUP BY #{quoted_hierarchy_table_name}.descendant_id
108
+ HAVING MAX(#{quoted_hierarchy_table_name}.generations) <= #{limit_depth - 1}
109
+ ) AS generation_depth
110
+ ON #{quoted_hierarchy_table_name}.descendant_id = generation_depth.descendant_id
109
111
  SQL
110
- scope.order(append_order("max_gen")).each do |ea|
112
+
113
+ scope = scope.order(append_order("generation_depth.depth"))
114
+
115
+ scope.each do |ea|
111
116
  h = id_to_hash[ea.id] = ActiveSupport::OrderedHash.new
112
117
  if ea.root?
113
118
  tree[ea] = h
@@ -1,3 +1,3 @@
1
1
  module ClosureTree
2
- VERSION = "3.6.7" unless defined?(::ClosureTree::VERSION)
2
+ VERSION = "3.6.8" 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.7
4
+ version: 3.6.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -208,7 +208,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
208
208
  version: '0'
209
209
  segments:
210
210
  - 0
211
- hash: -486781145760134167
211
+ hash: 1831255790364540871
212
212
  required_rubygems_version: !ruby/object:Gem::Requirement
213
213
  none: false
214
214
  requirements:
@@ -217,7 +217,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
217
217
  version: '0'
218
218
  segments:
219
219
  - 0
220
- hash: -486781145760134167
220
+ hash: 1831255790364540871
221
221
  requirements: []
222
222
  rubyforge_project:
223
223
  rubygems_version: 1.8.23