Sha256: 96f82c77fb4a6e31f12a9893b5f9debd03ef6fd0acccb6bcefbd8220eff8fd22

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 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,
                "archived_token" => child.archivedToken,
                "nodes"    => get_children(child.id, parents).compact
              }
            end
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ecoportal-api-graphql-0.3.3 lib/ecoportal/api/graphql/helpers/locations_tree.rb