Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

handle non-string input gracefully

  • Loading branch information...
commit f0cfb5930b4edfd8ecbee454e2c813c44cb3bada 1 parent 2fe72c1
Tomas Jogin authored
View
9 lib/naive-search/naive_search_on.rb
@@ -22,7 +22,7 @@ def naive_search_on(*fields)
end
def search_for(query, page_no = 1, page_size = nil)
- words = query.split " "
+ words = query.to_s.split " "
conditions = words.map do |w|
replace_bind_variables("#{self.naive_search_index_field} like ?", ["%#{w}%"])
end.join " OR "
@@ -37,7 +37,7 @@ def search_for(query, page_no = 1, page_size = nil)
end
def relevance_for(query)
- query = query.downcase
+ query = query.to_s.downcase
@naive_relevance ||= {}
return @naive_relevance[query] if @naive_relevance[query]
words = query.split " "
@@ -45,10 +45,9 @@ def relevance_for(query)
score = self.naive_search_fields.map do |field|
content = self.send(field).to_s.downcase
words.map do |w|
- w = w.downcase
- # one point per partial word matches
+ # one point for partial word matches
(content.scan(w).size +
- # one point per partial query matches
+ # one point for partial query matches
content.scan(query).size +
# two points for exact word match
(content == w ? 2 : 0) +
View
4 test/naive_search_test.rb
@@ -45,6 +45,10 @@ class NaiveSearchTest < ActiveSupport::TestCase
relevance_nice_breakfast = Hotel.order("id asc").map{|h| h.relevance_for "nice breakfast" }
assert_equal [1, 1, 2, 4], relevance_nice_breakfast, "should calculate correct relevance for 'nice breakfast'"
+
+ donald = Person.create(:name => "Donald", :surname => "Trump", :description => "rich, comb over")
+ assert_equal 0, donald.relevance_for("")
+ assert_equal 0, donald.relevance_for(100)
end
test "updating" do
Please sign in to comment.
Something went wrong with that request. Please try again.