Class: Pager

Inherits:
Struct
  • Object
show all
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

Instance Method Summary collapse

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_pageObject

Returns the value of attribute last_page

Returns:

  • (Object)

    the current value of last_page



2
3
4
# File 'public/app/models/pager.rb', line 2

def last_page
  @last_page
end

Returns the value of attribute link

Returns:

  • (Object)

    the current value of link



2
3
4
# File 'public/app/models/pager.rb', line 2

def link
  @link
end

#need_lastObject

Returns the value of attribute need_last

Returns:

  • (Object)

    the current value of need_last



2
3
4
# File 'public/app/models/pager.rb', line 2

def need_last
  @need_last
end

#need_nextObject

Returns the value of attribute need_next

Returns:

  • (Object)

    the current value of need_next



2
3
4
# File 'public/app/models/pager.rb', line 2

def need_next
  @need_next
end

#need_prevObject

Returns the value of attribute need_prev

Returns:

  • (Object)

    the current value of need_prev



2
3
4
# File 'public/app/models/pager.rb', line 2

def need_prev
  @need_prev
end

#nextObject

Returns the value of attribute next

Returns:

  • (Object)

    the current value of next



2
3
4
# File 'public/app/models/pager.rb', line 2

def next
  @next
end

#pageObject

Returns the value of attribute page

Returns:

  • (Object)

    the current value of page



2
3
4
# File 'public/app/models/pager.rb', line 2

def page
  @page
end

#pagesObject

Returns the value of attribute pages

Returns:

  • (Object)

    the current value of pages



2
3
4
# File 'public/app/models/pager.rb', line 2

def pages
  @pages
end

#prevObject

Returns the value of attribute prev

Returns:

  • (Object)

    the current value of prev



2
3
4
# File 'public/app/models/pager.rb', line 2

def prev
  @prev
end

Instance Method Details

#one_page?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'public/app/models/pager.rb', line 18

def one_page?
  last_page < 2
end

#to_sObject



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