Class: Pragma::Operation::Index

Inherits:
Base
  • Object
show all
Includes:
Defaults
Defined in:
lib/pragma/operation/index.rb

Overview

Finds all records of the requested resource, authorizes them, paginates them and returns the decorated collection.

Author:

  • Alessandro Desantis

Instance Method Summary collapse

Methods included from Defaults

included

Instance Method Details

#callObject



11
12
13
14
15
16
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
# File 'lib/pragma/operation/index.rb', line 11

def call
  context.records = authorize_collection(find_records)

  begin
    context.records = context.records.paginate(page: page, per_page: per_page)
  rescue RangeError => e
    respond_with!(
      status: :bad_request,
      resource: {
        error_type: :invalid_page,
        error_message: e.message
      }
    )
  end

  respond_with(
    resource: decorate(context.records),
    status: :ok,
    headers: {
      'Page' => context.records.current_page.to_i,
      'Per-Page' => context.records.per_page,
      'Total' => context.records.total_entries
    },
    links: {
      first: build_page_url(1),
      last: build_page_url(context.records.total_pages),
      next: (build_page_url(context.records.next_page) if context.records.next_page),
      prev: (build_page_url(context.records.previous_page) if context.records.previous_page)
    }
  )
end