Class: ActsAsFerret::SearchResults

Inherits:
BlankSlate
  • Object
show all
Defined in:
lib/acts_as_ferret/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

Instance Method Summary collapse

Methods inherited from BlankSlate

hide

Constructor Details

#initialize(results, total_hits, current_page = 1, per_page = nil) ⇒ SearchResults

Returns a new instance of SearchResults.



10
11
12
13
14
15
16
# File 'lib/acts_as_ferret/search_results.rb', line 10

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



18
19
20
# File 'lib/acts_as_ferret/search_results.rb', line 18

def method_missing(symbol, *args, &block)
  @results.send(symbol, *args, &block)
end

Instance Attribute Details

#current_pageObject (readonly)

Returns the value of attribute current_page.



6
7
8
# File 'lib/acts_as_ferret/search_results.rb', line 6

def current_page
  @current_page
end

#per_pageObject (readonly)

Returns the value of attribute per_page.



6
7
8
# File 'lib/acts_as_ferret/search_results.rb', line 6

def per_page
  @per_page
end

#total_hitsObject (readonly) Also known as: total_entries

Returns the value of attribute total_hits.



6
7
8
# File 'lib/acts_as_ferret/search_results.rb', line 6

def total_hits
  @total_hits
end

#total_pagesObject (readonly) Also known as: page_count

Returns the value of attribute total_pages.



6
7
8
# File 'lib/acts_as_ferret/search_results.rb', line 6

def total_pages
  @total_pages
end

Instance Method Details

#next_pageObject

current_page + 1 or nil if there is no next page



44
45
46
# File 'lib/acts_as_ferret/search_results.rb', line 44

def next_page
  current_page < total_pages ? (current_page + 1) : nil
end

#offsetObject

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.



34
35
36
# File 'lib/acts_as_ferret/search_results.rb', line 34

def offset
  (current_page - 1) * per_page
end

#previous_pageObject

current_page - 1 or nil if there is no previous page



39
40
41
# File 'lib/acts_as_ferret/search_results.rb', line 39

def previous_page
  current_page > 1 ? (current_page - 1) : nil
end

#respond_to?(name) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/acts_as_ferret/search_results.rb', line 22

def respond_to?(name)
  methods.include?(name.to_s) || @results.respond_to?(name)
end