Skip to content

Instantly share code, notes, and snippets.

@aermolaev
Created September 30, 2010 13:39
Show Gist options
  • Select an option

  • Save aermolaev/604581 to your computer and use it in GitHub Desktop.

Select an option

Save aermolaev/604581 to your computer and use it in GitHub Desktop.

Revisions

  1. aermolaev created this gist Sep 30, 2010.
    22 changes: 22 additions & 0 deletions ferret_pagination.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    module ActsAsFerret
    class SearchResults
    # To prevent WillPaginate warning
    attr_reader :total_pages
    end

    module ClassMethods
    def paginate_search(query, options = {}, find_options = {})
    page, per_page = wp_parse_options(options)
    offset = (page.to_i - 1) * per_page
    hits = total_hits(query)
    hits = 1 if hits == 0

    f_options = find_options.merge(:offset => offset, :limit => per_page)
    result = find_with_ferret(query, { :offset => 0, :limit => hits }, f_options)

    WillPaginate::Collection.new(page, per_page, result.total_hits).tap do |pager|
    pager.replace(result)
    end
    end
    end
    end