diff --git a/README.markdown b/README.markdown index dcb6d07..a61dc2a 100644 --- a/README.markdown +++ b/README.markdown @@ -10,7 +10,7 @@ Usage - As gem ` sudo gem install grosser-solr_collection --source http://gems.github.com ` Example: - options = {:limit=>10, :offset=>100} + options = {:limit=>10, :offset=>100, :facets=>{....}, .... } results = MyModel.find_by_solr("abc", options) rescue [] results = SolrCollection.new(results, options) diff --git a/lib/solr_collection.rb b/lib/solr_collection.rb index 2681643..4b86f18 100644 --- a/lib/solr_collection.rb +++ b/lib/solr_collection.rb @@ -6,8 +6,17 @@ class SolrCollection MAPPED_FIELDS = [:facets, :spellcheck, {:total=>:total_entries}] def initialize(solr_result, options={}) - @data = options + options = options.dup + #solr result has results in an array called results + results = if solr_result.respond_to?(:results) + solr_result.results + else + solr_result + end + + #get fields from solr_result or set them to nil + @solr_data = {} MAPPED_FIELDS.each do |fields| if fields.is_a? Hash solr_field = fields.keys.first @@ -16,36 +25,43 @@ def initialize(solr_result, options={}) solr_field = collection_field = fields end - if not @data[collection_field] - @data[collection_field] = if solr_result.respond_to?(solr_field) - solr_result.send(solr_field) - else - nil - end + @solr_data[collection_field] = if solr_result.respond_to?(solr_field) + solr_result.send(solr_field) + else + nil end end + @solr_data[:total_entries] ||= results.size - #solr result has results in an array called results - results = if solr_result.respond_to?(:results) - solr_result.results - else - solr_result - end + #build will_paginate collection from given options + options = fill_page_and_per_page(options) @subject = WillPaginate::Collection.new( - options.delete(:page) || 1, - options.delete(:per_page) || 24, - options.delete(:total_entries) || results.size + options[:page], + options[:per_page], + options[:total_entries] ) @subject.replace(results) end private + + def fill_page_and_per_page(options) + options[:per_page] ||= options[:limit] || 10 + if options[:page].to_s.empty? + options[:page] = if options[:offset] + (options[:offset] / options[:per_page]) + 1 + else + 1 + end + end + options + end def method_missing(method, *args, &block) method = method.to_sym - if @data.key? method - @data[method] + if @solr_data.key? method + @solr_data[method] else @subject.send(method, *args, &block) end diff --git a/spec/solr_collection_spec.rb b/spec/solr_collection_spec.rb index 2ac3c3d..b9ab9ba 100644 --- a/spec/solr_collection_spec.rb +++ b/spec/solr_collection_spec.rb @@ -7,11 +7,6 @@ s[1].should == 2 end - it "stores additional params as methods" do - s = SolrCollection.new([1,2,3], :facets=>1) - s.facets.should == 1 - end - it "behaves like a will_paginate collection" do s = SolrCollection.new([1,2,3], :page=>1, :per_page=>2, :total_entries=>20) s.total_pages.should == 10 @@ -22,23 +17,31 @@ end it "knows per_page" do - SolrCollection.new([1,2,3]).per_page.should == 24 + SolrCollection.new([1,2,3]).per_page.should == 10 end it "knows total_entries" do SolrCollection.new([1,2,3]).total_entries.should == 3 end - it "can overwrite page" do + it "can set page" do SolrCollection.new([1,2,3], :page=>3).current_page.should == 3 end - it "can overwrite per_page" do + it "can set per_page" do SolrCollection.new([1,2,3], :per_page=>3).per_page.should == 3 end - it "can overwrite total_entries" do - SolrCollection.new([1,2,3], :total_entries=>33).total_entries.should == 33 + 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 limit/offset" do + SolrCollection.new([1], :limit=>2, :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 "knows factes" do @@ -59,10 +62,10 @@ def a.total; 22;end SolrCollection.new(a).total_entries.should == 22 end - it "can overwrite total of collection" do + it "does not overwrite total" do a = [] def a.total; 22;end - SolrCollection.new(a, :total_entries=>33).total_entries.should == 33 + SolrCollection.new(a, :total_entries=>33).total_entries.should == 22 end it "uses results as subject" do @@ -70,4 +73,10 @@ def a.total; 22;end def a.results; [1,2,3];end SolrCollection.new(a)[2].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} + end end \ No newline at end of file