Class: ForestAdminDatasourceMongoid::Collection
Constant Summary
Parser::Column::TYPES
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#_create(_caller, flat_data) ⇒ Object
-
#_delete(_caller, filter) ⇒ Object
-
#_update(_caller, filter, flat_patch) ⇒ Object
-
#aggregate(_caller, filter, aggregation, limit = nil) ⇒ Object
-
#create(caller, data) ⇒ Object
-
#delete(caller, filter) ⇒ Object
-
#initialize(datasource, model, stack) ⇒ Collection
constructor
A new instance of Collection.
-
#list(_caller, filter, projection) ⇒ Object
-
#update(caller, filter, data) ⇒ Object
#compare_ids, #deep_merge, #escape, #group_ids_by_path, #recursive_delete, #recursive_set, #reformat_patch, #replace_mongo_types, #split_id, #unflatten_record, #unnest
#add_null_values, #add_null_values_on_record, #remove_not_exist_record
#get_validations, #parse_format_validator, #parse_length_validator, #parse_numericality_validator
#get_polymorphic_types
#get_column_type, #get_default_value, #get_embedded_fields, #operators_for_column_type
Constructor Details
#initialize(datasource, model, stack) ⇒ Collection
15
16
17
18
19
20
21
22
23
24
25
26
|
# File 'lib/forest_admin_datasource_mongoid/collection.rb', line 15
def initialize(datasource, model, stack)
prefix = stack[stack.length - 1][:prefix]
@model = model
@stack = stack
model_name = format_model_name(@model.name)
name = escape(prefix ? "#{model_name}.#{prefix}" : model_name)
super(datasource, name)
add_fields(FieldsGenerator.build_fields_schema(model, stack))
enable_count
end
|
Instance Attribute Details
#model ⇒ Object
Returns the value of attribute model.
13
14
15
|
# File 'lib/forest_admin_datasource_mongoid/collection.rb', line 13
def model
@model
end
|
#stack ⇒ Object
Returns the value of attribute stack.
13
14
15
|
# File 'lib/forest_admin_datasource_mongoid/collection.rb', line 13
def stack
@stack
end
|
Instance Method Details
#_create(_caller, flat_data) ⇒ Object
51
52
53
54
55
56
57
|
# File 'lib/forest_admin_datasource_mongoid/collection.rb', line 51
def _create(_caller, flat_data)
as_fields = @stack[stack.length - 1][:as_fields]
data = unflatten_record(flat_data, as_fields)
inserted_record = @model.create(data)
{ '_id' => inserted_record.attributes['_id'], **flat_data }
end
|
#_delete(_caller, filter) ⇒ Object
82
83
84
85
86
87
|
# File 'lib/forest_admin_datasource_mongoid/collection.rb', line 82
def _delete(_caller, filter)
records = list(nil, filter, Projection.new(['_id']))
ids = records.map { |record| record['_id'] }
@model.where(_id: ids).delete_all
end
|
#_update(_caller, filter, flat_patch) ⇒ Object
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/forest_admin_datasource_mongoid/collection.rb', line 63
def _update(_caller, filter, flat_patch)
as_fields = @stack[stack.length - 1][:as_fields]
patch = unflatten_record(flat_patch, as_fields, patch_mode: true)
formatted_patch = reformat_patch(patch)
records = list(nil, filter, Projection.new(['_id']))
ids = records.map { |record| record['_id'] }
if ids.length > 1
@model.where(_id: ids).update_all(formatted_patch)
else
@model.find(ids.first).update(formatted_patch)
end
end
|
#aggregate(_caller, filter, aggregation, limit = nil) ⇒ Object
34
35
36
37
38
39
40
41
42
43
44
45
|
# File 'lib/forest_admin_datasource_mongoid/collection.rb', line 34
def aggregate(_caller, filter, aggregation, limit = nil)
lookup_projection = aggregation.projection.union(filter.condition_tree&.projection || [])
pipeline = [
*build_base_pipeline(filter, lookup_projection),
*GroupGenerator.group(aggregation),
{ '$sort' => { value: -1 } }
]
pipeline << { '$limit' => limit } if limit
rows = model.unscoped.collection.aggregate(pipeline).to_a
replace_mongo_types(rows)
end
|
#create(caller, data) ⇒ Object
47
48
49
|
# File 'lib/forest_admin_datasource_mongoid/collection.rb', line 47
def create(caller, data)
handle_validation_error { _create(caller, data) }
end
|
#delete(caller, filter) ⇒ Object
78
79
80
|
# File 'lib/forest_admin_datasource_mongoid/collection.rb', line 78
def delete(caller, filter)
handle_validation_error { _delete(caller, filter) }
end
|
#list(_caller, filter, projection) ⇒ Object
28
29
30
31
32
|
# File 'lib/forest_admin_datasource_mongoid/collection.rb', line 28
def list(_caller, filter, projection)
projection = projection.union(filter.condition_tree&.projection || [], filter.sort&.projection || [])
pipeline = [*build_base_pipeline(filter, projection), *ProjectionGenerator.project(projection)]
add_null_values(replace_mongo_types(model.unscoped.collection.aggregate(pipeline).to_a), projection)
end
|
#update(caller, filter, data) ⇒ Object
59
60
61
|
# File 'lib/forest_admin_datasource_mongoid/collection.rb', line 59
def update(caller, filter, data)
handle_validation_error { _update(caller, filter, data) }
end
|