Module: PassionView::Paginable
Defined Under Namespace
Modules: Controller
Constant Summary collapse
- DEFAULT_PER_PAGE =
10
Instance Attribute Summary collapse
-
#current_page ⇒ Object
Returns the value of attribute current_page.
-
#current_per ⇒ Object
Returns the value of attribute current_per.
Instance Method Summary collapse
- #current_page?(page) ⇒ Boolean
- #first_page ⇒ Object
- #first_page?(page = nil) ⇒ Boolean
- #first_page_path ⇒ Object
- #initialize(items, options = {}) ⇒ Object
- #items ⇒ Object
- #last_page ⇒ Object
- #last_page?(page = nil) ⇒ Boolean
- #last_page_path ⇒ Object
- #next_page(shift = 1) ⇒ Object
- #next_page_path(shift = 1) ⇒ Object
- #next_pages(count = 2) ⇒ Object
- #out_of_range?(target) ⇒ Boolean
- #page_path(options = {}) ⇒ Object
- #paginable_params(page = nil, per = nil) ⇒ Object
- #paginable_path(options) ⇒ Object
- #paginate_with(page, per = nil) ⇒ Object
- #previous_page ⇒ Object
- #previous_page_path ⇒ Object
- #url_options ⇒ Object
Instance Attribute Details
#current_page ⇒ Object
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_per ⇒ Object
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
39 40 41 |
# File 'lib/passion_view/paginable.rb', line 39 def current_page?(page) page.to_i == current_page end |
#first_page ⇒ Object
43 44 45 |
# File 'lib/passion_view/paginable.rb', line 43 def first_page 1 end |
#first_page?(page = nil) ⇒ 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_path ⇒ Object
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, = {}) super pagination = .delete(:pagination) || {} paginate_with(pagination[:page], pagination[:per]) end |
#items ⇒ Object
17 18 19 20 |
# File 'lib/passion_view/paginable.rb', line 17 def items super.page(current_page) .per(current_per) end |
#last_page ⇒ Object
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
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_path ⇒ Object
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
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( = {}) 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() page_path(.merge()) 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_page ⇒ Object
47 48 49 |
# File 'lib/passion_view/paginable.rb', line 47 def previous_page next_page(-1) end |
#previous_page_path ⇒ Object
71 72 73 |
# File 'lib/passion_view/paginable.rb', line 71 def previous_page_path paginable_path(page: previous_page) end |
#url_options ⇒ Object
91 92 93 |
# File 'lib/passion_view/paginable.rb', line 91 def super.merge(paginable_params) end |