mirror of
https://github.com/Growstuff/growstuff.git
synced 2026-05-18 13:38:24 -04:00
Added search method to crops
For now it just does a naive SQL LIKE query. We can make this fancier in future, if we want to.
This commit is contained in:
@@ -147,4 +147,11 @@ class Crop < ActiveRecord::Base
|
||||
end
|
||||
end
|
||||
|
||||
# Crop.search(string)
|
||||
# searches for crops whose names match the string given
|
||||
# just uses SQL LIKE for now, but can be made fancier later
|
||||
def self.search(query)
|
||||
where("name LIKE ?", "%#{query}%")
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -243,4 +243,19 @@ describe Crop do
|
||||
end
|
||||
end
|
||||
|
||||
context "search" do
|
||||
before :each do
|
||||
@mushroom = FactoryGirl.create(:crop, :name => 'mushroom')
|
||||
end
|
||||
it "finds exact matches" do
|
||||
Crop.search('mushroom').should eq [@mushroom]
|
||||
end
|
||||
it "finds approximate matches" do
|
||||
Crop.search('mush').should eq [@mushroom]
|
||||
end
|
||||
it "doesn't find non-matches" do
|
||||
Crop.search('mush').should_not include @crop
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user