Module: Hanami::Pagination::View

Includes:
Helpers
Defined in:
lib/hanami/pagination/view.rb

Instance Method Summary collapse

Instance Method Details

#current_page_tagObject



60
61
62
63
64
# File 'lib/hanami/pagination/view.rb', line 60

def current_page_tag
  html.span(class: 'pagination-current-page') do
    pager.current_page
  end
end

#ellipsis_tagObject



78
79
80
81
82
# File 'lib/hanami/pagination/view.rb', line 78

def ellipsis_tag
  html.span(class: 'pagination-ellipsis') do
    '...'
  end
end

#first_page_tag(page) ⇒ Object



48
49
50
51
52
# File 'lib/hanami/pagination/view.rb', line 48

def first_page_tag(page)
  html.a(href: n_page_path(page, 1), class: 'pagination-first-page') do
    '1'
  end
end

#last_page_tag(page) ⇒ Object



66
67
68
69
70
# File 'lib/hanami/pagination/view.rb', line 66

def last_page_tag(page)
  html.a(href: n_page_path(page, pager.total_pages), class: 'pagination-last-page') do
    pager.total_pages
  end
end

#n_page_path(page, n) ⇒ Object



28
29
30
# File 'lib/hanami/pagination/view.rb', line 28

def n_page_path(page, n)
  routes.path(page, **params, page: n)
end

#next_page_path(page) ⇒ Object



24
25
26
# File 'lib/hanami/pagination/view.rb', line 24

def next_page_path(page)
  routes.path(page, **params, page: pager.next_page)
end

#next_page_tag(page) ⇒ Object



72
73
74
75
76
# File 'lib/hanami/pagination/view.rb', line 72

def next_page_tag(page)
  html.a(href: next_page_path(page), class: 'pagination-next-page') do
    pager.next_page
  end
end

#next_page_urlObject



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

def next_page_url
  page_url(pager.next_page)
end

#page_url(page) ⇒ Object



16
17
18
# File 'lib/hanami/pagination/view.rb', line 16

def page_url(page)
  "#{params.env['REQUEST_PATH']}?page=#{page}"
end

#paginate(page) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/hanami/pagination/view.rb', line 32

def paginate(page)
  html.nav(class: 'pagination') do
    content = []

    content << first_page_tag(page) unless pager.first_page?
    content << ellipsis_tag if pager.current_page > 3
    content << previous_page_tag(page) if pager.current_page > 2
    content << current_page_tag
    content << next_page_tag(page) if (pager.total_pages - pager.current_page) > 1
    content << ellipsis_tag if (pager.total_pages - pager.current_page) > 3
    content << last_page_tag(page) unless pager.last_page?

    raw(content.map(&:to_s).join)
  end
end

#prev_page_urlObject



12
13
14
# File 'lib/hanami/pagination/view.rb', line 12

def prev_page_url
  page_url(pager.prev_page)
end

#previous_page_path(page) ⇒ Object



20
21
22
# File 'lib/hanami/pagination/view.rb', line 20

def previous_page_path(page)
  routes.path(page, **params, page: pager.prev_page)
end

#previous_page_tag(page) ⇒ Object



54
55
56
57
58
# File 'lib/hanami/pagination/view.rb', line 54

def previous_page_tag(page)
  html.a(href: previous_page_path(page), class: 'pagination-previous-page') do
    pager.prev_page
  end
end