Module: PaginateCached::ControllerHelpers

Defined in:
lib/paginate_cached/paginate_cached.rb

Instance Method Summary collapse

Instance Method Details

#paginate_cached(args = {}) ⇒ Object

Arguments: This function takes :page as the page number, it defaults to 0. The number of results is passed in as :number_of_results. It defaults to 20. The variable used to hold the current state in the pagination should be placed in the :search_id variable.

The actual code which produces the result is passed in as a block.

Results: This returns a hash in which :results will hold the results, :search_id will hold the search id, next_page: will hold the new page number, and has_more_results will hold whether there are more results.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/paginate_cached/paginate_cached.rb', line 15

def paginate_cached args={}
       page = args[:page] || 1
       number_of_results = args[:number_of_results] || 25
       search_id = args[:search_id]
       offset = (page-1)*number_of_results
  
       unless search_id and results = Rails.cache.read(search_id) then
             results = yield
             search_id = "#{results.hash}#{Time.now.to_i}"
             Rails.cache.write search_id, results, expires_in: 15.minutes
       end 
  
       return {
               results: results[offset, number_of_results],
               search_id: search_id, 
               next_page: page+1, 
               has_more_results: results[((page)*number_of_results)+1] != nil 
              }   
  
end