Class: ForestAdminAgent::Routes::Resources::List

Inherits:
AbstractAuthenticatedRoute show all
Includes:
QueryHandler, Utils, ForestAdminDatasourceToolkit::Components::Query::ConditionTree
Defined in:
lib/forest_admin_agent/routes/resources/list.rb

Instance Method Summary collapse

Methods included from QueryHandler

#execute_query, #inject_context_variables, #parse_query_segment

Methods inherited from AbstractAuthenticatedRoute

#build, #format_attributes, #redacted_full_projection, #redacted_projection_with_pks

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



17
18
19
20
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
52
53
54
55
56
57
58
59
# File 'lib/forest_admin_agent/routes/resources/list.rb', line 17

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

  condition_tree = QueryStringParser.parse_condition_tree(context.collection, args)
  search = QueryStringParser.parse_search(context.collection, args)
  search_extended = QueryStringParser.parse_search_extended(args)
  sort = QueryStringParser.parse_sort(context.collection, args)
  context.permissions.assert_can_read_query_fields(
    context.collection,
    condition_tree: condition_tree, sort: sort, search: search, search_extended: search_extended
  )

  filter = ForestAdminDatasourceToolkit::Components::Query::Filter.new(
    condition_tree: ConditionTreeFactory.intersect(
      [
        context.permissions.get_scope(context.collection),
        condition_tree,
        parse_query_segment(context.collection, args, context.permissions, context.caller)
      ]
    ),
    page: QueryStringParser.parse_pagination(args),
    search: search,
    search_extended: search_extended,
    sort: sort,
    segment: QueryStringParser.parse_segment(context.collection, args)
  )

  projection = redacted_projection_with_pks(context, context.collection, args)
  records = context.collection.list(context.caller, filter, projection)

  {
    name: args[:params]['collection_name'],
    content: JSONAPI::Serializer.serialize(
      records,
      class_name: context.collection.name,
      is_collection: true,
      serializer: Serializer::ForestSerializer,
      include: projection.relation_include_paths,
      meta: handle_search_decorator(args[:params]['search'], records, context.collection)
    )
  }
end

#handle_search_decorator(search_value, records, collection) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/forest_admin_agent/routes/resources/list.rb', line 61

def handle_search_decorator(search_value, records, collection)
  decorator = { decorators: [] }
  unless search_value.nil?
    records.each_with_index do |entry, index|
      decorator[:decorators][index] = { id: Utils::Id.pack_id(collection, entry), search: [] }
      # attributes method is defined on ActiveRecord::Base model
      attributes = entry.respond_to?(:attributes) ? entry.attributes : entry

      attributes.each do |field_key, field_value|
        if !field_value.is_a?(Array) && field_value.to_s.downcase.include?(search_value.downcase)
          decorator[:decorators][index][:search] << field_key
        end
      end
    end
  end

  decorator
end

#setup_routesObject



11
12
13
14
15
# File 'lib/forest_admin_agent/routes/resources/list.rb', line 11

def setup_routes
  add_route('forest_list', 'get', '/:collection_name', ->(args) { handle_request(args) })

  self
end