3 # decorator that adds a total_hits accessor and will_paginate compatible
4 # paging support to search result arrays
5 class SearchResults < ActsAsFerret::BlankSlate
7 attr_reader :current_page, :per_page, :total_hits
8 alias total_entries total_hits # will_paginate compatibility
10 def initialize(results, total_hits, current_page = 1, per_page = nil)
12 @total_hits = total_hits
13 @current_page = current_page
14 @per_page = (per_page || total_hits)
15 @total_pages = @per_page > 0 ? (@total_hits / @per_page.to_f).ceil : 0
18 def method_missing(symbol, *args, &block)
19 @results.send(symbol, *args, &block)
23 methods.include?(name.to_s) || @results.respond_to?(name)
27 # code from here on was directly taken from will_paginate's collection.rb
30 # The total number of pages.
35 # Current offset of the paginated collection. If we're on the first page,
36 # it is always 0. If we're on the 2nd page and there are 30 entries per page,
37 # the offset is 30. This property is useful if you want to render ordinals
38 # besides your records: simply start with offset + 1.
41 (current_page - 1) * per_page
44 # current_page - 1 or nil if there is no previous page
46 current_page > 1 ? (current_page - 1) : nil
49 # current_page + 1 or nil if there is no next page
51 current_page < page_count ? (current_page + 1) : nil