Skip to content

Commit

Permalink
Merge pull request #5 from dougcole/candidate_search
Browse files Browse the repository at this point in the history
Candidate search
  • Loading branch information
dougcole authored Mar 28, 2018
2 parents c117118 + 259ac9d commit 3225ce7
Show file tree
Hide file tree
Showing 13 changed files with 77 additions and 6 deletions.
12 changes: 12 additions & 0 deletions app/assets/javascripts/questions.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.


$(document).ready(function() {
$('.select2').select2({
ajax: {
url: '/search',
dataType: 'json'
},
minimumInputLength: 3,
width: '200px'
});
});
2 changes: 2 additions & 0 deletions app/assets/javascripts/search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.
3 changes: 3 additions & 0 deletions app/assets/stylesheets/search.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the search controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
7 changes: 7 additions & 0 deletions app/controllers/search_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class SearchController < ApplicationController
def index
result = ContestSearch.search(params[:q]).limit(4)
hash_results = result.map {|r| {id: r.id, text: r.display_name} }
render json: { "results": hash_results }
end
end
2 changes: 2 additions & 0 deletions app/helpers/search_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module SearchHelper
end
4 changes: 4 additions & 0 deletions app/models/candidate.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
class Candidate < ApplicationRecord
belongs_to :contest

def display_name
primary_name
end
end
4 changes: 4 additions & 0 deletions app/models/contest.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
class Contest < ApplicationRecord

def display_name
"#{name} - #{electoral_district_name}"
end
end
23 changes: 23 additions & 0 deletions app/models/contest_search.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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', ?)", 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', ?)", to_tsquery(query))
end

results
end

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
2 changes: 2 additions & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
<link href="https://use.fontawesome.com/releases/v5.0.6/css/all.css" rel="stylesheet">
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>
</head>

<body>
Expand Down
2 changes: 1 addition & 1 deletion app/views/questions/_question.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<div class="text-center">votes</div>
</div>
<div class="col">
Question for: <%= question.contest.name -%><br />
Question for: <%= question.contest.display_name -%><br />
<%= question.body -%>
</div>
</div>
14 changes: 9 additions & 5 deletions app/views/questions/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<%= form_for @question do |f|-%>
<%= f.select :contest_id, options_for_select(Contest.pluck(:name, :id)) -%>
<%= f.text_area :body -%>
<%= f.submit-%>
<% end -%>
<div class="row w-80">
<div class="col-12">
<%= form_for @question, html: {class: 'mw-100'} do |f|-%>
<div> <%= f.select :contest_id, '', {}, class: 'select2' -%> </div>
<%= f.text_area :body -%>
<%= f.submit-%>
<% end -%>
</div>
</div>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@

resources :questions, :only => [:index, :new, :create]
resources :votes, :only => :create
get 'search', to: 'search#index'
end
7 changes: 7 additions & 0 deletions test/controllers/search_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class SearchControllerTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end
end

0 comments on commit 3225ce7

Please sign in to comment.