Sha256: bf69398d439bb047ae00854467e7d33d7048dda497158fa52ff724b340f801f4

Contents?: true

Size: 1.25 KB

Versions: 3

Compression:

Stored size: 1.25 KB

Contents

module ForestAdminDatasourceToolkit
  module Components
    module Query
      class Projection < Array
        include ForestAdminDatasourceToolkit::Utils
        def with_pks(collection)
          ForestAdminDatasourceToolkit::Utils::Schema.primary_keys(collection).each do |key|
            push(key) unless include?(key)
          end

          relations.each do |relation, projection|
            schema = collection.fields[relation]
            association = collection.datasource.collection(schema.foreign_collection)
            projection_with_pks = projection.with_pks(association).nest(prefix: relation)

            projection_with_pks.each { |field| push(field) unless include?(field) }
          end

          self
        end

        def columns
          reject { |field| field.include?(':') }
        end

        def relations
          each_with_object({}) do |path, memo|
            next unless path.include?(':')

            split_path = path.split(':')
            relation = split_path[0]
            memo[relation] = Projection.new([split_path[1]].union(memo[relation] || []))
          end
        end

        def nest(prefix: nil)
          prefix ? Projection.new(map { |path| "#{prefix}:#{path}" }) : self
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
forest_admin_datasource_toolkit-1.0.0.pre.beta.23 lib/forest_admin_datasource_toolkit/components/query/projection.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.22 lib/forest_admin_datasource_toolkit/components/query/projection.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.21 lib/forest_admin_datasource_toolkit/components/query/projection.rb