Skip to content

Commit

Permalink
Added omni_search api to query both competitions and people
Browse files Browse the repository at this point in the history
simultaneously!
  • Loading branch information
jfly committed Nov 24, 2015
1 parent dc93fd6 commit a75c4e8
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 16 deletions.
18 changes: 11 additions & 7 deletions WcaOnRails/app/assets/javascripts/wca-autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ $(function() {

var only_one = $(that).hasClass("wca-autocomplete-only_one");
var search = $(that).hasClass("wca-autocomplete-search");
var omni_search = $(that).hasClass("wca-autocomplete-search");
var users_search = $(that).hasClass("wca-autocomplete-users_search");
var competitions_search = $(that).hasClass("wca-autocomplete-competitions_search");
var posts_search = $(that).hasClass("wca-autocomplete-posts_search");

var searchFields = [];
searchFields = searchFields.concat([ 'wca_id', 'name' ]); // user search fields
Expand All @@ -14,21 +16,23 @@ $(function() {

var url;
var defaultSearchData = {};
var valueField = 'id';
if(users_search) {
url = '/api/v0/users/search';
if(omni_search) {
url = '/api/v0/search';
} else if(users_search) {
url = '/api/v0/search/users';
var only_delegates = $(that).hasClass("wca-autocomplete-only_delegates");
var persons_table = $(that).hasClass("wca-autocomplete-persons_table");

if(only_delegates) {
defaultSearchData.only_delegates = true;
}
if(persons_table) {
valueField = 'wca_id';
defaultSearchData.persons_table = true;
}
} else if(competitions_search) {
url = '/api/v0/competitions/search';
url = '/api/v0/search/competitions';
} else if(posts_search) {
url = '/api/v0/search/posts';
} else {
throw new Error("Unrecognized wca-autocomplete type");
}
Expand Down Expand Up @@ -83,11 +87,11 @@ $(function() {
create = function(input, callback) {
var query = input;
var object = {
id: query,
query: query,
'class': 'search',
url: '/search?q=' + encodeURIComponent(query),
};
object[valueField] = query;
callback(object);
};
onChange = function(value) {
Expand All @@ -100,7 +104,7 @@ $(function() {
plugins: ['restore_on_backspace', 'remove_button', 'do_not_clear_on_blur'],
preload: true,
maxItems: only_one ? 1 : null,
valueField: valueField,
valueField: 'id',
searchField: searchFields,
delimeter: ',',
persist: false,
Expand Down
2 changes: 1 addition & 1 deletion WcaOnRails/app/assets/stylesheets/navbar-static-top.scss
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ $omni-search-open-width: 300px;
position: absolute;
right: 0;

.wca-omni-search + .selectize-control .selectize-input {
.wca-autocomplete-omni + .selectize-control .selectize-input {
width: $omni-search-closed-width;
transition: width 0.25s ease;

Expand Down
11 changes: 9 additions & 2 deletions WcaOnRails/app/controllers/api/v0/api_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ def scramble_program
def help
end

def search(model)
def search(*models)
query = params[:q]
unless query
render status: :bad_request, json: { error: "No query specified" }
return
end
result = model.search(query, params: params).limit(DEFAULT_API_RESULT_LIMIT)
result = models.map { |model| model.search(query, params: params).limit(DEFAULT_API_RESULT_LIMIT) }.flatten(1)
render json: { status: "ok", result: result.map(&:to_jsonable) }
end

Expand All @@ -77,6 +77,13 @@ def users_search
search(User)
end

def omni_search
# We intentionally exclude Post, as our autocomplete ui isn't very useful with
# them yet.
params[:persons_table] = true
search(Competition, User)
end

def show_user(user)
if user
render status: :ok, json: { status: "ok", user: user.to_jsonable }
Expand Down
2 changes: 1 addition & 1 deletion WcaOnRails/app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def wca_highlight(html, phrases)
end

def wca_omni_search
'<input type="text" class="form-control wca-omni-search wca-autocomplete wca-autocomplete-search wca-autocomplete-only_one wca-autocomplete-users_search wca-autocomplete-persons_table" />'.html_safe
'<input type="text" class="form-control wca-autocomplete wca-autocomplete-omni wca-autocomplete-search wca-autocomplete-only_one wca-autocomplete-users_search wca-autocomplete-persons_table" />'.html_safe
end

def notifications_for_user(user)
Expand Down
1 change: 1 addition & 0 deletions WcaOnRails/app/models/competition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ def self.search(query, params: {})
def to_jsonable
json = {
class: self.class.to_s.downcase,
url: "/results/c.php?i=#{id}",

id: id,
name: name,
Expand Down
2 changes: 1 addition & 1 deletion WcaOnRails/app/models/person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def to_jsonable(include_private_info: false)
class: self.class.to_s.downcase,
url: "/results/p.php?i=#{self.wca_id}",

id: nil,
id: self.id,
wca_id: self.wca_id,
name: self.name,

Expand Down
1 change: 1 addition & 0 deletions WcaOnRails/app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ def self.search(query, params: {})
def to_jsonable(include_private_info: false)
json = {
class: self.class.to_s.downcase,
url: "/results/p.php?i=#{self.wca_id}",

id: self.id,
wca_id: self.wca_id,
Expand Down
2 changes: 1 addition & 1 deletion WcaOnRails/app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<% if @omni_query.present? %>
<script>
$(function() {
$('.wca-omni-search').each(function() {
$('.wca-autocomplete-omni').each(function() {
var selectize = this.selectize;
// We're only interested in touching the original input, *not* the duplicate
// that selectize created. The original input has a selectize attribute.
Expand Down
7 changes: 4 additions & 3 deletions WcaOnRails/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,10 @@
get '/me' => "api#me"
get '/auth/results' => "api#auth_results"
get '/scramble-program' => "api#scramble_program"
get '/posts/search' => 'api#posts_search'
get '/competitions/search' => 'api#competitions_search'
get '/users/search' => 'api#users_search'
get '/search' => 'api#omni_search'
get '/search/posts' => 'api#posts_search'
get '/search/competitions' => 'api#competitions_search'
get '/search/users' => 'api#users_search'
get '/users/:id' => 'api#show_user_by_id', constraints: { id: /\d+/ }
get '/users/:wca_id' => 'api#show_user_by_wca_id'
resources :competitions, only: [:show]
Expand Down
23 changes: 23 additions & 0 deletions WcaOnRails/spec/controllers/api_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,29 @@
end
end

describe 'GET #omni_search' do
let!(:comp) { FactoryGirl.create(:competition, name: "jeremy Jfly's Competition 2015") }
let!(:post) { FactoryGirl.create(:post, title: "jeremy post title", body: "post body") }
let!(:user) { FactoryGirl.create(:user_with_wca_id, name: "Jeremy") }

it 'requires query parameter' do
get :competitions_search
expect(response.status).to eq 400
json = JSON.parse(response.body)
expect(json["error"]).to eq "No query specified"
end

it "finds all the things!" do
get :omni_search, q: "jeremy"
expect(response.status).to eq 200
json = JSON.parse(response.body)
expect(json["result"].length).to eq 2
expect(json["result"].select { |r| r["class"] == "competition" }.length).to eq 1
expect(json["result"].select { |r| r["class"] == "post" }.length).to eq 0
expect(json["result"].select { |r| r["class"] == "user" }.length).to eq 1
end
end

describe 'show_user_*' do
it 'can query by id' do
user = FactoryGirl.create(:user, name: "Jeremy")
Expand Down

0 comments on commit a75c4e8

Please sign in to comment.