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:
Skud
2014-01-03 21:50:41 +11:00
parent 30c1129cab
commit addd9cbddf
2 changed files with 22 additions and 0 deletions

View File

@@ -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

View File

@@ -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