Module: Endpoints::Searches::SearchActions

Defined in:
app/api/endpoints/searches.rb

Instance Method Summary collapse

Instance Method Details

#search_action(name) ⇒ Object

rubocop:todo Metrics/AbcSize



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'app/api/endpoints/searches.rb', line 5

def search_action(name) # rubocop:todo Metrics/AbcSize
  bind_action(:create, to: name.to_s, as: name.to_sym) do |action, request, response|
    request.json['search']['page'] ||= request.path.fetch(1).to_i if request.path.fetch(1, false)
    scope = request.target.scope(request.json['search']).send(name)

    # If we're not paginated, just convert to an array. This will stop
    # the api from trying to paginate the results. Ideally all searches should be
    # paginated, but this may break downstream clients
    (scope.respond_to?(:total_entries) ? scope : scope.to_a).tap do |results|
      response.handled_by = action
      yield(response, results)
    end
  end
end

#singular_search_action(name) ⇒ Object

rubocop:todo Metrics/AbcSize, Metrics/MethodLength



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/api/endpoints/searches.rb', line 20

def singular_search_action(name) # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
  bind_action(:create, to: name.to_s, as: name.to_sym) do |_action, request, response|
    record = request.target.scope(request.json['search']).send(name.to_sym)
    raise ActiveRecord::RecordNotFound, 'no resources found with that search criteria' if record.nil?

    request.io = ::Core::Io::Registry.instance.lookup_for_object(record)
    request
      .io
      .eager_loading_for(record.class)
      .include_uuid
      .find(record.id)
      .tap { |_result| response.redirect_to(record.uuid) }
  end
end