Sha256: 2c2d41a26e5812d7ccb526d8b1a2780358e70e78a7d1a826f4d247117f30b94c

Contents?: true

Size: 1.06 KB

Versions: 4

Compression:

Stored size: 1.06 KB

Contents

module Ecoportal
  module API
    class GraphQL
      module Helpers
        module LocationsTree
          def treeify(nodes)
            parents = nodes.each_with_object({}) do |node, parents|
              parent   = node.parent
              # puts "Node '#{node.name}' with ID: '#{node.id}'. "
              # puts "  • Parent '#{parent.name}' with ID: '#{parent.id}'." if parent
              parentId = parent && parent.id.upcase
              (parents[parentId] ||= []).push(node)
            end
            get_children(nil, parents)
          end

          private

          def get_children(node_id, parents)
            (parents[node_id] ||= []).each_with_object([]) do |child, results|
              results << {
                "id"    => child.id,
                "name"  => child.name,
                "weight" => child.weight,
                "archived" => child.archived,
                "nodes" => get_children(child.id, parents).compact
              }
            end
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ecoportal-api-graphql-0.3.1 lib/ecoportal/api/graphql/helpers/locations_tree.rb
ecoportal-api-graphql-0.2.3 lib/ecoportal/api/graphql/helpers/locations_tree.rb
ecoportal-api-graphql-0.2.2 lib/ecoportal/api/graphql/helpers/locations_tree.rb
ecoportal-api-graphql-0.2.1 lib/ecoportal/api/graphql/helpers/locations_tree.rb