Module: ClosureTree::HashTreeSupport
- Included in:
- Support
- Defined in:
- lib/closure_tree/hash_tree_support.rb
Instance Method Summary collapse
-
#build_hash_tree(tree_scope) ⇒ Object
Builds nested hash structure using the scope returned from the passed in scope.
- #default_tree_scope(scope, limit_depth = nil) ⇒ Object
- #hash_tree(tree_scope, limit_depth = nil) ⇒ Object
Instance Method Details
#build_hash_tree(tree_scope) ⇒ Object
Builds nested hash structure using the scope returned from the passed in scope
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/closure_tree/hash_tree_support.rb', line 25 def build_hash_tree(tree_scope) tree = ActiveSupport::OrderedHash.new id_to_hash = {} tree_scope.each do |ea| h = id_to_hash[ea.id] = ActiveSupport::OrderedHash.new (id_to_hash[ea._ct_parent_id] || tree)[ea] = h end tree end |
#default_tree_scope(scope, limit_depth = nil) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/closure_tree/hash_tree_support.rb', line 3 def default_tree_scope(scope, limit_depth = nil) # Deepest generation, within limit, for each descendant # NOTE: Postgres requires HAVING clauses to always contains aggregate functions (!!) having_clause = limit_depth ? "HAVING MAX(generations) <= #{limit_depth - 1}" : '' generation_depth = <<-SQL.squish INNER JOIN ( SELECT descendant_id, MAX(generations) as depth FROM #{quoted_hierarchy_table_name} GROUP BY descendant_id #{having_clause} ) #{ t_alias_keyword } generation_depth ON #{quoted_table_name}.#{model_class.primary_key} = generation_depth.descendant_id SQL scope_with_order(scope.joins(generation_depth), 'generation_depth.depth') end |
#hash_tree(tree_scope, limit_depth = nil) ⇒ Object
19 20 21 22 |
# File 'lib/closure_tree/hash_tree_support.rb', line 19 def hash_tree(tree_scope, limit_depth = nil) limited_scope = limit_depth ? tree_scope.where("#{quoted_hierarchy_table_name}.generations <= #{limit_depth - 1}") : tree_scope build_hash_tree(limited_scope) end |