Class: ForestAdminDatasourceMongoid::Utils::Pipeline::ProjectionGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/forest_admin_datasource_mongoid/utils/pipeline/projection_generator.rb

Class Method Summary collapse

Class Method Details

.project(projection) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/forest_admin_datasource_mongoid/utils/pipeline/projection_generator.rb', line 5

def self.project(projection)
  return [{ '$replaceRoot' => { 'newRoot' => { '$literal' => {} } } }] if projection.empty?

  project = { '_id' => false, 'FOREST_RECORD_DOES_NOT_EXIST' => true }

  projection.each do |field|
    formatted_field = field.tr(':', '.')
    project[formatted_field] = true
  end

  [{ '$project' => project }]
end