Skip to content

Commit

Permalink
Search now works for fields in referenced_in
Browse files Browse the repository at this point in the history
  • Loading branch information
mauriciozaffari committed Dec 7, 2010
1 parent 2ffc545 commit c0552b6
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/mongoid_search/keywords_extractor.rb
@@ -1,6 +1,6 @@
class KeywordsExtractor
def self.extract(text)
return if text.blank?
return [] if text.blank?
text.mb_chars.normalize(:kd).to_s.gsub(/[^\x00-\x7F]/,'').downcase.split(/[\s\.\-_:;'",]+/)
end
end
9 changes: 8 additions & 1 deletion lib/mongoid_search/mongoid_search.rb
Expand Up @@ -28,7 +28,14 @@ def search(query, options={})
def set_keywords
self._keywords = self.search_fields.map do |field|
if field.is_a?(Hash)
field.keys.map { |key| self.send(key).map(&field[key]).map { |t| KeywordsExtractor.extract t } }
field.keys.map do |key|
attribute = self.send(key)
if attribute.is_a?(Array)
attribute.map(&field[key]).map { |t| KeywordsExtractor.extract t }
else
KeywordsExtractor.extract(attribute.send(field[key]))
end
end
else
KeywordsExtractor.extract(self.send(field))
end
Expand Down
8 changes: 4 additions & 4 deletions spec/keywords_extractor_spec.rb
Expand Up @@ -2,8 +2,8 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')

describe KeywordsExtractor do
it "should return if no text is passed" do
KeywordsExtractor.extract("").should == nil
it "should return an empty array if no text is passed" do
KeywordsExtractor.extract("").should == []
end

it "should return an array of keywords" do
Expand All @@ -22,7 +22,7 @@
KeywordsExtractor.extract("CaFé").should == ["cafe"]
end

it "should split whitespaces, hifens, dots and underlines" do
KeywordsExtractor.extract("CaFé-express.com delicious").should == ["cafe", "express", "com", "delicious"]
it "should split whitespaces, hifens, dots, underlines, etc.." do
KeywordsExtractor.extract("CaFé-express.com delicious;come visit, and 'win' an \"iPad\"").should == ["cafe", "express", "com", "delicious", "come", "visit", "and", "win", "an", "ipad"]
end
end
6 changes: 6 additions & 0 deletions spec/models/category.rb
@@ -0,0 +1,6 @@
class Category
include Mongoid::Document
field :name

references_many :products
end
3 changes: 2 additions & 1 deletion spec/models/product.rb
Expand Up @@ -5,6 +5,7 @@ class Product
field :name

references_many :tags
referenced_in :category

search_in :brand, :name, :tags => :name
search_in :brand, :name, :tags => :name, :category => :name
end
8 changes: 6 additions & 2 deletions spec/mongoid_search_spec.rb
Expand Up @@ -5,11 +5,11 @@
describe Mongoid::Search do

before(:each) do
@product = Product.create :brand => "Apple", :name => "iPhone", :tags => ["Amazing", "Awesome", "Olé"].map { |tag| Tag.new(:name => tag) }
@product = Product.create :brand => "Apple", :name => "iPhone", :tags => ["Amazing", "Awesome", "Olé"].map { |tag| Tag.new(:name => tag) }, :category => Category.new(:name => "Mobile")
end

it "should set the _keywords field" do
@product._keywords.should == ["amazing", "apple", "awesome", "iphone", "ole"]
@product._keywords.should == ["amazing", "apple", "awesome", "iphone", "mobile", "ole"]
end

it "should return results in search" do
Expand Down Expand Up @@ -46,4 +46,8 @@
it "should not return results when all words do not match, passing :match => :all to .search" do
Product.search("apple motorola", :match => :all).size.should == 0
end

it "should return no results when a blank search is made" do
Product.search("").size.should == 0
end
end

0 comments on commit c0552b6

Please sign in to comment.