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.



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

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.



4
5
6
# File 'lib/ckeditor/paginatable.rb', line 4

def limit_value
  @limit_value
end

#offset_valueObject (readonly)

Returns the value of attribute offset_value.



4
5
6
# File 'lib/ckeditor/paginatable.rb', line 4

def offset_value
  @offset_value
end

Instance Method Details

#current_pageObject

Current page number



53
54
55
56
# File 'lib/ckeditor/paginatable.rb', line 53

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)


33
34
35
# File 'lib/ckeditor/paginatable.rb', line 33

def first_page?
  current_page == 1
end

#last_page?Boolean

Last page of the collection?

Returns:

  • (Boolean)


38
39
40
# File 'lib/ckeditor/paginatable.rb', line 38

def last_page?
  current_page >= total_pages
end

#next_pageObject

Next page number in the collection



23
24
25
# File 'lib/ckeditor/paginatable.rb', line 23

def next_page
  current_page + 1 unless last_page?
end

#page(num = 1) ⇒ Object



11
12
13
14
# File 'lib/ckeditor/paginatable.rb', line 11

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

#prev_pageObject

Previous page number in the collection



28
29
30
# File 'lib/ckeditor/paginatable.rb', line 28

def prev_page
  current_page - 1 unless first_page?
end

#scopedObject Also known as: all, to_a



16
17
18
# File 'lib/ckeditor/paginatable.rb', line 16

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

#total_countObject

total item numbers of scope



48
49
50
# File 'lib/ckeditor/paginatable.rb', line 48

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

#total_pagesObject

Total number of pages



43
44
45
# File 'lib/ckeditor/paginatable.rb', line 43

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