diff --git a/app/controllers/admin/poll/booth_assignments_controller.rb b/app/controllers/admin/poll/booth_assignments_controller.rb index 0fc6e386a..627e18ef0 100644 --- a/app/controllers/admin/poll/booth_assignments_controller.rb +++ b/app/controllers/admin/poll/booth_assignments_controller.rb @@ -9,7 +9,7 @@ class Admin::Poll::BoothAssignmentsController < Admin::Poll::BaseController def search_booths load_search - @booths = ::Poll::Booth.search(@search) + @booths = ::Poll::Booth.quick_search(@search) respond_to do |format| format.js end diff --git a/app/controllers/admin/poll/booths_controller.rb b/app/controllers/admin/poll/booths_controller.rb index c27a65694..9f80a0de4 100644 --- a/app/controllers/admin/poll/booths_controller.rb +++ b/app/controllers/admin/poll/booths_controller.rb @@ -2,7 +2,7 @@ class Admin::Poll::BoothsController < Admin::Poll::BaseController load_and_authorize_resource class: "Poll::Booth" def index - @booths = @booths.search params[:search] if params[:search] + @booths = @booths.search(params[:search]) @booths = @booths.order(name: :asc).page(params[:page]) end diff --git a/app/models/poll/booth.rb b/app/models/poll/booth.rb index 4c45b9d1a..64174a7a6 100644 --- a/app/models/poll/booth.rb +++ b/app/models/poll/booth.rb @@ -7,10 +7,17 @@ class Poll 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 + def self.quick_search(terms) + if terms.blank? + Booth.none + else + search(terms) + end + end + def self.available where(polls: { id: Poll.current_or_recounting }).joins(:polls) end diff --git a/spec/models/poll/booth_spec.rb b/spec/models/poll/booth_spec.rb index 95a9b7ad3..e19d82bd6 100644 --- a/spec/models/poll/booth_spec.rb +++ b/spec/models/poll/booth_spec.rb @@ -13,7 +13,7 @@ describe Poll::Booth do expect(booth).not_to be_valid end - describe "#search" do + describe ".search" do it "finds 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") @@ -22,6 +22,26 @@ describe Poll::Booth do expect(Poll::Booth.search("hall")).to eq([booth2]) expect(Poll::Booth.search("cen")).to match_array [booth1, booth2] end + + it "returns all booths if search term is blank" do + 2.times { create(:poll_booth) } + + expect(Poll::Booth.search("").count).to eq 2 + end + + it "returns all booths if search term is nil" do + 2.times { create(:poll_booth) } + + expect(Poll::Booth.search(nil).count).to eq 2 + end + end + + describe ".quick_search" do + it "returns no booths if search term is blank" do + create(:poll_booth) + + expect(Poll::Booth.quick_search("")).to be_empty + end end describe ".available" do