Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #67 from lardawge/generic_pagination_support

Generic pagination support
  • Loading branch information...
commit 08dc66b45179d3332987b95467aad70d1f444b84 2 parents 9dc7cb7 + 733f7db
Mat Brown authored
18 sunspot/lib/sunspot/search/abstract_search.rb
View
@@ -1,3 +1,5 @@
+require 'sunspot/search/paginated_collection'
+
module Sunspot
module Search #:nodoc:
@@ -54,7 +56,7 @@ def execute! #:nodoc: deprecated
# WillPaginate::Collection or Array:: Instantiated result objects
#
def results
- @results ||= maybe_will_paginate(verified_hits.map { |hit| hit.instance })
+ @results ||= paginate_collection(verified_hits.map { |hit| hit.instance })
end
#
@@ -85,7 +87,7 @@ def hits(options = {})
Hit.new(doc, highlights_for(doc), self)
end
end
- maybe_will_paginate(hits || [])
+ paginate_collection(hits || [])
end
end
end
@@ -270,17 +272,11 @@ def highlights_for(doc)
end
def verified_hits
- @verified_hits ||= maybe_will_paginate(hits.select { |hit| hit.instance })
+ @verified_hits ||= paginate_collection(hits.select { |hit| hit.instance })
end
- def maybe_will_paginate(collection)
- if defined?(WillPaginate::Collection)
- WillPaginate::Collection.create(@query.page, @query.per_page, total) do |pager|
- pager.replace(collection)
- end
- else
- collection
- end
+ def paginate_collection(collection)
+ PaginatedCollection.new(collection, @query.page, @query.per_page, total)
end
def add_facet(name, facet)
55 sunspot/lib/sunspot/search/paginated_collection.rb
View
@@ -0,0 +1,55 @@
+module Sunspot
+ module Search
+
+ class PaginatedCollection
+ instance_methods.each { |m| undef_method m unless m =~ /^__|instance_eval|object_id/ }
+
+ attr_reader :total_count, :current_page, :per_page
+ alias :total_entries :total_count
+ alias :limit_value :per_page
+
+ def initialize(collection, page, per_page, total)
+ @collection = collection
+ @current_page = page
+ @per_page = per_page
+ @total_count = total
+ end
+
+ def total_pages
+ (total_count.to_f / per_page).ceil
+ end
+ alias :num_pages :total_pages
+
+ def first_page?
+ current_page == 1
+ end
+
+ def last_page?
+ current_page >= total_pages
+ end
+
+ def previous_page
+ current_page > 1 ? (current_page - 1) : nil
+ end
+
+ def next_page
+ current_page < total_pages ? (current_page + 1) : nil
+ end
+
+ def out_of_bounds?
+ current_page > total_pages
+ end
+
+ def offset
+ (current_page - 1) * per_page
+ end
+
+ private
+
+ def method_missing(method, *args, &block)
+ @collection.send(method, *args, &block)
+ end
+
+ end
+ end
+end
44 sunspot/spec/api/search/hits_spec.rb
View
@@ -12,38 +12,18 @@
end.should == [['Post', post_1.id.to_s], ['Post', post_2.id.to_s]]
end
- if ENV['USE_WILL_PAGINATE']
-
- it 'returns search total as attribute of hits' do
- stub_results(Post.new, 4)
- session.search(Post) do
- paginate(:page => 1)
- end.hits.total_entries.should == 4
- end
-
- it 'returns search total as attribute of verified hits' do
- stub_results(Post.new, 4)
- session.search(Post) do
- paginate(:page => 1)
- end.hits(:verify => true).total_entries.should == 4
- end
-
- else
-
- it 'returns vanilla array of hits if WillPaginate is not available' do
- stub_results(Post.new)
- session.search(Post) do
- paginate(:page => 1)
- end.hits.should_not respond_to(:total_entries)
- end
-
- it 'returns vanilla array of verified hits if WillPaginate is not available' do
- stub_results(Post.new)
- session.search(Post) do
- paginate(:page => 1)
- end.hits(:verified => true).should_not respond_to(:total_entries)
- end
-
+ it 'returns search total as attribute of hits' do
+ stub_results(Post.new, 4)
+ session.search(Post) do
+ paginate(:page => 1)
+ end.hits.total_entries.should == 4
+ end
+
+ it 'returns search total as attribute of verified hits' do
+ stub_results(Post.new, 4)
+ session.search(Post) do
+ paginate(:page => 1)
+ end.hits(:verify => true).total_entries.should == 4
end
it 'should return instance from hit' do
26 sunspot/spec/api/search/paginated_collection_spec.rb
View
@@ -0,0 +1,26 @@
+require File.expand_path('spec_helper', File.dirname(__FILE__))
+
+describe "PaginatedCollection" do
+ subject { Sunspot::Search::PaginatedCollection.new [], 1, 10, 20 }
+
+ it { subject.should be_an(Array) }
+
+ context "behaves like a WillPaginate::Collection" do
+ it { subject.total_entries.should eql(20) }
+ it { subject.total_pages.should eql(2) }
+ it { subject.current_page.should eql(1) }
+ it { subject.per_page.should eql(10) }
+ it { subject.previous_page.should be_nil }
+ it { subject.next_page.should eql(2) }
+ it { subject.out_of_bounds?.should_not be_true }
+ it { subject.offset.should eql(0) }
+ end
+
+ context "behaves like Kaminari" do
+ it { subject.total_count.should eql(20) }
+ it { subject.num_pages.should eql(2) }
+ it { subject.limit_value.should eql(10) }
+ it { subject.first_page?.should be_true }
+ it { subject.last_page?.should_not be_true }
+ end
+end
23 sunspot/spec/api/search/results_spec.rb
View
@@ -30,24 +30,11 @@
session.search(Post).results.should == []
end
- if ENV['USE_WILL_PAGINATE']
-
- it 'returns search total as attribute of results' do
- stub_results(Post.new, 4)
- session.search(Post) do
- paginate(:page => 1)
- end.results.total_entries.should == 4
- end
-
- else
-
- it 'returns vanilla array if WillPaginate is not available' do
- stub_results(Post.new)
- session.search(Post) do
- paginate(:page => 1)
- end.results.should_not respond_to(:total_entries)
- end
-
+ it 'returns search total as attribute of results' do
+ stub_results(Post.new, 4)
+ session.search(Post) do
+ paginate(:page => 1)
+ end.results.total_entries.should == 4
end
it 'returns total' do
8 sunspot/spec/spec_helper.rb
View
@@ -5,14 +5,6 @@
require 'ostruct'
require 'sunspot'
-if ENV['USE_WILL_PAGINATE']
- require 'will_paginate'
- require 'will_paginate/collection'
-elsif ENV['USE_KAMINARI']
- module Kaminari
- end
-end
-
require File.join(File.dirname(__FILE__), 'mocks', 'mock_record.rb')
Dir.glob(File.join(File.dirname(__FILE__), 'mocks', '**', '*.rb')).each do |file|
require file unless File.basename(file) == 'mock_record.rb'
2  sunspot/sunspot.gemspec
View
@@ -31,8 +31,6 @@ Gem::Specification.new do |s|
s.add_dependency 'pr_geohash', '~> 1.0'
s.add_development_dependency 'rspec', '~> 1.1'
- s.add_development_dependency 'activesupport'
- s.add_development_dependency 'will_paginate'
s.add_development_dependency 'hanna'
s.rdoc_options << '--webcvs=http://github.com/outoftime/sunspot/tree/master/%s' <<
Please sign in to comment.
Something went wrong with that request. Please try again.