Module: JustPaginate
- Defined in:
- lib/just_paginate.rb
Constant Summary collapse
- VERSION =
"0.1"
Class Method Summary collapse
- .index_range(curr_page, per_page, total_entry_count) ⇒ Object
- .page_labels(current, total) ⇒ Object
- .page_links(curr_page, total_page_count, &page_link_constructor) ⇒ Object
- .page_navigation(curr_page, total_page_count, &page_link_constructor) ⇒ Object
- .page_out_of_bounds?(curr_page, per_page, total_entry_count) ⇒ Boolean
-
.page_value(page) ⇒ Object
TODO make sure negative numbers, non-integers etc are just converted to page 1.
- .paginate(curr_page, per_page, total_entry_count, &selection_strategy) ⇒ Object
- .total_page_number(total_entry_count, per_page) ⇒ Object
Class Method Details
.index_range(curr_page, per_page, total_entry_count) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/just_paginate.rb', line 26 def self.index_range(curr_page, per_page, total_entry_count) start_index = ((curr_page-1)*per_page) end_index = (start_index+per_page)-1 if total_entry_count == 0 return 0..0 end if(start_index>(total_entry_count-1)) start_index = total_entry_count-per_page end_index = total_entry_count-1 end if end_index>total_entry_count end_index = total_entry_count end Range.new(start_index, end_index) end |
.page_labels(current, total) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/just_paginate.rb', line 89 def self.page_labels(current, total) max_visible = 10 labels = [] if total <= max_visible 1.upto(total).each {|n| labels.push(n.to_s)} else if current > max_visible labels = ["<", "1", "..."] else current = 1 end if (current > (total-max_visible)) current = (total-max_visible)+1 end current.upto(current+(max_visible-1)).each {|n| labels.push(n.to_s)} if (current <= (total-max_visible)) labels.concat ["...", "#{total}", ">"] end end return labels end |
.page_links(curr_page, total_page_count, &page_link_constructor) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/just_paginate.rb', line 65 def self.page_links(curr_page, total_page_count, &page_link_constructor) page_labels(curr_page, total_page_count).map do |label| page_element = "" if label == "..." page_element = "<li class='disabled'><a>#{label}</a></li>" elsif label == "<" page_url = yield(curr_page-1) page_element = "<li><a rel='prev' href='#{page_url}'>#{label}</a></li>" elsif label == ">" page_url = yield(curr_page+1) page_element = "<li><a rel='next' href='#{page_url}'>#{label}</a></li>" else page_url = yield(label) if label.to_i == curr_page page_element = "<li class='active'><a>#{label}</a></li>" else page_element = "<li><a href='#{page_url}'>#{label}</a></li>" end end end.join(" ") end |
.page_navigation(curr_page, total_page_count, &page_link_constructor) ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/just_paginate.rb', line 56 def self.(curr_page, total_page_count, &page_link_constructor) links = page_links(curr_page.to_i, total_page_count, &page_link_constructor) if total_page_count > 1 "<div class='pagination'><ul>#{links}</ul></div>" else "" end end |
.page_out_of_bounds?(curr_page, per_page, total_entry_count) ⇒ Boolean
46 47 48 49 50 51 52 53 54 |
# File 'lib/just_paginate.rb', line 46 def self.page_out_of_bounds?(curr_page, per_page, total_entry_count) if curr_page < 1 true else start_index = ((curr_page-1)*per_page) end_index = (start_index+per_page)-1 start_index > total_entry_count end end |
.page_value(page) ⇒ Object
TODO make sure negative numbers, non-integers etc are just converted to page 1.
7 8 9 10 11 12 13 |
# File 'lib/just_paginate.rb', line 7 def self.page_value(page) if page.nil? 1 else page.to_i end end |
.paginate(curr_page, per_page, total_entry_count, &selection_strategy) ⇒ Object
15 16 17 18 19 20 |
# File 'lib/just_paginate.rb', line 15 def self.paginate(curr_page, per_page, total_entry_count, &selection_strategy) raise "Pagination just supplies index range, expects a selection strategy" if selection_strategy.nil? entries = yield(index_range(curr_page, per_page, total_entry_count)) || [] return entries, total_page_number(total_entry_count, per_page) end |
.total_page_number(total_entry_count, per_page) ⇒ Object
22 23 24 |
# File 'lib/just_paginate.rb', line 22 def self.total_page_number(total_entry_count, per_page) (total_entry_count.to_f / per_page).ceil end |