Class: DmPagination::Pagination
- Inherits:
-
Object
- Object
- DmPagination::Pagination
- Defined in:
- lib/dm-pagination/pagination.rb
Instance Attribute Summary collapse
-
#num_pages ⇒ Object
readonly
Returns the value of attribute num_pages.
-
#page ⇒ Object
readonly
Returns the value of attribute page.
Instance Method Summary collapse
- #count ⇒ Object
-
#initialize(collection, options) ⇒ Pagination
constructor
A new instance of Pagination.
- #method_missing(method, *args, &block) ⇒ Object
- #pages(window = 5, left = 2, right = 2) ⇒ Object
- #respond_to?(*args, &block) ⇒ Boolean
Constructor Details
#initialize(collection, options) ⇒ Pagination
Returns a new instance of Pagination.
5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/dm-pagination/pagination.rb', line 5 def initialize(collection, ) @proxy_collection = collection order = [:order] @page = ([:page] || 1).to_i @per_page = ([:per_page] || 10).to_i @num_pages = (@proxy_collection.count + @per_page - 1) / @per_page @offset = (@page - 1)*@per_page @collection = collection.all(:offset => @offset, :limit => @per_page) @collection = @collection.all(:order => order) if order end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
34 35 36 |
# File 'lib/dm-pagination/pagination.rb', line 34 def method_missing(method, *args, &block) @collection.send(method, *args, &block) end |
Instance Attribute Details
#num_pages ⇒ Object (readonly)
Returns the value of attribute num_pages.
3 4 5 |
# File 'lib/dm-pagination/pagination.rb', line 3 def num_pages @num_pages end |
#page ⇒ Object (readonly)
Returns the value of attribute page.
3 4 5 |
# File 'lib/dm-pagination/pagination.rb', line 3 def page @page end |
Instance Method Details
#count ⇒ Object
26 27 28 |
# File 'lib/dm-pagination/pagination.rb', line 26 def count [0, [@proxy_collection.count - @offset, @per_page].min].max end |
#pages(window = 5, left = 2, right = 2) ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/dm-pagination/pagination.rb', line 18 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
30 31 32 |
# File 'lib/dm-pagination/pagination.rb', line 30 def respond_to?(*args, &block) super || @collection.send(:respond_to?, *args, &block) end |