Class: Pager
- Inherits:
-
Struct
- Object
- Struct
- Pager
- Defined in:
- public/app/models/pager.rb
Overview
container for paging information, to keep it in one place
Constant Summary collapse
- PAGE_NUMBERS_TO_SHOW =
10
Instance Attribute Summary collapse
-
#last_page ⇒ Object
Returns the value of attribute last_page.
-
#link ⇒ Object
Returns the value of attribute link.
-
#need_last ⇒ Object
Returns the value of attribute need_last.
-
#need_next ⇒ Object
Returns the value of attribute need_next.
-
#need_prev ⇒ Object
Returns the value of attribute need_prev.
-
#next ⇒ Object
Returns the value of attribute next.
-
#page ⇒ Object
Returns the value of attribute page.
-
#pages ⇒ Object
Returns the value of attribute pages.
-
#prev ⇒ Object
Returns the value of attribute prev.
Instance Method Summary collapse
-
#initialize(link, page, last_page) ⇒ Pager
constructor
A new instance of Pager.
-
#one_page? ⇒ Boolean
-
#to_s ⇒ Object
Constructor Details
#initialize(link, page, last_page) ⇒ Pager
Returns a new instance of Pager.
4 5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'public/app/models/pager.rb', line 4 def initialize(link, page, last_page) self.link = link self.page = page.to_i || 1 self.last_page = last_page.to_i lower_page = [ (self.page - PAGE_NUMBERS_TO_SHOW / 2), 1].max upper_page = [lower_page + PAGE_NUMBERS_TO_SHOW, (last_page.to_i == 1? 1 : last_page.to_i + 1) ].min self.need_prev = (lower_page > 1) self.prev = self.page - 1 self.need_next = (upper_page < last_page.to_i) self.need_last = (last_page.to_i + 1 > upper_page ) self.next = self.page + 1 self.pages = Range.new(lower_page, upper_page, true) end |
Instance Attribute Details
#last_page ⇒ Object
Returns the value of attribute last_page
2 3 4 |
# File 'public/app/models/pager.rb', line 2 def last_page @last_page end |
#link ⇒ Object
Returns the value of attribute link
2 3 4 |
# File 'public/app/models/pager.rb', line 2 def link @link end |
#need_last ⇒ Object
Returns the value of attribute need_last
2 3 4 |
# File 'public/app/models/pager.rb', line 2 def need_last @need_last end |
#need_next ⇒ Object
Returns the value of attribute need_next
2 3 4 |
# File 'public/app/models/pager.rb', line 2 def need_next @need_next end |
#need_prev ⇒ Object
Returns the value of attribute need_prev
2 3 4 |
# File 'public/app/models/pager.rb', line 2 def need_prev @need_prev end |
#next ⇒ Object
Returns the value of attribute next
2 3 4 |
# File 'public/app/models/pager.rb', line 2 def next @next end |
#page ⇒ Object
Returns the value of attribute page
2 3 4 |
# File 'public/app/models/pager.rb', line 2 def page @page end |
#pages ⇒ Object
Returns the value of attribute pages
2 3 4 |
# File 'public/app/models/pager.rb', line 2 def pages @pages end |
#prev ⇒ Object
Returns the value of attribute prev
2 3 4 |
# File 'public/app/models/pager.rb', line 2 def prev @prev end |
Instance Method Details
#one_page? ⇒ Boolean
18 19 20 |
# File 'public/app/models/pager.rb', line 18 def one_page? last_page < 2 end |
#to_s ⇒ Object
22 23 24 |
# File 'public/app/models/pager.rb', line 22 def to_s "Link: #{self.link} Page #{self.page} Last Page #{self.last_page} Need Prev? #{self.need_prev} Need Next: #{self.need_next}" end |