Skip to content

Commit

Permalink
refactoring+grouping specs, also give resutls.size to will_paginate
Browse files Browse the repository at this point in the history
  • Loading branch information
grosser committed Aug 13, 2009
1 parent 0a202e5 commit 4ee1461
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 52 deletions.
4 changes: 2 additions & 2 deletions lib/solr_collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ def initialize(solr_result, options={})
nil
end
end
@solr_data[:total_entries] ||= results.size
@solr_data[:total_entries] ||= options[:total_entries] || results.size

#build will_paginate collection from given options
options = fill_page_and_per_page(options)

@subject = WillPaginate::Collection.new(
options[:page],
options[:per_page],
options[:total_entries]
@solr_data[:total_entries]
)
@subject.replace(results)
end
Expand Down
115 changes: 65 additions & 50 deletions spec/solr_collection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,72 +11,87 @@
s = SolrCollection.new([1,2,3], :page=>1, :per_page=>2, :total_entries=>20)
s.total_pages.should == 10
end

it "knows page" do
SolrCollection.new([1,2,3]).current_page.should == 1

it "does not modify options" do
a = {:per_page=>2, :offset=>2}
SolrCollection.new([1], a).current_page.should == 2
a.should == {:per_page=>2, :offset=>2}
end

it "knows per_page" do
SolrCollection.new([1,2,3]).per_page.should == 10
end
describe "pages" do
it "does not know page" do
lambda{ SolrCollection.new([1,2,3]).page }.should raise_error
end

it "knows total_entries" do
SolrCollection.new([1,2,3]).total_entries.should == 3
end
it "knows current_page" do
SolrCollection.new([1,2,3]).current_page.should == 1
end

it "can set page" do
SolrCollection.new([1,2,3], :page=>3).current_page.should == 3
end
it "knows per_page" do
SolrCollection.new([1,2,3]).per_page.should == 10
end

it "can set per_page" do
SolrCollection.new([1,2,3], :per_page=>3).per_page.should == 3
end
it "can set page" do
SolrCollection.new([1,2,3], :page=>3).current_page.should == 3
end

it "uses per_page when per_page and limit are given" do
SolrCollection.new([1], :per_page=>2, :limit=>20, :offset=>10).current_page.should == 6
it "can set per_page" do
SolrCollection.new([1,2,3], :per_page=>3).per_page.should == 3
end
end

it "understands limit/offset" do
SolrCollection.new([1], :limit=>2, :offset=>10).current_page.should == 6
end
describe "limit and offset" do
it "uses per_page when per_page and limit are given" do
SolrCollection.new([1], :per_page=>2, :limit=>20, :offset=>10).current_page.should == 6
end

it "understands per_page/offset" do
SolrCollection.new([1], :per_page=>2, :offset=>2).current_page.should == 2
end
it "understands limit/offset" do
SolrCollection.new([1], :limit=>2, :offset=>10).current_page.should == 6
end

it "knows factes" do
SolrCollection.new([]).facets.should == nil
it "understands per_page/offset" do
SolrCollection.new([1], :per_page=>2, :offset=>2).current_page.should == 2
end
end

it "knows spellcheck" do
SolrCollection.new([]).spellcheck.should == nil
end
describe "solr fields" do
it "knows factes" do
SolrCollection.new([]).facets.should == nil
end

it "does not know non-solr-method" do
lambda{ SolrCollection.new([]).fooo }.should raise_error
end
it "knows spellcheck" do
SolrCollection.new([]).spellcheck.should == nil
end

it "uses total from the collection" do
a = []
def a.total; 22;end
SolrCollection.new(a).total_entries.should == 22
end
it "does not know non-solr-method" do
lambda{ SolrCollection.new([]).fooo }.should raise_error
end

it "does not overwrite total" do
a = []
def a.total; 22;end
SolrCollection.new(a, :total_entries=>33).total_entries.should == 22
end
it "uses results as subject" do
a = []
def a.results; [1,2,3];end
SolrCollection.new(a)[2].should == 3
end

it "uses results as subject" do
a = []
def a.results; [1,2,3];end
SolrCollection.new(a)[2].should == 3
end
it "knows total_entries" do
SolrCollection.new([1,2,3]).total_entries.should == 3
end

it "does not modify options" do
a = {:per_page=>2, :offset=>2}
SolrCollection.new([1], a).current_page.should == 2
a.should == {:per_page=>2, :offset=>2}
it "can get total_entries from an array" do
a = [1,2,3]
SolrCollection.new(a, :per_page=>2).total_pages.should == 2
end

it "can get total_entries from a solr resultset" do
a = []
def a.total; 22;end
SolrCollection.new(a).total_entries.should == 22
end

it "does not overwrite total from solr resultset with given total_entries" do
a = []
def a.total; 22;end
SolrCollection.new(a, :total_entries=>33).total_entries.should == 22
end
end
end

0 comments on commit 4ee1461

Please sign in to comment.