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



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

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)


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

def first_page?
  current_page == 1
end

#last_page?Boolean

Last page of the collection?

Returns:

  • (Boolean)


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

def last_page?
  current_page >= total_pages
end

#next_pageObject

Next page number in the collection



21
22
23
# File 'lib/ckeditor/paginatable.rb', line 21

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



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

def prev_page
  current_page - 1 unless first_page?
end

#scopedObject



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



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

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

#total_pagesObject

Total number of pages



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

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