let ferret index logger inherit the log level of AR::Base's logger
[acts_as_ferret.git] / lib / search_results.rb
blob8f985e5a10587adc04344d6e309e72a42c52aaf3
1 module ActsAsFerret
3   # decorator that adds a total_hits accessor and will_paginate compatible 
4   # paging support to search result arrays
5   class SearchResults
6     attr_reader :current_page, :per_page, :total_hits
8     def initialize(results, total_hits, current_page = 1, per_page = nil)
9       @results = results
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
14     end
16     def method_missing(symbol, *args, &block)
17       @results.send(symbol, *args, &block)
18     end
20     def respond_to?(name)
21       self.methods.include?(name) || @results.respond_to?(name)
22     end
25     # code from here on was directly taken from will_paginate's collection.rb
27     #
28     # The total number of pages.
29     def page_count
30       @total_pages
31     end
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.
37     #
38     def offset
39       (current_page - 1) * per_page
40     end
42     # current_page - 1 or nil if there is no previous page
43     def previous_page
44       current_page > 1 ? (current_page - 1) : nil
45     end
47     # current_page + 1 or nil if there is no next page
48     def next_page
49       current_page < page_count ? (current_page + 1) : nil
50     end
51   end
53 end