Module: ForestAdminDatasourceMongoid::Utils::AddNullValues
- Included in:
- Collection
- Defined in:
- lib/forest_admin_datasource_mongoid/utils/add_null_values.rb
Instance Method Summary collapse
- #add_null_values(records, projection) ⇒ Object
- #add_null_values_on_record(record, projection) ⇒ Object
-
#remove_not_exist_record(record) ⇒ Object
Filter out records that have been tagged as not existing If the key FOREST_RECORD_DOES_NOT_EXIST is present in the record, the record is removed If a nested object has a key with FOREST_RECORD_DOES_NOT_EXIST, the nested object is removed.
Instance Method Details
#add_null_values(records, projection) ⇒ Object
53 54 55 |
# File 'lib/forest_admin_datasource_mongoid/utils/add_null_values.rb', line 53 def add_null_values(records, projection) records.filter_map { |record| add_null_values_on_record(record, projection) } end |
#add_null_values_on_record(record, projection) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/forest_admin_datasource_mongoid/utils/add_null_values.rb', line 21 def add_null_values_on_record(record, projection) return nil if record.nil? result = record.dup projection.each do |field| field_prefix = field.split(':').first result[field_prefix] ||= nil end nested_prefixes = projection.filter_map { |field| field.split(':').first if field.include?(':') }.uniq nested_prefixes.each do |nested_prefix| prefix_with_colon = "#{nested_prefix}:" child_paths = projection.filter_map do |field| field[(nested_prefix.size + 1)..] if field.start_with?(prefix_with_colon) end next unless result[nested_prefix] && !result[nested_prefix].nil? if result[nested_prefix].is_a?(Array) result[nested_prefix] = result[nested_prefix].map do |child_record| add_null_values_on_record(child_record, child_paths) end elsif result[nested_prefix].is_a?(Hash) result[nested_prefix] = add_null_values_on_record(result[nested_prefix], child_paths) end end remove_not_exist_record(result) end |
#remove_not_exist_record(record) ⇒ Object
Filter out records that have been tagged as not existing If the key FOREST_RECORD_DOES_NOT_EXIST is present in the record, the record is removed If a nested object has a key with FOREST_RECORD_DOES_NOT_EXIST, the nested object is removed
7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/forest_admin_datasource_mongoid/utils/add_null_values.rb', line 7 def remove_not_exist_record(record) return nil if record.nil? || record[Pipeline::ConditionGenerator::FOREST_RECORD_DOES_NOT_EXIST] record.transform_values! do |value| if value.is_a?(Hash) && value[Pipeline::ConditionGenerator::FOREST_RECORD_DOES_NOT_EXIST] nil else value end end record end |