Browse files

multi_solr_search now returns an empty result set instead of nil if t…

…he parsed query data is nil
  • Loading branch information...
1 parent 6d9812e commit e47a35adbdbd6bdd88536e8aab88ed15ab14d643 Luke Francl committed Dec 17, 2008
Showing with 23 additions and 13 deletions.
  1. +15 −12 lib/class_methods.rb
  2. +7 −0 test/functional/multi_solr_search_test.rb
  3. +1 −1 test/test_helper.rb
View
27 lib/class_methods.rb
@@ -98,19 +98,22 @@ def multi_solr_search(query, options = {})
options.update(:results_format => :objects) unless options[:results_format]
data = parse_query(query, options, models<<")")
result = []
- if data
- docs = data.hits
- return SearchResults.new(:docs => [], :total => 0) if data.total_hits == 0
- if options[:results_format] == :objects
- docs.each{|doc|
- k = doc.fetch('id').first.to_s.split(':')
- result << k[0].constantize.find_by_id(k[1])
- }
- elsif options[:results_format] == :ids
- docs.each{|doc| result << {"id"=>doc.values.pop.to_s}}
- end
- SearchResults.new :docs => result, :total => data.total_hits
+ if data.nil?
+ return SearchResults.new(:docs => [], :total => 0)
end
+
+ docs = data.hits
+ return SearchResults.new(:docs => [], :total => 0) if data.total_hits == 0
+ if options[:results_format] == :objects
+ docs.each{|doc|
+ k = doc.fetch('id').first.to_s.split(':')
+ result << k[0].constantize.find_by_id(k[1])
+ }
+ elsif options[:results_format] == :ids
+ docs.each{|doc| result << {"id"=>doc.values.pop.to_s}}
+ end
+
+ SearchResults.new :docs => result, :total => data.total_hits
end
# returns the total number of documents found in the query specified:
View
7 test/functional/multi_solr_search_test.rb
@@ -36,4 +36,11 @@ def test_returns_no_matches
assert_equal 0, records.total
end
+ def test_search_on_empty_string_does_not_return_nil
+ records = Book.multi_solr_search('', :models => [Movie, Category])
+ assert_not_nil records
+ assert_equal [], records.docs
+ assert_equal 0, records.total
+ end
+
end
View
2 test/test_helper.rb
@@ -7,7 +7,7 @@
RAILS_ENV = 'test' unless defined? RAILS_ENV
require File.dirname(__FILE__) + '/../lib/acts_as_solr'
-require File.dirname(__FILE__) + '/../config/environment.rb'
+require File.dirname(__FILE__) + '/../config/solr_environment.rb'
# Load Models
models_dir = File.join(File.dirname( __FILE__ ), 'models')

0 comments on commit e47a35a

Please sign in to comment.