Module: Railsful::Interceptors::Pagination

Included in:
Serializer
Defined in:
lib/railsful/interceptors/pagination.rb

Overview

Interceptor that paginates a given ActiveRecord::Relation with help of the Kaminari gem.

Instance Method Summary collapse

Instance Method Details

#pagination_options(options) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/railsful/interceptors/pagination.rb', line 12

def pagination_options(options)
  # Check if json value should be paginated.
  return options unless paginate?(options)

  # Get the relation from options hash so we can paginate it and
  # check the total count.
  relation = options.fetch(:json)

  # Paginate the relation and store new relation in temporary variable.
  paginated = paginate(relation)

  # Create a meta hash
  meta = {
    total_pages: paginated.try(:total_pages),
    total_count: paginated.try(:total_count),
    current_page: paginated.try(:current_page),
    next_page: paginated.try(:next_page),
    prev_page: paginated.try(:prev_page)
  }

  options.deeper_merge(
    links: links(paginated), meta: meta
  ).merge(json: paginated)
end

#render(options) ⇒ Object



8
9
10
# File 'lib/railsful/interceptors/pagination.rb', line 8

def render(options)
  super(pagination_options(options))
end