Class: ForestAdminAgent::Routes::Resources::Related::ListRelated

Inherits:
AbstractRelatedRoute show all
Includes:
Builder, ForestAdminDatasourceToolkit::Components::Query::ConditionTree, ForestAdminDatasourceToolkit::Utils
Defined in:
lib/forest_admin_agent/routes/resources/related/list_related.rb

Instance Method Summary collapse

Methods inherited from AbstractRelatedRoute

#build

Methods inherited from AbstractAuthenticatedRoute

#build, #format_attributes

Methods inherited from AbstractRoute

#add_route, #build, #initialize, #routes

Constructor Details

This class inherits a constructor from ForestAdminAgent::Routes::AbstractRoute

Instance Method Details

#handle_request(args = {}) ⇒ Object



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
52
53
54
55
56
57
58
59
# File 'lib/forest_admin_agent/routes/resources/related/list_related.rb', line 23

def handle_request(args = {})
  context = build(args)
  context.permissions.can?(:browse, context.collection)

  filter = ForestAdminDatasourceToolkit::Components::Query::Filter.new(
    condition_tree: ConditionTreeFactory.intersect(
      [
        context.permissions.get_scope(context.collection),
        ForestAdminAgent::Utils::QueryStringParser.parse_condition_tree(context.child_collection, args)
      ]
    ),
    page: ForestAdminAgent::Utils::QueryStringParser.parse_pagination(args),
    sort: ForestAdminAgent::Utils::QueryStringParser.parse_sort(context.child_collection, args)
  )
  projection = ForestAdminAgent::Utils::QueryStringParser.parse_projection_with_pks(context.child_collection,
                                                                                    args)
  primary_key_values = Utils::Id.unpack_id(context.collection, args[:params]['id'], with_key: true)
  records = Collection.list_relation(
    context.collection,
    primary_key_values,
    args[:params]['relation_name'],
    context.caller,
    filter,
    projection
  )

  {
    name: context.child_collection.name,
    content: JSONAPI::Serializer.serialize(
      records,
      is_collection: true,
      class_name: context.child_collection.name,
      serializer: Serializer::ForestSerializer,
      include: projection.relations.keys
    )
  }
end

#setup_routesObject



12
13
14
15
16
17
18
19
20
21
# File 'lib/forest_admin_agent/routes/resources/related/list_related.rb', line 12

def setup_routes
  add_route(
    'forest_related_list',
    'get',
    '/:collection_name/:id/relationships/:relation_name',
    ->(args) { handle_request(args) }
  )

  self
end