Class: ActsAsFerret::SearchResults
- Inherits:
-
BlankSlate
- Object
- BlankSlate
- 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
(also: #total_entries)
readonly
Returns the value of attribute total_hits.
-
#total_pages ⇒ Object
(also: #page_count)
readonly
Returns the value of attribute total_pages.
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.
-
#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.
11 12 13 14 15 16 17 |
# File 'lib/search_results.rb', line 11 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
19 20 21 |
# File 'lib/search_results.rb', line 19 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.
7 8 9 |
# File 'lib/search_results.rb', line 7 def current_page @current_page end |
#per_page ⇒ Object (readonly)
Returns the value of attribute per_page.
7 8 9 |
# File 'lib/search_results.rb', line 7 def per_page @per_page end |
#total_hits ⇒ Object (readonly) Also known as: total_entries
Returns the value of attribute total_hits.
7 8 9 |
# File 'lib/search_results.rb', line 7 def total_hits @total_hits end |
#total_pages ⇒ Object (readonly) Also known as: page_count
Returns the value of attribute total_pages.
7 8 9 |
# File 'lib/search_results.rb', line 7 def total_pages @total_pages end |
Instance Method Details
#next_page ⇒ Object
current_page + 1 or nil if there is no next page
45 46 47 |
# File 'lib/search_results.rb', line 45 def next_page current_page < total_pages ? (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.
35 36 37 |
# File 'lib/search_results.rb', line 35 def offset (current_page - 1) * per_page end |
#previous_page ⇒ Object
current_page - 1 or nil if there is no previous page
40 41 42 |
# File 'lib/search_results.rb', line 40 def previous_page current_page > 1 ? (current_page - 1) : nil end |
#respond_to?(name) ⇒ Boolean
23 24 25 |
# File 'lib/search_results.rb', line 23 def respond_to?(name) methods.include?(name.to_s) || @results.respond_to?(name) end |