3 # decorator that adds a total_hits accessor and will_paginate compatible
4 # paging support to search result arrays
6 attr_reader :current_page, :per_page, :total_hits
8 def initialize(results, total_hits, current_page = 1, per_page = nil)
10 @total_hits = total_hits
11 @current_page = current_page
12 @per_page = (per_page || total_hits)
13 @total_pages = @per_page > 0 ? (@total_hits / @per_page.to_f).ceil : 0
16 def method_missing(symbol, *args, &block)
17 @results.send(symbol, *args, &block)
21 self.methods.include?(name) || @results.respond_to?(name)
25 # code from here on was directly taken from will_paginate's collection.rb
28 # The total number of pages.
33 # Current offset of the paginated collection. If we're on the first page,
34 # it is always 0. If we're on the 2nd page and there are 30 entries per page,
35 # the offset is 30. This property is useful if you want to render ordinals
36 # besides your records: simply start with offset + 1.
39 (current_page - 1) * per_page
42 # current_page - 1 or nil if there is no previous page
44 current_page > 1 ? (current_page - 1) : nil
47 # current_page + 1 or nil if there is no next page
49 current_page < page_count ? (current_page + 1) : nil