Class: TableMe::TablePagination

Inherits:
Object
  • Object
show all
Defined in:
lib/table_me/table_pagination.rb

Overview

This handles the pagination elements of the table

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table_options) ⇒ TablePagination

Returns a new instance of TablePagination.



9
10
11
# File 'lib/table_me/table_pagination.rb', line 9

def initialize table_options
  self.options = table_options
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



7
8
9
# File 'lib/table_me/table_pagination.rb', line 7

def options
  @options
end

Instance Method Details

#next_page_urlObject



35
36
37
38
39
40
41
42
43
# File 'lib/table_me/table_pagination.rb', line 35

def next_page_url
  page = if current_page == total_pages
    total_pages
  else
    current_page + 1
  end

  link_for_page page
end

#pagination_controlsObject

Adds controls at the bottom of the table for previous and next and a 5 number range TODO Refactor so controllers are hidden when pages on unavailable. IE, if your on page 1 you shouldn’t be able to see a previous button, or if your on the last page you shouldn’t be able to see the next button



27
28
29
30
31
32
33
# File 'lib/table_me/table_pagination.rb', line 27

def pagination_controls
  <<-HTML.strip_heredoc
    <div class='table-me-pagination-controls'>
      <a href="#{prev_page_url}" class='previous'>&laquo; Prev</a> #{pagination_number_list} <a href="#{next_page_url}" class='next'>Next &raquo;</a>
    </div>
  HTML
end

#pagination_infoObject

Information at the top of the table displaying the table name and position page/item wise out of a total.



15
16
17
18
19
20
21
# File 'lib/table_me/table_pagination.rb', line 15

def pagination_info
  <<-HTML.strip_heredoc
    <div class='table-me-pagination-info'>
      <h3>#{options[:name].split('_').join(' ').titleize}</h3> <p><b>#{options[:page]}</b> of <b>#{options[:page_total]}</b> out of a total <b>#{options[:total_count]}</b></p>
    </div>
  HTML
end

#pagination_number_listObject

List of number links for the number range between next and previous



56
57
58
59
60
61
# File 'lib/table_me/table_pagination.rb', line 56

def pagination_number_list 
  (0...page_button_count).to_a.map do |n|
    link_number = n + page_number_offset
    number_span(link_number)
  end.join(' ')
end

#prev_page_urlObject



45
46
47
48
49
50
51
52
53
# File 'lib/table_me/table_pagination.rb', line 45

def prev_page_url
  page = if current_page == 0
    0
  else
    current_page - 1
  end

  link_for_page page
end