Module: PassionView::Paginable

Extended by:
ActiveSupport::Concern
Includes:
Pathable
Defined in:
lib/passion_view/paginable.rb

Defined Under Namespace

Modules: Controller

Constant Summary collapse

DEFAULT_PER_PAGE =
10

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#current_pageObject

Returns the value of attribute current_page.



6
7
8
# File 'lib/passion_view/paginable.rb', line 6

def current_page
  @current_page
end

#current_perObject

Returns the value of attribute current_per.



6
7
8
# File 'lib/passion_view/paginable.rb', line 6

def current_per
  @current_per
end

Instance Method Details

#current_page?(page) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/passion_view/paginable.rb', line 39

def current_page?(page)
  page.to_i == current_page
end

#first_pageObject



43
44
45
# File 'lib/passion_view/paginable.rb', line 43

def first_page
  1
end

#first_page?(page = nil) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
32
# File 'lib/passion_view/paginable.rb', line 29

def first_page?(page = nil)
  page ||= current_page
  page.to_i == first_page
end

#first_page_pathObject



67
68
69
# File 'lib/passion_view/paginable.rb', line 67

def first_page_path
  paginable_path(page: first_page)
end

#initialize(items, options = {}) ⇒ Object



10
11
12
13
14
15
# File 'lib/passion_view/paginable.rb', line 10

def initialize(items, options = {})
  super
  pagination = options.delete(:pagination) || {}

  paginate_with(pagination[:page], pagination[:per])
end

#itemsObject



17
18
19
20
# File 'lib/passion_view/paginable.rb', line 17

def items
  super.page(current_page)
       .per(current_per)
end

#last_pageObject



59
60
61
# File 'lib/passion_view/paginable.rb', line 59

def last_page
  [items.total_pages, 1].max
end

#last_page?(page = nil) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
# File 'lib/passion_view/paginable.rb', line 34

def last_page?(page = nil)
  page ||= current_page
  page.to_i == last_page
end

#last_page_pathObject



79
80
81
# File 'lib/passion_view/paginable.rb', line 79

def last_page_path
  paginable_path(page: last_page)
end

#next_page(shift = 1) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/passion_view/paginable.rb', line 51

def next_page(shift = 1)
  target = current_page + shift
  target = first_page if target < first_page
  target = last_page  if target > last_page

  target
end

#next_page_path(shift = 1) ⇒ Object



75
76
77
# File 'lib/passion_view/paginable.rb', line 75

def next_page_path(shift = 1)
  paginable_path(page: next_page(shift))
end

#next_pages(count = 2) ⇒ Object



22
23
24
25
26
27
# File 'lib/passion_view/paginable.rb', line 22

def next_pages(count = 2)
  (-count..count).each do |idx|
    target = current_page + idx
    yield target, next_page_path(idx) if block_given? && !out_of_range?(target)
  end
end

#out_of_range?(target) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/passion_view/paginable.rb', line 63

def out_of_range?(target)
  (target < first_page) || (target > last_page)
end

#page_path(options = {}) ⇒ Object



87
88
89
# File 'lib/passion_view/paginable.rb', line 87

def page_path(options = {})
  options
end

#paginable_params(page = nil, per = nil) ⇒ Object



95
96
97
98
99
100
101
102
103
104
# File 'lib/passion_view/paginable.rb', line 95

def paginable_params(page = nil, per = nil)
  params = {}
  page ||= current_page
  per  ||= current_per

  params[:page] = page unless page == first_page
  params[:per]  = per  unless per.nil? || per == DEFAULT_PER_PAGE

  params
end

#paginable_path(options) ⇒ Object



83
84
85
# File 'lib/passion_view/paginable.rb', line 83

def paginable_path(options)
  page_path(url_options.merge(options))
end

#paginate_with(page, per = nil) ⇒ Object



106
107
108
109
# File 'lib/passion_view/paginable.rb', line 106

def paginate_with(page, per = nil)
  self.current_page = page.nil? ? 1 : page.to_i
  self.current_per  = per.nil?  ? DEFAULT_PER_PAGE : per.to_i
end

#previous_pageObject



47
48
49
# File 'lib/passion_view/paginable.rb', line 47

def previous_page
  next_page(-1)
end

#previous_page_pathObject



71
72
73
# File 'lib/passion_view/paginable.rb', line 71

def previous_page_path
  paginable_path(page: previous_page)
end

#url_optionsObject



91
92
93
# File 'lib/passion_view/paginable.rb', line 91

def url_options
  super.merge(paginable_params)
end