Class: Api::Paging::IndexPaginator

Inherits:
Object
  • Object
show all
Defined in:
lib/api/paging/index_paginator.rb

Instance Method Summary collapse

Constructor Details

#initialize(query_base, current_page, limit) ⇒ IndexPaginator

Returns a new instance of IndexPaginator.


6
7
8
9
10
# File 'lib/api/paging/index_paginator.rb', line 6

def initialize(query_base, current_page, limit)
  @query_base = query_base
  @current_page = current_page.to_i
  @limit = limit.to_i
end

Instance Method Details

#filter_parameters(parameters) ⇒ Object


37
38
39
# File 'lib/api/paging/index_paginator.rb', line 37

def filter_parameters(parameters)
  parameters.delete(:page)
end

#next_page(for_url = true) ⇒ Object


19
20
21
22
23
24
25
26
# File 'lib/api/paging/index_paginator.rb', line 19

def next_page(for_url=true)
  page_data
  return nil if for_url && @current_page == @max_page

  return "page=#{@current_page + 1}" if for_url

  IndexPaginator.new(@query_base, @current_page + 1, @limit)
end

#page_dataObject


12
13
14
15
16
17
# File 'lib/api/paging/index_paginator.rb', line 12

def page_data
  @page_data ||= @query_base.paginate(page: @current_page, per_page: @limit)
  @max_page = (@query_base.count * 1.0 / @limit * 1.0).ceil
  @max_page = 1 if @max_page < 1
  @page_data
end

#previous_page(for_url = true) ⇒ Object


28
29
30
31
32
33
34
35
# File 'lib/api/paging/index_paginator.rb', line 28

def previous_page(for_url=true)
  page_data
  return nil if for_url && @current_page == 1

  return "page=#{@current_page - 1}" if for_url

  IndexPaginator.new(@query_base, @current_page - 1, @limit)
end