Sha256: 89f5f7e4cce633e030ecdfc70ad30b890c254519a4cd0f4bb5fe937e8d1c9296

Contents?: true

Size: 1.61 KB

Versions: 13

Compression:

Stored size: 1.61 KB

Contents

module ActsAsFerret

  # decorator that adds a total_hits accessor and will_paginate compatible 
  # paging support to search result arrays
  class SearchResults < ActsAsFerret::BlankSlate
    reveal :methods
    attr_reader :current_page, :per_page, :total_hits, :total_pages
    alias total_entries total_hits  # will_paginate compatibility
    alias page_count total_pages    # will_paginate backwards compatibility

    def initialize(results, total_hits, current_page = 1, per_page = nil)
      @results = results
      @total_hits = total_hits
      @current_page = current_page
      @per_page = (per_page || total_hits)
      @total_pages   = @per_page > 0 ? (@total_hits / @per_page.to_f).ceil : 0
    end

    def method_missing(symbol, *args, &block)
      @results.send(symbol, *args, &block)
    end

    def respond_to?(name)
      methods.include?(name.to_s) || @results.respond_to?(name)
    end


    # code from here on was directly taken from will_paginate's collection.rb

    # Current offset of the paginated collection. If we're on the first page,
    # it is always 0. If we're on the 2nd page and there are 30 entries per page,
    # the offset is 30. This property is useful if you want to render ordinals
    # besides your records: simply start with offset + 1.
    #
    def offset
      (current_page - 1) * per_page
    end

    # current_page - 1 or nil if there is no previous page
    def previous_page
      current_page > 1 ? (current_page - 1) : nil
    end

    # current_page + 1 or nil if there is no next page
    def next_page
      current_page < total_pages ? (current_page + 1) : nil
    end
  end

end

Version data entries

13 entries across 13 versions & 4 rubygems

Version Path
cehoffman-acts_as_ferret-0.4.4 lib/search_results.rb
jkraemer-acts_as_ferret-0.4.4 lib/search_results.rb
watson-acts_as_ferret-0.4.8.2 lib/search_results.rb
acts_as_ferret-0.5.2 lib/acts_as_ferret/search_results.rb
acts_as_ferret-0.4.8.2 lib/search_results.rb
acts_as_ferret-0.4.8.1 lib/search_results.rb
acts_as_ferret-0.5.1 lib/acts_as_ferret/search_results.rb
acts_as_ferret-0.5 lib/acts_as_ferret/search_results.rb
acts_as_ferret-0.4.8.rails3 lib/search_results.rb
acts_as_ferret-0.4.8 lib/search_results.rb
acts_as_ferret-0.4.7 lib/search_results.rb
acts_as_ferret-0.4.6 lib/search_results.rb
acts_as_ferret-0.4.4 lib/search_results.rb