Class: DmPagination::Pagination
- Inherits:
-
Object
- Object
- DmPagination::Pagination
show all
- Defined in:
- lib/dm-pagination/pagination.rb
Instance Method Summary
collapse
Constructor Details
#initialize(collection, options) ⇒ Pagination
Returns a new instance of Pagination.
3
4
5
6
7
8
9
|
# File 'lib/dm-pagination/pagination.rb', line 3
def initialize(collection, options)
@page = (options[:page] || 1).to_i
@per_page = (options[:per_page] || 10).to_i
@proxy_collection = collection
@collection = collection.all(
:offset => (@page - 1)*@per_page, :limit => @per_page)
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
36
37
38
|
# File 'lib/dm-pagination/pagination.rb', line 36
def method_missing(method, *args, &block)
@collection.send(method, *args, &block)
end
|
Instance Method Details
#count ⇒ Object
27
28
29
30
|
# File 'lib/dm-pagination/pagination.rb', line 27
def count
offset = (@page - 1)*@per_page
[0, [@proxy_collection.count - offset, @per_page].min].max
end
|
#num_pages ⇒ Object
15
16
17
|
# File 'lib/dm-pagination/pagination.rb', line 15
def num_pages
(@proxy_collection.count + @per_page - 1) / @per_page
end
|
#page ⇒ Object
11
12
13
|
# File 'lib/dm-pagination/pagination.rb', line 11
def page
@page
end
|
#pages(window = 5, left = 2, right = 2) ⇒ Object
19
20
21
22
23
24
25
|
# File 'lib/dm-pagination/pagination.rb', line 19
def pages(window = 5, left = 2, right = 2)
return [] if num_pages <= 1
(1..num_pages).inject([]) do |result, i|
i <= left || (num_pages - i) < right || (i-page).abs < window ?
result << i : (result.last.nil? ? result : result << nil)
end
end
|
#respond_to?(*args, &block) ⇒ Boolean
32
33
34
|
# File 'lib/dm-pagination/pagination.rb', line 32
def respond_to?(*args, &block)
super || @collection.send(:respond_to?, *args, &block)
end
|
#to_json ⇒ Object
40
41
42
|
# File 'lib/dm-pagination/pagination.rb', line 40
def to_json
@collection.to_json
end
|