Skip to content

Instantly share code, notes, and snippets.

@shanna
Created March 10, 2009 04:54
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 shanna/76741 to your computer and use it in GitHub Desktop.
Save shanna/76741 to your computer and use it in GitHub Desktop.
module Blah
include DataMapper::SphinxResource
# repository(:default) properties
# repository(:search) sphinx properties
#--
# Pagination provided by dm-is-paginated
# http://github.com/lholden/dm-is-paginated/tree
# You could just as easily brew the pagination yourself, it's very simple:
# http://github.com/lholden/dm-is-paginated/blob/670509c97d6fe298d2314dafbe1db91eff759ee5/lib/dm-is-paginated/is/paginated.rb
def self.search(options)
page = options.delete(:page) || 1
per_page = options.delete(:per_page) || 5
# Search sphinx. This is an all search since we need the total for the page_count.
docs = repository(:search){ self.all(options) }
ids = docs.map{|doc| doc[:id]}
page_count = (ids.size.to_f / per_page).ceil
[page_count, all(:id => ids, :limit => per_page, :offset => (page - 1) * per_page)]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment