Class: ForestAdminAgent::Routes::Resources::Store
Instance Method Summary
collapse
#build, #format_attributes
#add_route, #build, #initialize, #routes
Instance Method Details
#handle_request(args = {}) ⇒ Object
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/forest_admin_agent/routes/resources/store.rb', line 16
def handle_request(args = {})
context = build(args)
context.permissions.can?(:add, context.collection)
data = format_attributes(args, context.collection)
record = context.collection.create(context.caller, data)
link_one_to_one_relations(args, record, context)
id = ForestAdminDatasourceToolkit::Utils::Record.primary_keys(context.collection, record)
filter = ForestAdminDatasourceToolkit::Components::Query::Filter.new(
condition_tree: ConditionTree::ConditionTreeFactory.match_ids(context.collection, [id])
)
records = context.collection.list(context.caller, filter, ProjectionFactory.all(context.collection))
{
name: args[:params]['collection_name'],
content: JSONAPI::Serializer.serialize(
records[0],
is_collection: false,
class_name: context.collection.name,
serializer: Serializer::ForestSerializer
)
}
end
|
#link_one_to_one_relations(args, record, context) ⇒ Object
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/forest_admin_agent/routes/resources/store.rb', line 39
def link_one_to_one_relations(args, record, context)
args[:params][:data][:relationships]&.map do |field, value|
schema = context.collection.schema[:fields][field]
next unless %w[OneToOne PolymorphicOneToOne].include?(schema.type)
primary_key_values = Utils::Id.unpack_id(context.collection, value['data']['id'], with_key: true)
foreign_collection = context.datasource.get_collection(schema.foreign_collection)
origin_value = record[schema.origin_key_target]
patch = { schema.origin_key => origin_value }
if schema.type == 'PolymorphicOneToOne'
patch[schema.origin_type_field] =
context.collection.name.gsub('__', '::')
end
condition_tree = ConditionTree::ConditionTreeFactory.match_records(foreign_collection, [primary_key_values])
filter = Filter.new(condition_tree: condition_tree)
foreign_collection.update(context.caller, filter, patch)
end
end
|
#setup_routes ⇒ Object
10
11
12
13
14
|
# File 'lib/forest_admin_agent/routes/resources/store.rb', line 10
def setup_routes
add_route('forest_store', 'post', '/:collection_name', ->(args) { handle_request(args) })
self
end
|