diff --git a/app/controllers/search_controller.rb b/app/controllers/search_controller.rb index 36751f6..df3eedf 100644 --- a/app/controllers/search_controller.rb +++ b/app/controllers/search_controller.rb @@ -1,7 +1,7 @@ class SearchController < ApplicationController def index result = ContestSearch.search(params[:q]).limit(4) - hash_results = result.map {|r| {id: r.id, text: r.name} } + hash_results = result.map {|r| {id: r.id, text: r.display_name} } render json: { "results": hash_results } end end diff --git a/app/models/candidate.rb b/app/models/candidate.rb index 3a858a5..61651ad 100644 --- a/app/models/candidate.rb +++ b/app/models/candidate.rb @@ -1,7 +1,7 @@ class Candidate < ApplicationRecord belongs_to :contest - def name + def display_name primary_name end end diff --git a/app/models/contest.rb b/app/models/contest.rb index 375afc5..0a2a92f 100644 --- a/app/models/contest.rb +++ b/app/models/contest.rb @@ -1,2 +1,6 @@ class Contest < ApplicationRecord + + def display_name + "#{name} - #{electoral_district_name}" + end end diff --git a/app/models/contest_search.rb b/app/models/contest_search.rb index 1f0a1e0..5435476 100644 --- a/app/models/contest_search.rb +++ b/app/models/contest_search.rb @@ -1,14 +1,13 @@ class ContestSearch def self.search(query) - columns = %w(primary_office_name secondary_office_name electoral_district_name) tsvector = to_tsvector(columns) - results = Contest.where("to_tsvector(#{tsvector}) @@ to_tsquery('english', ?)", query + ":*") + results = Contest.where("to_tsvector(#{tsvector}) @@ to_tsquery('english', ?)", to_tsquery(query)) if results.empty? columns = %w(primary_name primary_party secondary_name) tsvector = to_tsvector(columns) - results = Candidate.where("to_tsvector(#{tsvector}) @@ to_tsquery('english', ?)", query + ":*") + results = Candidate.where("to_tsvector(#{tsvector}) @@ to_tsquery('english', ?)", to_tsquery(query)) end results @@ -17,4 +16,8 @@ def self.search(query) def self.to_tsvector(columns) columns.map {|c| "coalesce(#{c}, '')" }.join(" || ' ' || ") end + + def self.to_tsquery(query) + res = query.split(' ').map {|q| "#{q}:*" }.join(" & ") + end end diff --git a/app/views/questions/_question.html.erb b/app/views/questions/_question.html.erb index e35c9ed..e45e47e 100644 --- a/app/views/questions/_question.html.erb +++ b/app/views/questions/_question.html.erb @@ -9,7 +9,7 @@
votes
- Question for: <%= question.contest.name -%>
+ Question for: <%= question.contest.display_name -%>
<%= question.body -%>