Skip to content

Commit

Permalink
refactored to not use options as keys, keys should come from resultset
Browse files Browse the repository at this point in the history
  • Loading branch information
grosser committed Aug 12, 2009
1 parent ca38ba6 commit ad1f59b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 31 deletions.
2 changes: 1 addition & 1 deletion README.markdown
Expand Up @@ -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)

Expand Down
52 changes: 34 additions & 18 deletions lib/solr_collection.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down
33 changes: 21 additions & 12 deletions spec/solr_collection_spec.rb
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -59,15 +62,21 @@ 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
a = []
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

0 comments on commit ad1f59b

Please sign in to comment.