Skip to content

Instantly share code, notes, and snippets.

@bf4
Created February 18, 2011 19:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bf4/834227 to your computer and use it in GitHub Desktop.
Save bf4/834227 to your computer and use it in GitHub Desktop.
+module Sunspot
+ # monkey patch for Sunspot v 1.1.1 to add support for solr "start" query parameter
+ # we can now optionally add a :start number to offset search results
+ # in this scenario, we want the first 3 results for the carousel, and then 5 more
+ # the code as is requires the start to be set to 3 and the per_page to 8 for this to work
+ # a better solution would be to modify the per_page= method but I was unable to do this
+ # with alias_method_chain, BF 12/6
+ module Query
+ class Pagination
+
+ def start_offset=(offset)
+ @start_offset = offset
+ end
+
+ def start_offset
+ @start_offset || 0
+ end
+
+ private
+
+ def start_with_start_offset
+ start_offset + start_without_start_offset
+ end
+
+ alias_method_chain(:start,:start_offset)
+ end
+ end
+
+ module DSL
+ module Paginatable
+ def paginate(options = {})
+ page = options.delete(:page)
+ per_page = options.delete(:per_page)
+ offset = options.delete(:start_offset) # remove term so we don't throw an error when an offset passed in
+ raise ArgumentError, "unknown argument #{options.keys.first.inspect} passed to paginate" unless options.empty?
+ @query.paginate(page, per_page)
+ end
+ end
+ end
+
+end
+
+
+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment