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.
This commit is contained in:
Javi Martín
2019-10-01 02:19:41 +02:00
parent 24e4f4518f
commit 710a1d8357
4 changed files with 31 additions and 4 deletions

View File

@@ -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