Guest User

Untitled

a guest
Jul 22nd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. +module Sunspot
  2. + # monkey patch for Sunspot v 1.1.1 to add support for solr "start" query parameter
  3. + # we can now optionally add a :start number to offset search results
  4. + # in this scenario, we want the first 3 results for the carousel, and then 5 more
  5. + # the code as is requires the start to be set to 3 and the per_page to 8 for this to work
  6. + # a better solution would be to modify the per_page= method but I was unable to do this
  7. + # with alias_method_chain, BF 12/6
  8. + module Query
  9. + class Pagination
  10. +
  11. + def start_offset=(offset)
  12. + @start_offset = offset
  13. + end
  14. +
  15. + def start_offset
  16. + @start_offset || 0
  17. + end
  18. +
  19. + private
  20. +
  21. + def start_with_start_offset
  22. + start_offset + start_without_start_offset
  23. + end
  24. +
  25. + alias_method_chain(:start,:start_offset)
  26. + end
  27. + end
  28. +
  29. + module DSL
  30. + module Paginatable
  31. + def paginate(options = {})
  32. + page = options.delete(:page)
  33. + per_page = options.delete(:per_page)
  34. + offset = options.delete(:start_offset) # remove term so we don't throw an error when an offset passed in
  35. + raise ArgumentError, "unknown argument #{options.keys.first.inspect} passed to paginate" unless options.empty?
  36. + @query.paginate(page, per_page)
  37. + end
  38. + end
  39. + end
  40. +
  41. +end
  42. +
  43. +
  44. +
Add Comment
Please, Sign In to add comment