Skip to content

Loading…

This will allow searching in a serialized hash #37

Merged
merged 5 commits into from

2 participants

@ekampp

This allows the hash-syntax search fields to also search in serialized hashes.

search_fields { info: :name} 

Or if you want to search in multiple keys in the hash:

search_fields { info: [ :name, :info ] }

Will search in the serialized hash field, info.

@mauriciozaffari

Thanks for tackling it down @ekampp!

I need you to do two things: remove the .rvmrc file from the commit and write a spec for it, this way I can merge it.

Thanks in advance!

@ekampp

Ups. Your right, I forgot I left that .rvmrc file in there ;) I will take that out.

I looked through the tests, and I'm not quite sure, where my tests should go. I ran yours, and they all passed. So I didn't break anything.

@mauriciozaffari

You just need to add an Hash field in spec/models/products and then spec it in https://github.com/mauriciozaffari/mongoid_search/blob/master/spec/mongoid_search_spec.rb

This should be good enough.

@ekampp

@mauriciozaffari I added the tests. Can you please verify, that everything is in order :)

@mauriciozaffari mauriciozaffari merged commit 258e9b3 into mauriciozaffari:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 2, 2012
  1. @ekampp
  2. @ekampp
  3. @ekampp

    Removing old rvmrc file

    ekampp committed
Commits on Jun 4, 2012
  1. @ekampp
Commits on Jun 5, 2012
  1. @ekampp

    Adding tests

    ekampp committed
This page is out of date. Refresh to see the latest.
Showing with 40 additions and 6 deletions.
  1. +3 −0 .gitignore
  2. +6 −0 lib/mongoid_search/util.rb
  3. +3 −1 spec/models/product.rb
  4. +28 −5 spec/mongoid_search_spec.rb
View
3 .gitignore
@@ -10,6 +10,9 @@ tmtags
\#*
.\#*
+## RVM
+.rvmrc
+
## VIM
*.swp
View
6 lib/mongoid_search/util.rb
@@ -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
View
4 spec/models/product.rb
@@ -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
View
33 spec/mongoid_search_spec.rb
@@ -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
@@ -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"] }
@@ -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
@@ -157,5 +180,5 @@
Product.index_keywords!.should_not include(false)
end
-
+
end
Something went wrong with that request. Please try again.