Class: ActsAsFerret::SearchResults
- Inherits:
-
Object
- Object
- ActsAsFerret::SearchResults
- Defined in:
- lib/search_results.rb
Overview
decorator that adds a total_hits accessor and will_paginate compatible paging support to search result arrays
Instance Attribute Summary collapse
-
#current_page ⇒ Object
readonly
Returns the value of attribute current_page.
-
#per_page ⇒ Object
readonly
Returns the value of attribute per_page.
-
#total_hits ⇒ Object
readonly
Returns the value of attribute total_hits.
Instance Method Summary collapse
-
#initialize(results, total_hits, current_page = 1, per_page = nil) ⇒ SearchResults
constructor
A new instance of SearchResults.
- #method_missing(symbol, *args, &block) ⇒ Object
-
#next_page ⇒ Object
current_page + 1 or nil if there is no next page.
-
#offset ⇒ Object
Current offset of the paginated collection.
-
#page_count ⇒ Object
The total number of pages.
-
#previous_page ⇒ Object
current_page - 1 or nil if there is no previous page.
- #respond_to?(name) ⇒ Boolean
Constructor Details
#initialize(results, total_hits, current_page = 1, per_page = nil) ⇒ SearchResults
Returns a new instance of SearchResults.
8 9 10 11 12 13 14 |
# File 'lib/search_results.rb', line 8 def initialize(results, total_hits, current_page = 1, per_page = nil) @results = results @total_hits = total_hits @current_page = current_page @per_page = (per_page || total_hits) @total_pages = @per_page > 0 ? (@total_hits / @per_page.to_f).ceil : 0 end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(symbol, *args, &block) ⇒ Object
16 17 18 |
# File 'lib/search_results.rb', line 16 def method_missing(symbol, *args, &block) @results.send(symbol, *args, &block) end |
Instance Attribute Details
#current_page ⇒ Object (readonly)
Returns the value of attribute current_page.
6 7 8 |
# File 'lib/search_results.rb', line 6 def current_page @current_page end |
#per_page ⇒ Object (readonly)
Returns the value of attribute per_page.
6 7 8 |
# File 'lib/search_results.rb', line 6 def per_page @per_page end |
#total_hits ⇒ Object (readonly)
Returns the value of attribute total_hits.
6 7 8 |
# File 'lib/search_results.rb', line 6 def total_hits @total_hits end |
Instance Method Details
#next_page ⇒ Object
current_page + 1 or nil if there is no next page
48 49 50 |
# File 'lib/search_results.rb', line 48 def next_page current_page < page_count ? (current_page + 1) : nil end |
#offset ⇒ Object
Current offset of the paginated collection. If we’re on the first page, it is always 0. If we’re on the 2nd page and there are 30 entries per page, the offset is 30. This property is useful if you want to render ordinals besides your records: simply start with offset + 1.
38 39 40 |
# File 'lib/search_results.rb', line 38 def offset (current_page - 1) * per_page end |
#page_count ⇒ Object
The total number of pages.
29 30 31 |
# File 'lib/search_results.rb', line 29 def page_count @total_pages end |
#previous_page ⇒ Object
current_page - 1 or nil if there is no previous page
43 44 45 |
# File 'lib/search_results.rb', line 43 def previous_page current_page > 1 ? (current_page - 1) : nil end |
#respond_to?(name) ⇒ Boolean
20 21 22 |
# File 'lib/search_results.rb', line 20 def respond_to?(name) self.methods.include?(name) || @results.respond_to?(name) end |