Class: ForestAdminDatasourceMongoid::Utils::Pipeline::ConditionGenerator

Inherits:
Object
  • Object
show all
Includes:
Schema
Defined in:
lib/forest_admin_datasource_mongoid/utils/pipeline/condition_generator.rb

Constant Summary collapse

FOREST_RECORD_DOES_NOT_EXIST =
'FOREST_RECORD_DOES_NOT_EXIST'.freeze

Class Method Summary collapse

Class Method Details

.if_missing(field, then_expr, else_expr) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/forest_admin_datasource_mongoid/utils/pipeline/condition_generator.rb', line 17

def self.if_missing(field, then_expr, else_expr)
  {
    '$cond' => {
      'if' => { '$and' => [{ '$ne' => [{ '$type' => "$#{field}" }, 'missing'] },
                           { '$ne' => ["$#{field}", nil] }] },
      'then' => then_expr,
      'else' => else_expr
    }
  }
end

.tag_record_if_not_exist(field, then_expr) ⇒ Object



9
10
11
# File 'lib/forest_admin_datasource_mongoid/utils/pipeline/condition_generator.rb', line 9

def self.tag_record_if_not_exist(field, then_expr)
  if_missing(field, then_expr, { FOREST_RECORD_DOES_NOT_EXIST => true })
end

.tag_record_if_not_exist_by_value(field, then_expr) ⇒ Object



13
14
15
# File 'lib/forest_admin_datasource_mongoid/utils/pipeline/condition_generator.rb', line 13

def self.tag_record_if_not_exist_by_value(field, then_expr)
  if_missing(field, then_expr, FOREST_RECORD_DOES_NOT_EXIST)
end