diff --git a/app/models/poll/booth.rb b/app/models/poll/booth.rb index 24f71c11a..c7fb63efc 100644 --- a/app/models/poll/booth.rb +++ b/app/models/poll/booth.rb @@ -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 \ No newline at end of file diff --git a/spec/models/poll/booth_spec.rb b/spec/models/poll/booth_spec.rb index 2fabb9c12..c095c62cd 100644 --- a/spec/models/poll/booth_spec.rb +++ b/spec/models/poll/booth_spec.rb @@ -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 \ No newline at end of file