Sha256: dd171c9c2b63e3d33878972a1a28ba80f11960083562636640c65224e0cd3e03

Contents?: true

Size: 1.29 KB

Versions: 6

Compression:

Stored size: 1.29 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
              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|
              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

6 entries across 6 versions & 1 rubygems

Version Path
ecoportal-api-graphql-0.3.13 lib/ecoportal/api/graphql/helpers/locations_tree.rb
ecoportal-api-graphql-0.3.12 lib/ecoportal/api/graphql/helpers/locations_tree.rb
ecoportal-api-graphql-0.3.11 lib/ecoportal/api/graphql/helpers/locations_tree.rb
ecoportal-api-graphql-0.3.10 lib/ecoportal/api/graphql/helpers/locations_tree.rb
ecoportal-api-graphql-0.3.9 lib/ecoportal/api/graphql/helpers/locations_tree.rb
ecoportal-api-graphql-0.3.8 lib/ecoportal/api/graphql/helpers/locations_tree.rb