Module: V1::ApiController::Pagination

Included in:
V1::ApiController
Defined in:
app/controllers/v1/api_controller/pagination.rb

Overview

Pagination request module

Instance Method Summary collapse

Instance Method Details

#compile_page(uri, page) ⇒ string

Compile the page uri for request.


25
26
27
28
29
30
31
32
33
34
# File 'app/controllers/v1/api_controller/pagination.rb', line 25

def compile_page(uri, page)
  # Get the query params of the URI request
  query_params = Rack::Utils.parse_query(uri.query)
  # Change the param page for the *page* param
  query_params['page'] = page.to_s
  # Change the uri query with the new data
  uri.query = Rack::Utils.build_query(query_params)
  # Return the URI in String format
  uri.to_s
end

#select_page(collection) ⇒ ActiveRecord

Get the specific page collection with limit option.


12
13
14
15
16
17
18
# File 'app/controllers/v1/api_controller/pagination.rb', line 12

def select_page(collection)
  page = params['page'] || 1
  # The value of the param of the request or the configuration value
  limit = params['limit'] || Rails.application.secrets.return_configuration[:items_per_page]
  # The result por the page *page* with a limit of *limit*
  collection.page(page).per(limit)
end