Class: ResqueCleaner::Server::Paginate

Inherits:
Object
  • Object
show all
Defined in:
lib/resque_cleaner/server.rb

Overview

Pagination helper for list page.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(jobs, url, page = 1, page_size = 20) ⇒ Paginate

Returns a new instance of Paginate.



18
19
20
21
22
23
# File 'lib/resque_cleaner/server.rb', line 18

def initialize(jobs, url, page=1, page_size=20)
  @jobs = jobs
  @url = url
  @page = (!page || page < 1) ? 1 : page
  @page_size = 20
end

Instance Attribute Details

#jobsObject

Returns the value of attribute jobs.



17
18
19
# File 'lib/resque_cleaner/server.rb', line 17

def jobs
  @jobs
end

#pageObject

Returns the value of attribute page.



17
18
19
# File 'lib/resque_cleaner/server.rb', line 17

def page
  @page
end

#page_sizeObject

Returns the value of attribute page_size.



17
18
19
# File 'lib/resque_cleaner/server.rb', line 17

def page_size
  @page_size
end

#urlObject

Returns the value of attribute url.



17
18
19
# File 'lib/resque_cleaner/server.rb', line 17

def url
  @url
end

Instance Method Details

#first_indexObject



25
26
27
# File 'lib/resque_cleaner/server.rb', line 25

def first_index
  @page_size * (@page-1)
end

#first_page?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/resque_cleaner/server.rb', line 38

def first_page?
  @page <= 1
end

#last_indexObject



29
30
31
32
# File 'lib/resque_cleaner/server.rb', line 29

def last_index
  last = first_index + @page_size - 1
  last > @jobs.size-1 ? @jobs.size-1 : last
end

#last_page?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/resque_cleaner/server.rb', line 42

def last_page?
  @page >= max_page
end

#max_pageObject



60
61
62
# File 'lib/resque_cleaner/server.rb', line 60

def max_page
  ((total_size-1) / @page_size) + 1
end

#page_url(page) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/resque_cleaner/server.rb', line 46

def page_url(page)
  u = @url
  u += @url.include?("?") ? "&" : "?"
  if page.is_a?(Symbol)
    page = @page - 1 if page==:prev
    page = @page + 1 if page==:next
  end
  u += "p=#{page}"
end

#paginated_jobsObject



34
35
36
# File 'lib/resque_cleaner/server.rb', line 34

def paginated_jobs
  @jobs[first_index,@page_size]
end

#total_sizeObject



56
57
58
# File 'lib/resque_cleaner/server.rb', line 56

def total_size
  @jobs.size
end