Sha256: 059a031d5146f4560f71b755c59189e2ee851bb7f6b12d5614799e3aff803cf6

Contents?: true

Size: 1002 Bytes

Versions: 3

Compression:

Stored size: 1002 Bytes

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
              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&.upcase] ||= []).each_with_object([]) do |child, results|
              results << {
                "id"       => child.id,
                "name"     => child.name,
                "weight"   => child.weight,
                "archived" => child.archived,
                "archived_token" => child.archivedToken,
                "nodes"    => get_children(child.id, parents).compact
              }
            end
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ecoportal-api-graphql-0.3.7 lib/ecoportal/api/graphql/helpers/locations_tree.rb
ecoportal-api-graphql-0.3.6 lib/ecoportal/api/graphql/helpers/locations_tree.rb
ecoportal-api-graphql-0.3.5 lib/ecoportal/api/graphql/helpers/locations_tree.rb