Module: Kaminari::PageScopeMethods

Defined in:
lib/kaminari/models/page_scope_methods.rb

Instance Method Summary collapse

Instance Method Details

#current_pageObject

Current page number



47
48
49
50
51
52
53
54
55
# File 'lib/kaminari/models/page_scope_methods.rb', line 47

def current_page
  offset_without_padding = offset_value
  offset_without_padding -= @_padding if defined?(@_padding) && @_padding
  offset_without_padding = 0 if offset_without_padding < 0

  (offset_without_padding / limit_value) + 1
rescue ZeroDivisionError
  raise ZeroPerPageOperation, "Current page was incalculable. Perhaps you called .per(0)?"
end

#current_per_pageObject

Current per-page number



58
59
60
61
62
63
64
# File 'lib/kaminari/models/page_scope_methods.rb', line 58

def current_per_page
  ActiveSupport::Deprecation.warn '#current_per_page is deprecated and will be removed in the next major ' \
                                  'version. Please use #limit_value instead.'

  limit_value
  # (defined?(@_per) && @_per) || default_per_page
end

#first_page?Boolean

First page of the collection?

Returns:

  • (Boolean)


77
78
79
# File 'lib/kaminari/models/page_scope_methods.rb', line 77

def first_page?
  current_page == 1
end

#last_page?Boolean

Last page of the collection?

Returns:

  • (Boolean)


82
83
84
# File 'lib/kaminari/models/page_scope_methods.rb', line 82

def last_page?
  current_page == total_pages
end

#max_paginates_per(new_max_per_page) ⇒ Object



21
22
23
24
25
# File 'lib/kaminari/models/page_scope_methods.rb', line 21

def max_paginates_per(new_max_per_page)
  @_max_per_page = new_max_per_page

  per (defined?(@_per) && @_per) || default_per_page, max_per_page: new_max_per_page
end

#next_pageObject

Next page number in the collection



67
68
69
# File 'lib/kaminari/models/page_scope_methods.rb', line 67

def next_page
  current_page + 1 unless last_page? || out_of_range?
end

#out_of_range?Boolean

Out of range of the collection?

Returns:

  • (Boolean)


87
88
89
# File 'lib/kaminari/models/page_scope_methods.rb', line 87

def out_of_range?
  current_page > total_pages
end

#padding(num) ⇒ Object

Raises:

  • (ArgumentError)


27
28
29
30
31
32
# File 'lib/kaminari/models/page_scope_methods.rb', line 27

def padding(num)
  num = num.to_i
  raise ArgumentError, "padding must not be negative" if num < 0
  @_padding = num
  offset(offset_value + @_padding)
end

#per(num, max_per_page: nil) ⇒ Object

Specify the per_page value for the preceding page scope

Model.page(3).per(10)


7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/kaminari/models/page_scope_methods.rb', line 7

def per(num, max_per_page: nil)
  max_per_page ||= ((defined?(@_max_per_page) && @_max_per_page) || self.max_per_page)
  @_per = (num || default_per_page).to_i
  if (n = num.to_i) < 0 || !(/^\d/ =~ num.to_s)
    self
  elsif n.zero?
    limit(n)
  elsif max_per_page && (max_per_page < n)
    limit(max_per_page).offset(offset_value / limit_value * max_per_page)
  else
    limit(n).offset(offset_value / limit_value * n)
  end
end

#prev_pageObject

Previous page number in the collection



72
73
74
# File 'lib/kaminari/models/page_scope_methods.rb', line 72

def prev_page
  current_page - 1 unless first_page? || out_of_range?
end

#total_pagesObject

Total number of pages



35
36
37
38
39
40
41
42
43
44
# File 'lib/kaminari/models/page_scope_methods.rb', line 35

def total_pages
  count_without_padding = total_count
  count_without_padding -= @_padding if defined?(@_padding) && @_padding
  count_without_padding = 0 if count_without_padding < 0

  total_pages_count = (count_without_padding.to_f / limit_value).ceil
  max_pages && (max_pages < total_pages_count) ? max_pages : total_pages_count
rescue FloatDomainError
  raise ZeroPerPageOperation, "The number of total pages was incalculable. Perhaps you called .per(0)?"
end