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
-
#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.
6 7 8 9 |
# File 'lib/ckeditor/paginatable.rb', line 6 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.
4 5 6 |
# File 'lib/ckeditor/paginatable.rb', line 4 def limit_value @limit_value end |
#offset_value ⇒ Object (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_page ⇒ Object
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?
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?
36 37 38 |
# File 'lib/ckeditor/paginatable.rb', line 36 def last_page? current_page >= total_pages end |
#next_page ⇒ Object
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_page ⇒ Object
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 |
#scoped ⇒ Object
16 17 18 |
# File 'lib/ckeditor/paginatable.rb', line 16 def scoped @scope.limit(limit_value).offset(offset_value) end |
#total_count ⇒ Object
total item numbers of scope
46 47 48 |
# File 'lib/ckeditor/paginatable.rb', line 46 def total_count @total_count ||= @scope.count end |
#total_pages ⇒ Object
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 |