adds search to Booth model
This commit is contained in:
@@ -4,5 +4,10 @@ class Poll
|
||||
has_many :polls, through: :booth_assignments
|
||||
|
||||
validates :name, presence: true, uniqueness: true
|
||||
|
||||
def self.search(terms)
|
||||
return Booth.none if terms.blank?
|
||||
Booth.where("name ILIKE ? OR location ILIKE ?", "%#{terms}%", "%#{terms}%")
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -13,4 +13,15 @@ describe :booth do
|
||||
expect(booth).to_not be_valid
|
||||
end
|
||||
|
||||
describe "#search" do
|
||||
it "should find booths searching by name or location" do
|
||||
booth1 = create(:poll_booth, name: "Booth number 1", location: "City center")
|
||||
booth2 = create(:poll_booth, name: "Central", location: "Town hall")
|
||||
|
||||
expect(Poll::Booth.search("number")).to eq([booth1])
|
||||
expect(Poll::Booth.search("hall")).to eq([booth2])
|
||||
expect(Poll::Booth.search("cen").size).to eq 2
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
Reference in New Issue
Block a user