Class: Super::Pagination

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/super/pagination.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(total_count:, limit:, query_params:, page_query_param:) ⇒ Pagination

Returns a new instance of Pagination.



8
9
10
11
12
13
14
15
# File 'lib/super/pagination.rb', line 8

def initialize(total_count:, limit:, query_params:, page_query_param:)
  @total_count = total_count.to_i
  @limit = limit.to_i
  @query_params = query_params
  @page_query_param = page_query_param

  self.current_pageno = query_params[page_query_param]
end

Instance Attribute Details

#current_pagenoObject

Returns the value of attribute current_pageno.



5
6
7
# File 'lib/super/pagination.rb', line 5

def current_pageno
  @current_pageno
end

#limitObject (readonly)

Returns the value of attribute limit.



6
7
8
# File 'lib/super/pagination.rb', line 6

def limit
  @limit
end

Instance Method Details

#eachObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/super/pagination.rb', line 32

def each
  if !block_given?
    return enum_for(:each)
  end

  quotient, remainder = @total_count.divmod(@limit)
  pages =
    if remainder.zero?
      quotient
    else
      quotient + 1
    end

  (1..pages).each do |pageno|
    is_current_page = pageno == current_pageno
    display = pageno.to_s
    page_query_params = @query_params.dup
    if pageno == 1
      page_query_params.delete(:page)
    else
      page_query_params[@page_query_param] = pageno
    end

    yield(page_query_params, is_current_page, display)
  end
end

#offsetObject



17
18
19
# File 'lib/super/pagination.rb', line 17

def offset
  limit * (current_pageno - 1)
end