Class: Redmine::Pagination::Paginator

Inherits:
Object
  • Object
show all
Defined in:
lib/redmine/pagination.rb

Overview

Since:

  • 2.3.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Paginator

Returns a new instance of Paginator



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/redmine/pagination.rb', line 25

def initialize(*args)
  if args.first.is_a?(ActionController::Base)
    args.shift
    ActiveSupport::Deprecation.warn "Paginator no longer takes a controller instance as the first argument. Remove it from #new arguments."
  end
  item_count, per_page, page, page_param = *args

  @item_count = item_count
  @per_page = per_page
  page = (page || 1).to_i
  if page < 1
    page = 1
  end
  @page = page
  @page_param = page_param || :page
end

Instance Attribute Details

#item_countObject (readonly)

Returns the value of attribute item_count



23
24
25
# File 'lib/redmine/pagination.rb', line 23

def item_count
  @item_count
end

#pageObject (readonly)

Returns the value of attribute page



23
24
25
# File 'lib/redmine/pagination.rb', line 23

def page
  @page
end

#page_paramObject (readonly)

Returns the value of attribute page_param



23
24
25
# File 'lib/redmine/pagination.rb', line 23

def page_param
  @page_param
end

#per_pageObject (readonly)

Returns the value of attribute per_page



23
24
25
# File 'lib/redmine/pagination.rb', line 23

def per_page
  @per_page
end

Instance Method Details

#currentObject



102
103
104
105
# File 'lib/redmine/pagination.rb', line 102

def current
  ActiveSupport::Deprecation.warn "Paginator#current will be removed. Use .offset instead of .current.offset."
  self
end

#first_itemObject



74
75
76
# File 'lib/redmine/pagination.rb', line 74

def first_item
  item_count == 0 ? 0 : (offset + 1)
end

#first_pageObject



46
47
48
49
50
# File 'lib/redmine/pagination.rb', line 46

def first_page
  if item_count > 0
    1
  end
end

#items_per_pageObject



97
98
99
100
# File 'lib/redmine/pagination.rb', line 97

def items_per_page
  ActiveSupport::Deprecation.warn "Paginator#items_per_page will be removed. Use #per_page instead."
  per_page
end

#last_itemObject



78
79
80
81
# File 'lib/redmine/pagination.rb', line 78

def last_item
  l = first_item + per_page - 1
  l > item_count ? item_count : l
end

#last_pageObject



64
65
66
67
68
# File 'lib/redmine/pagination.rb', line 64

def last_page
  if item_count > 0
    (item_count - 1) / per_page + 1
  end
end

#linked_pagesObject



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/redmine/pagination.rb', line 83

def linked_pages
  pages = []
  if item_count > 0
    pages += [first_page, page, last_page]
    pages += ((page-2)..(page+2)).to_a.select {|p| p > first_page && p < last_page}
  end
  pages = pages.compact.uniq.sort
  if pages.size > 1
    pages
  else
    []
  end
end

#multiple_pages?Boolean

Returns:

  • (Boolean)

Since:

  • 3.3.0



70
71
72
# File 'lib/redmine/pagination.rb', line 70

def multiple_pages?
  per_page < item_count
end

#next_pageObject



58
59
60
61
62
# File 'lib/redmine/pagination.rb', line 58

def next_page
  if last_item < item_count
    page + 1
  end
end

#offsetObject



42
43
44
# File 'lib/redmine/pagination.rb', line 42

def offset
  (page - 1) * per_page
end

#previous_pageObject



52
53
54
55
56
# File 'lib/redmine/pagination.rb', line 52

def previous_page
  if page > 1
    page - 1
  end
end