Module: Sphinxsearchlogic::Search::Pagination

Included in:
Sphinxsearchlogic::Search
Defined in:
lib/sphinxsearchlogic.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#max_matchesObject

Returns a set max_matches or 1000.



52
53
54
# File 'lib/sphinxsearchlogic.rb', line 52

def max_matches
  @max_matches || 1000
end

#pageObject



66
67
68
69
70
71
72
73
74
# File 'lib/sphinxsearchlogic.rb', line 66

def page
  page = (@page || 1).to_i
  page = 1 if page < 1 # Fixes pages like -1 and 0.
  # Fix riddle error.. we always return the last page? Think should be handled by application!
  # However this isn't yet the case for pages > total_results.
  # raise OutofboundsError.new(page, per_page) if page > last_page
  page = last_page if page > last_page
  page
end

#per_pageObject

Returns 20 by default (ThinkingSphinx/Riddle default)



77
78
79
80
81
# File 'lib/sphinxsearchlogic.rb', line 77

def per_page
  per_page = (@per_page || default_per_page).to_i
  per_page = default_per_page if per_page < 1 # Fixes per_page like -1 and 0.
  per_page
end

Instance Method Details

#default_per_pageObject



45
46
47
48
49
# File 'lib/sphinxsearchlogic.rb', line 45

def default_per_page
  default_per_page = 20
  default_per_page = klass.per_page if klass.respond_to?(:per_page)
  default_per_page
end

#last_pageObject

Returns the last page we have in this search based on the max_matches. So we don’t get a Riddle error for an offset that is too high.



58
59
60
# File 'lib/sphinxsearchlogic.rb', line 58

def last_page
  (max_matches.to_f / per_page).ceil
end

#offsetObject



62
63
64
# File 'lib/sphinxsearchlogic.rb', line 62

def offset
  (page-1)*per_page
end

#pagination_optionsObject



83
84
85
86
87
88
# File 'lib/sphinxsearchlogic.rb', line 83

def pagination_options
  options = {}
  options[:page] = page
  options[:per_page] = per_page
  options
end