Class: Ckeditor::Paginatable
- Inherits:
-
Object
- Object
- Ckeditor::Paginatable
- Defined in:
- lib/ckeditor/paginatable.rb
Overview
Simple paginate relation
Instance Attribute Summary collapse
-
#limit_value ⇒ Object
readonly
Returns the value of attribute limit_value.
-
#offset_value ⇒ Object
readonly
Returns the value of attribute offset_value.
Instance Method Summary collapse
-
#current_page ⇒ Object
Current page number.
-
#first_page? ⇒ Boolean
First page of the collection?.
-
#initialize(scope, options = {}) ⇒ Paginatable
constructor
A new instance of Paginatable.
-
#last_page? ⇒ Boolean
Last page of the collection?.
-
#next_page ⇒ Object
Next page number in the collection.
- #page(num = 1) ⇒ Object
-
#prev_page ⇒ Object
Previous page number in the collection.
- #scoped ⇒ Object (also: #all, #to_a)
-
#total_count ⇒ Object
total item numbers of scope.
-
#total_pages ⇒ Object
Total number of pages.
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, = {}) @scope = scope @limit_value = ([:limit] || Ckeditor.default_per_page).to_i end |
Instance Attribute Details
#limit_value ⇒ Object (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_value ⇒ Object (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_page ⇒ Object
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?
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?
40 41 42 |
# File 'lib/ckeditor/paginatable.rb', line 40 def last_page? current_page >= total_pages end |
#next_page ⇒ Object
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_page ⇒ Object
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 |
#scoped ⇒ Object 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_count ⇒ Object
total item numbers of scope
50 51 52 |
# File 'lib/ckeditor/paginatable.rb', line 50 def total_count @total_count ||= @scope.count end |
#total_pages ⇒ Object
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 |