2016-09-25 17:00:30 +00:00
|
|
|
# frozen_string_literal: true
|
2017-11-06 16:15:18 +00:00
|
|
|
|
2016-09-29 00:02:25 +00:00
|
|
|
class SearchController < ApplicationController
|
2016-09-25 17:00:30 +00:00
|
|
|
include TopicsHelper
|
|
|
|
include MapsHelper
|
|
|
|
include UsersHelper
|
|
|
|
include SynapsesHelper
|
|
|
|
|
|
|
|
before_action :authorize_search
|
|
|
|
after_action :verify_authorized
|
2017-11-06 16:15:18 +00:00
|
|
|
after_action :verify_policy_scoped, only: %i(maps mappers synapses topics)
|
2016-09-25 17:00:30 +00:00
|
|
|
|
|
|
|
# get /search/topics?term=SOMETERM
|
|
|
|
def topics
|
|
|
|
term = params[:term]
|
|
|
|
user = params[:user] ? params[:user] : false
|
|
|
|
|
2017-01-24 00:30:13 +00:00
|
|
|
if term.present? && term.downcase[0..3] != 'map:' &&
|
|
|
|
term.downcase[0..6] != 'mapper:' && !term.casecmp('topic:').zero?
|
2016-09-25 17:00:30 +00:00
|
|
|
|
|
|
|
# remove "topic:" if appended at beginning
|
|
|
|
term = term[6..-1] if term.downcase[0..5] == 'topic:'
|
|
|
|
|
|
|
|
# if desc: search desc instead
|
|
|
|
desc = false
|
|
|
|
if term.downcase[0..4] == 'desc:'
|
|
|
|
term = term[5..-1]
|
|
|
|
desc = true
|
|
|
|
end
|
|
|
|
|
|
|
|
# if link: search link instead
|
|
|
|
link = false
|
|
|
|
if term.downcase[0..4] == 'link:'
|
|
|
|
term = term[5..-1]
|
|
|
|
link = true
|
|
|
|
end
|
|
|
|
|
|
|
|
# check whether there's a filter by metacode as part of the query
|
2017-01-24 00:30:13 +00:00
|
|
|
filter_by_metacode = false
|
2016-09-25 17:00:30 +00:00
|
|
|
Metacode.all.each do |m|
|
2017-01-24 00:30:13 +00:00
|
|
|
length_one = m.name.length + 1
|
|
|
|
length_two = m.name.length
|
2016-09-25 17:00:30 +00:00
|
|
|
|
2017-01-24 00:30:13 +00:00
|
|
|
if term.downcase[0..length_two] == m.name.downcase + ':'
|
|
|
|
term = term[length_one..-1]
|
|
|
|
filter_by_metacode = m
|
2016-09-25 17:00:30 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-07 19:58:53 +00:00
|
|
|
search = '%' + term.downcase.strip + '%'
|
2016-09-25 17:00:30 +00:00
|
|
|
builder = policy_scope(Topic)
|
|
|
|
|
2017-01-24 00:30:13 +00:00
|
|
|
if filter_by_metacode
|
2016-09-25 17:00:30 +00:00
|
|
|
if term == ''
|
|
|
|
builder = builder.none
|
|
|
|
else
|
|
|
|
builder = builder.where('LOWER("name") like ? OR
|
|
|
|
LOWER("desc") like ? OR
|
|
|
|
LOWER("link") like ?', search, search, search)
|
2017-01-24 00:30:13 +00:00
|
|
|
builder = builder.where(metacode_id: filter_by_metacode.id)
|
2016-09-25 17:00:30 +00:00
|
|
|
end
|
|
|
|
elsif desc
|
|
|
|
builder = builder.where('LOWER("desc") like ?', search)
|
|
|
|
elsif link
|
|
|
|
builder = builder.where('LOWER("link") like ?', search)
|
|
|
|
else # regular case, just search the name
|
|
|
|
builder = builder.where('LOWER("name") like ? OR
|
|
|
|
LOWER("desc") like ? OR
|
|
|
|
LOWER("link") like ?', search, search, search)
|
|
|
|
end
|
|
|
|
|
|
|
|
builder = builder.where(user: user) if user
|
|
|
|
@topics = builder.order(:name)
|
|
|
|
else
|
2016-11-30 16:46:09 +00:00
|
|
|
skip_policy_scope
|
2016-09-25 17:00:30 +00:00
|
|
|
@topics = []
|
|
|
|
end
|
|
|
|
|
2016-09-29 00:02:25 +00:00
|
|
|
render json: autocomplete_array_json(@topics).to_json
|
2016-09-25 17:00:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# get /search/maps?term=SOMETERM
|
|
|
|
def maps
|
|
|
|
term = params[:term]
|
|
|
|
user = params[:user] ? params[:user] : nil
|
|
|
|
|
2017-01-24 00:30:13 +00:00
|
|
|
if term.present? && term.downcase[0..5] != 'topic:' &&
|
|
|
|
term.downcase[0..6] != 'mapper:' && !term.casecmp('map:').zero?
|
2016-09-25 17:00:30 +00:00
|
|
|
|
|
|
|
# remove "map:" if appended at beginning
|
|
|
|
term = term[4..-1] if term.downcase[0..3] == 'map:'
|
|
|
|
|
|
|
|
# if desc: search desc instead
|
|
|
|
desc = false
|
|
|
|
if term.downcase[0..4] == 'desc:'
|
|
|
|
term = term[5..-1]
|
|
|
|
desc = true
|
|
|
|
end
|
|
|
|
|
2016-11-07 19:58:53 +00:00
|
|
|
search = '%' + term.downcase.strip + '%'
|
2016-09-25 17:00:30 +00:00
|
|
|
builder = policy_scope(Map)
|
|
|
|
|
|
|
|
builder = if desc
|
|
|
|
builder.where('LOWER("desc") like ?', search)
|
|
|
|
else
|
|
|
|
builder.where('LOWER("name") like ?', search)
|
|
|
|
end
|
|
|
|
builder = builder.where(user: user) if user
|
|
|
|
@maps = builder.order(:name)
|
|
|
|
else
|
2016-12-06 18:09:42 +00:00
|
|
|
skip_policy_scope
|
2016-09-25 17:00:30 +00:00
|
|
|
@maps = []
|
|
|
|
end
|
|
|
|
|
2016-09-29 00:02:25 +00:00
|
|
|
render json: autocomplete_map_array_json(@maps).to_json
|
2016-09-25 17:00:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# get /search/mappers?term=SOMETERM
|
|
|
|
def mappers
|
|
|
|
term = params[:term]
|
2017-01-24 00:30:13 +00:00
|
|
|
if term.present? && term.downcase[0..3] != 'map:' &&
|
|
|
|
term.downcase[0..5] != 'topic:' && !term.casecmp('mapper:').zero?
|
2016-09-25 17:00:30 +00:00
|
|
|
|
|
|
|
# remove "mapper:" if appended at beginning
|
|
|
|
term = term[7..-1] if term.downcase[0..6] == 'mapper:'
|
2016-11-07 19:58:53 +00:00
|
|
|
search = term.downcase.strip + '%'
|
2016-09-25 17:00:30 +00:00
|
|
|
|
2016-12-06 18:09:42 +00:00
|
|
|
builder = policy_scope(User).where('LOWER("name") like ?', search)
|
2016-09-25 17:00:30 +00:00
|
|
|
@mappers = builder.order(:name)
|
|
|
|
else
|
2016-12-06 18:09:42 +00:00
|
|
|
skip_policy_scope
|
2016-09-25 17:00:30 +00:00
|
|
|
@mappers = []
|
|
|
|
end
|
2016-09-29 00:02:25 +00:00
|
|
|
render json: autocomplete_user_array_json(@mappers).to_json
|
2016-09-25 17:00:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# get /search/synapses?term=SOMETERM OR
|
|
|
|
# get /search/synapses?topic1id=SOMEID&topic2id=SOMEID
|
|
|
|
def synapses
|
|
|
|
term = params[:term]
|
|
|
|
topic1id = params[:topic1id]
|
|
|
|
topic2id = params[:topic2id]
|
|
|
|
|
2017-11-06 16:15:18 +00:00
|
|
|
if term.present?
|
2017-01-24 00:30:13 +00:00
|
|
|
@synapses = policy_scope(Synapse)
|
|
|
|
.where('LOWER("desc") like ?', '%' + term.downcase.strip + '%')
|
|
|
|
.order('"desc"')
|
2016-09-25 17:00:30 +00:00
|
|
|
|
|
|
|
@synapses = @synapses.uniq(&:desc)
|
2017-11-06 16:15:18 +00:00
|
|
|
elsif topic1id.present?
|
2017-01-24 00:30:13 +00:00
|
|
|
one = policy_scope(Synapse).where(topic1_id: topic1id, topic2_id: topic2id)
|
|
|
|
two = policy_scope(Synapse).where(topic2_id: topic1id, topic1_id: topic2id)
|
|
|
|
@synapses = one + two
|
2016-09-25 17:00:30 +00:00
|
|
|
@synapses.sort! { |s1, s2| s1.desc <=> s2.desc }.to_a
|
|
|
|
else
|
2016-12-06 18:09:42 +00:00
|
|
|
skip_policy_scope
|
2016-09-25 17:00:30 +00:00
|
|
|
@synapses = []
|
|
|
|
end
|
|
|
|
|
|
|
|
# limit to 5 results
|
|
|
|
@synapses = @synapses.to_a.slice(0, 5)
|
|
|
|
|
2016-09-29 00:02:25 +00:00
|
|
|
render json: autocomplete_synapse_array_json(@synapses).to_json
|
2016-09-25 17:00:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def authorize_search
|
|
|
|
authorize :Search
|
|
|
|
end
|
|
|
|
end
|