From 89979e549f34b6557bb7b17acccf4ef999c917ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baza=CC=81n?= Date: Fri, 23 Dec 2016 19:03:39 +0100 Subject: [PATCH] adds search to Booth model --- app/models/poll/booth.rb | 5 +++++ spec/models/poll/booth_spec.rb | 11 +++++++++++ 2 files changed, 16 insertions(+) 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