Skip to content

Commit

Permalink
Merge branch 'master' of github.com:grantr/rubberband
Browse files Browse the repository at this point in the history
  • Loading branch information
grantr committed Jan 8, 2013
2 parents 3696fd9 + fa05469 commit 1f0c24f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
11 changes: 11 additions & 0 deletions lib/elasticsearch/client/hits.rb
Expand Up @@ -37,13 +37,24 @@ def per_page
def total_pages
(total_entries / per_page.to_f).ceil
end

def offset
per_page * (current_page - 1)
end

alias_method :page_count, :total_pages

# Kaminari support
alias_method :limit_value, :per_page
alias_method :num_pages, :total_pages
alias_method :offset_value, :offset
end


class Hits
include Pagination
attr_reader :hits, :total_entries, :_shards, :response, :facets, :scroll_id
alias_method :total_count, :total_entries

def initialize(response, options={})
@response = response
Expand Down
2 changes: 1 addition & 1 deletion lib/elasticsearch/version.rb
@@ -1,3 +1,3 @@
module ElasticSearch
VERSION = "0.9.5"
VERSION = "0.9.6"
end
10 changes: 9 additions & 1 deletion spec/hits_spec.rb
Expand Up @@ -27,7 +27,8 @@

it { should respond_to(:response) }

its(:total_entries) { should == response["hits"]["hits"].size }
its(:total_entries) { should == response["hits"]["hits"].size } # will_paginate
its(:total_count) { should == response["hits"]["hits"].size } # kaminari

it "should instantiate hits in order" do
response["hits"]["hits"].each_with_index do |hit, i|
Expand Down Expand Up @@ -58,10 +59,17 @@

subject { described_class.new(response, {:page => page, :per_page => per_page}) }

# will_paginate
its(:total_pages) { should == (response["hits"]["total"] / per_page.to_f).ceil }
its(:next_page) { should == (page + 1) }
its(:previous_page) { should == (page - 1) }
its(:current_page) { should == page }
its(:per_page) { should == per_page }
its(:offset) { should == per_page * (page - 1) }

# kaminari
its(:limit_value) { should == per_page }
its(:num_pages) { should == (response["hits"]["total"] / per_page.to_f).ceil }
its(:offset_value) { should == per_page * (page - 1) }
end
end

0 comments on commit 1f0c24f

Please sign in to comment.