Class: Super::Pagination

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

Defined Under Namespace

Modules: ControllerMethods

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.



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

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.



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

def current_pageno
  @current_pageno
end

#limitObject (readonly)

Returns the value of attribute limit.



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

def limit
  @limit
end

Instance Method Details

#eachObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/super/pagination.rb', line 38

def each
  if !block_given?
    return enum_for(:each)
  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

#necessary?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/super/pagination.rb', line 34

def necessary?
  pages > 1
end

#offsetObject



19
20
21
# File 'lib/super/pagination.rb', line 19

def offset
  limit * (current_pageno - 1)
end

#to_partial_pathObject



57
58
59
# File 'lib/super/pagination.rb', line 57

def to_partial_path
  "pagination"
end