Class: Stellr::Search::SearchResults
- Inherits:
-
Object
- Object
- Stellr::Search::SearchResults
- Defined in:
- lib/stellr/search/search_results.rb
Overview
Thin wrapper around an array of search results
Instance Attribute Summary collapse
-
#current_page ⇒ Object
Returns the value of attribute current_page.
-
#per_page ⇒ Object
Returns the value of attribute per_page.
-
#total_hits ⇒ Object
(also: #total_entries)
Returns the value of attribute total_hits.
Instance Method Summary collapse
-
#initialize ⇒ 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
- #to_json ⇒ Object
-
#total_pages ⇒ Object
(also: #page_count)
The total number of pages.
Constructor Details
#initialize ⇒ SearchResults
Returns a new instance of SearchResults.
9 10 11 |
# File 'lib/stellr/search/search_results.rb', line 9 def initialize @results = [] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(symbol, *args, &block) ⇒ Object
22 23 24 |
# File 'lib/stellr/search/search_results.rb', line 22 def method_missing(symbol, *args, &block) @results.send(symbol, *args, &block) end |
Instance Attribute Details
#current_page ⇒ Object
Returns the value of attribute current_page.
6 7 8 |
# File 'lib/stellr/search/search_results.rb', line 6 def current_page @current_page end |
#per_page ⇒ Object
Returns the value of attribute per_page.
6 7 8 |
# File 'lib/stellr/search/search_results.rb', line 6 def per_page @per_page end |
#total_hits ⇒ Object Also known as: total_entries
Returns the value of attribute total_hits.
6 7 8 |
# File 'lib/stellr/search/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
54 55 56 |
# File 'lib/stellr/search/search_results.rb', line 54 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.
44 45 46 |
# File 'lib/stellr/search/search_results.rb', line 44 def offset (current_page - 1) * per_page end |
#previous_page ⇒ Object
current_page - 1 or nil if there is no previous page
49 50 51 |
# File 'lib/stellr/search/search_results.rb', line 49 def previous_page current_page > 1 ? (current_page - 1) : nil end |
#respond_to?(name) ⇒ Boolean
26 27 28 |
# File 'lib/stellr/search/search_results.rb', line 26 def respond_to?(name) self.methods.include?(name) || @results.respond_to?(name) end |
#to_json ⇒ Object
13 14 15 16 17 18 19 20 |
# File 'lib/stellr/search/search_results.rb', line 13 def to_json { :results => @results, :total_hits => total_hits, :current_page => current_page, :per_page => per_page }.to_json end |
#total_pages ⇒ Object Also known as: page_count
The total number of pages.
34 35 36 |
# File 'lib/stellr/search/search_results.rb', line 34 def total_pages @total_pages ||= per_page > 0 ? (total_hits / per_page.to_f).ceil : 0 end |