Files
nairobi/app/models/poll/booth.rb
Javi Martín 710a1d8357 Split Poll::Booth.search in two methods
We use this method in two different scenarios. In an AJAX request, we
don't want to return every booth if the search is blank. However, in a
normal HTTP GET request, we want to return every record when the search
is empty, as we do everywhere else.

It's possible the behaviour of the AJAX call is unusual, since it
searches all booths, and not just the ones assigned to a poll. If we
changed this behaviour, we could simplify the code and remove the
`quick_search` method.
2019-10-07 14:30:39 +02:00

30 lines
684 B
Ruby

class Poll
class Booth < ApplicationRecord
has_many :booth_assignments, class_name: "Poll::BoothAssignment"
has_many :polls, through: :booth_assignments
has_many :shifts
validates :name, presence: true, uniqueness: true
def self.search(terms)
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
def assignment_on_poll(poll)
booth_assignments.where(poll: poll).first
end
end
end