Sha256: c2a7a534416369dd51242265742061baa908f347349db45b8994e2a74cdfc76c

Contents?: true

Size: 1.32 KB

Versions: 7

Compression:

Stored size: 1.32 KB

Contents

module Ecoportal
  module API
    class GraphQL
      module Helpers
        module LocationsTree
          RELEVANT_NODES_COUNT = 1000

          def treeify(nodes)
            parents = nodes.each_with_object({}) do |node, out|
              (out[node.parent_id_upcase] ||= []).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|
              class_types = child.classifications || []
              class_ids   = class_types.map(&:id)
              class_names = class_types.map(&:name)

              results << {
                "id"                   => child.id,
                "name"                 => child.name,
                "weight"               => child.weight,
                "parent_id"            => node_id,
                "archived"             => child.archived,
                "archived_token"       => child.archivedToken,
                "classifications"      => class_ids,
                "classification_names" => class_names,
                "nodes"                => get_children(child.id, parents).compact
              }
            end
          end
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
ecoportal-api-graphql-0.4.3 lib/ecoportal/api/graphql/helpers/locations_tree.rb
ecoportal-api-graphql-0.4.2 lib/ecoportal/api/graphql/helpers/locations_tree.rb
ecoportal-api-graphql-0.4.1 lib/ecoportal/api/graphql/helpers/locations_tree.rb
ecoportal-api-graphql-0.4.0 lib/ecoportal/api/graphql/helpers/locations_tree.rb
ecoportal-api-graphql-0.3.16 lib/ecoportal/api/graphql/helpers/locations_tree.rb
ecoportal-api-graphql-0.3.15 lib/ecoportal/api/graphql/helpers/locations_tree.rb
ecoportal-api-graphql-0.3.14 lib/ecoportal/api/graphql/helpers/locations_tree.rb