Class: Ckeditor::Paginatable

Inherits:
Object
  • Object
show all
Defined in:
lib/ckeditor/paginatable.rb

Overview

Simple paginate relation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scope, options = {}) ⇒ Paginatable

Returns a new instance of Paginatable.



8
9
10
11
# File 'lib/ckeditor/paginatable.rb', line 8

def initialize(scope, options = {})
  @scope = scope
  @limit_value = (options[:limit] || Ckeditor.default_per_page).to_i
end

Instance Attribute Details

#limit_valueObject (readonly)

Returns the value of attribute limit_value.



6
7
8
# File 'lib/ckeditor/paginatable.rb', line 6

def limit_value
  @limit_value
end

#offset_valueObject (readonly)

Returns the value of attribute offset_value.



6
7
8
# File 'lib/ckeditor/paginatable.rb', line 6

def offset_value
  @offset_value
end

Instance Method Details

#current_pageObject

Current page number



55
56
57
58
# File 'lib/ckeditor/paginatable.rb', line 55

def current_page
  offset = (offset_value < 0 ? 0 : offset_value)
  (offset / limit_value) + 1
end

#first_page?Boolean

First page of the collection?

Returns:

  • (Boolean)


35
36
37
# File 'lib/ckeditor/paginatable.rb', line 35

def first_page?
  current_page == 1
end

#last_page?Boolean

Last page of the collection?

Returns:

  • (Boolean)


40
41
42
# File 'lib/ckeditor/paginatable.rb', line 40

def last_page?
  current_page >= total_pages
end

#next_pageObject

Next page number in the collection



25
26
27
# File 'lib/ckeditor/paginatable.rb', line 25

def next_page
  current_page + 1 unless last_page?
end

#page(num = 1) ⇒ Object



13
14
15
16
# File 'lib/ckeditor/paginatable.rb', line 13

def page(num = 1)
  @offset_value = limit_value * ([num.to_i, 1].max - 1)
  self
end

#prev_pageObject

Previous page number in the collection



30
31
32
# File 'lib/ckeditor/paginatable.rb', line 30

def prev_page
  current_page - 1 unless first_page?
end

#scopedObject Also known as: all, to_a



18
19
20
# File 'lib/ckeditor/paginatable.rb', line 18

def scoped
  @scope.limit(limit_value).offset(offset_value)
end

#total_countObject

total item numbers of scope



50
51
52
# File 'lib/ckeditor/paginatable.rb', line 50

def total_count
  @total_count ||= @scope.count
end

#total_pagesObject

Total number of pages



45
46
47
# File 'lib/ckeditor/paginatable.rb', line 45

def total_pages
  (total_count.to_f / limit_value).ceil
end