Module: Endpoints::Searches::SearchActions

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

Instance Method Summary collapse

Instance Method Details

#search_action(name) ⇒ Object


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

def search_action(name)
  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


18
19
20
21
22
23
24
25
26
27
28
# File 'app/api/endpoints/searches.rb', line 18

def singular_search_action(name)
  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 do |_result|
      response.redirect_to(record.uuid)
    end
  end
end