Class: Paginate

Inherits:
Object
  • Object
show all
Defined in:
app/controllers/resque_controller.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.



327
328
329
330
331
332
# File 'app/controllers/resque_controller.rb', line 327

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.



325
326
327
# File 'app/controllers/resque_controller.rb', line 325

def jobs
  @jobs
end

#pageObject

Returns the value of attribute page.



325
326
327
# File 'app/controllers/resque_controller.rb', line 325

def page
  @page
end

#page_sizeObject

Returns the value of attribute page_size.



325
326
327
# File 'app/controllers/resque_controller.rb', line 325

def page_size
  @page_size
end

#urlObject

Returns the value of attribute url.



325
326
327
# File 'app/controllers/resque_controller.rb', line 325

def url
  @url
end

Instance Method Details

#first_indexObject



334
335
336
# File 'app/controllers/resque_controller.rb', line 334

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

#first_page?Boolean

Returns:

  • (Boolean)


347
348
349
# File 'app/controllers/resque_controller.rb', line 347

def first_page?
  @page <= 1
end

#last_indexObject



338
339
340
341
# File 'app/controllers/resque_controller.rb', line 338

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

#last_page?Boolean

Returns:

  • (Boolean)


351
352
353
# File 'app/controllers/resque_controller.rb', line 351

def last_page?
  @page >= max_page
end

#max_pageObject



369
370
371
# File 'app/controllers/resque_controller.rb', line 369

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

#page_url(page) ⇒ Object



355
356
357
358
359
360
361
362
363
# File 'app/controllers/resque_controller.rb', line 355

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



343
344
345
# File 'app/controllers/resque_controller.rb', line 343

def paginated_jobs
  @jobs[first_index, @page_size]
end

#total_sizeObject



365
366
367
# File 'app/controllers/resque_controller.rb', line 365

def total_size
  @jobs.size
end