Skip to content

Commit

Permalink
Merge pull request #37 from ekampp/master
Browse files Browse the repository at this point in the history
This will allow searching in a serialized hash
  • Loading branch information
mauriciozaffari committed Jun 5, 2012
2 parents e493a9e + fcd0a5c commit 258e9b3
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ tmtags
\#*
.\#*

## RVM
.rvmrc

## VIM
*.swp

Expand Down
6 changes: 6 additions & 0 deletions lib/mongoid_search/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ def self.keywords(klass, field, stem_keywords, ignore_list)
else
attribute.map(&method).map { |t| Util.normalize_keywords t, stem_keywords, ignore_list }
end
elsif attribute.is_a?(Hash)
if method.is_a?(Array)
method.map {|m| Util.normalize_keywords attribute[m.to_sym], stem_keywords, ignore_list }
else
Util.normalize_keywords(attribute[method.to_sym], stem_keywords, ignore_list)
end
else
Util.normalize_keywords(attribute.send(method), stem_keywords, ignore_list)
end
Expand Down
4 changes: 3 additions & 1 deletion spec/models/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ class Product
field :brand
field :name
field :attrs, :type => Array
field :info, :type => Hash

references_many :tags
referenced_in :category
embeds_many :subproducts

search_in :brand, :name, :outlet, :attrs, :tags => :name, :category => :name, :subproducts => [:brand, :name]
search_in :brand, :name, :outlet, :attrs, :tags => :name, :category => :name,
:subproducts => [:brand, :name], :info => [ :summary, :description ]
end
33 changes: 28 additions & 5 deletions spec/mongoid_search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,30 @@
:name => "iPhone",
:tags => ["Amazing", "Awesome", "Olé"].map { |tag| Tag.new(:name => tag) },
:category => Category.new(:name => "Mobile"),
:subproducts => [Subproduct.new(:brand => "Apple", :name => "Craddle")]
:subproducts => [Subproduct.new(:brand => "Apple", :name => "Craddle")],
:info => { :summary => "Info-summary",
:description => "Info-description"}
end

describe "Serialized hash fields" do
context "when the hash is populated" do
it "should return the product" do
Product.search("Info-summary").first.should eq @product
Product.search("Info-description").first.should eq @product
end
end

context "when the hash is empty" do
before(:each) do
@product.info = nil
@product.save
end

it "should not return the product" do
Product.search("Info-description").size.should eq 0
Product.search("Info-summary").size.should eq 0
end
end
end

context "utf-8 characters" do
Expand Down Expand Up @@ -40,7 +63,7 @@
lambda { Product.create! }.should_not raise_error
end
end

subject { Product.create :brand => "Apple", :name => "iPhone" }

its(:_keywords) { should == ["apple", "iphone"] }
Expand All @@ -66,13 +89,13 @@
it "should set the _keywords field with stemmed words if stem is enabled" do
Product.stem_keywords = true
@product.save!
@product._keywords.should == ["amaz", "appl", "awesom", "craddl", "iphon", "mobil", "ol"]
@product._keywords.sort.should == ["amaz", "appl", "awesom", "craddl", "iphon", "mobil", "ol", "info", "descript", "summari"].sort
end

it "should ignore keywords in an ignore list" do
Product.ignore_list = YAML.load(File.open(File.dirname(__FILE__) + '/config/ignorelist.yml'))["ignorelist"]
@product.save!
@product._keywords.should == ["apple", "craddle", "iphone", "mobile", "ole"]
@product._keywords.sort.should == ["apple", "craddle", "iphone", "mobile", "ole", "info", "description", "summary"].sort
end

it "should incorporate numbers as keywords" do
Expand Down Expand Up @@ -157,5 +180,5 @@
Product.index_keywords!.should_not include(false)
end


end

0 comments on commit 258e9b3

Please sign in to comment.