2012-09-23 02:39:12 +00:00
|
|
|
class MainController < ApplicationController
|
2013-01-01 22:45:35 +00:00
|
|
|
include TopicsHelper
|
2014-01-29 03:46:58 +00:00
|
|
|
include MapsHelper
|
|
|
|
include UsersHelper
|
2014-02-05 01:28:06 +00:00
|
|
|
include SynapsesHelper
|
2016-03-11 14:10:31 +00:00
|
|
|
|
2016-04-24 15:50:35 +00:00
|
|
|
after_action :verify_policy_scoped, except: [:requestinvite, :searchmappers]
|
2016-07-26 00:14:23 +00:00
|
|
|
|
2014-07-30 05:24:20 +00:00
|
|
|
respond_to :html, :json
|
2016-07-26 00:14:23 +00:00
|
|
|
|
2014-01-29 03:46:58 +00:00
|
|
|
# home page
|
2013-01-08 04:03:41 +00:00
|
|
|
def home
|
2016-07-26 00:14:23 +00:00
|
|
|
@maps = policy_scope(Map).order('updated_at DESC').page(1).per(20)
|
2014-08-10 17:06:58 +00:00
|
|
|
respond_to do |format|
|
2016-07-26 00:14:23 +00:00
|
|
|
format.html do
|
|
|
|
if !authenticated?
|
|
|
|
render 'main/home'
|
|
|
|
else
|
|
|
|
render 'maps/activemaps'
|
|
|
|
end
|
|
|
|
end
|
2014-08-10 17:06:58 +00:00
|
|
|
end
|
2013-01-08 04:03:41 +00:00
|
|
|
end
|
2016-07-26 00:14:23 +00:00
|
|
|
|
2014-01-29 03:46:58 +00:00
|
|
|
### SEARCHING ###
|
2016-07-26 00:14:23 +00:00
|
|
|
|
2014-01-29 03:46:58 +00:00
|
|
|
# get /search/topics?term=SOMETERM
|
|
|
|
def searchtopics
|
|
|
|
term = params[:term]
|
2014-02-02 19:56:07 +00:00
|
|
|
user = params[:user] ? params[:user] : false
|
2016-07-26 00:14:23 +00:00
|
|
|
|
|
|
|
if term && !term.empty? && term.downcase[0..3] != 'map:' && term.downcase[0..6] != 'mapper:' && !term.casecmp('topic:').zero?
|
|
|
|
|
|
|
|
# remove "topic:" if appended at beginning
|
|
|
|
term = term[6..-1] if term.downcase[0..5] == 'topic:'
|
|
|
|
|
|
|
|
# if desc: search desc instead
|
2014-02-05 01:28:06 +00:00
|
|
|
desc = false
|
2016-07-26 00:14:23 +00:00
|
|
|
if term.downcase[0..4] == 'desc:'
|
|
|
|
term = term[5..-1]
|
2014-02-05 01:28:06 +00:00
|
|
|
desc = true
|
|
|
|
end
|
2016-07-26 00:14:23 +00:00
|
|
|
|
|
|
|
# if link: search link instead
|
2014-02-05 01:28:06 +00:00
|
|
|
link = false
|
2016-07-26 00:14:23 +00:00
|
|
|
if term.downcase[0..4] == 'link:'
|
|
|
|
term = term[5..-1]
|
2014-02-05 01:28:06 +00:00
|
|
|
link = true
|
|
|
|
end
|
2016-07-26 00:14:23 +00:00
|
|
|
|
|
|
|
# check whether there's a filter by metacode as part of the query
|
2014-01-29 03:46:58 +00:00
|
|
|
filterByMetacode = false
|
|
|
|
Metacode.all.each do |m|
|
2016-07-26 00:14:23 +00:00
|
|
|
lOne = m.name.length + 1
|
2014-01-29 03:46:58 +00:00
|
|
|
lTwo = m.name.length
|
2016-07-26 00:14:23 +00:00
|
|
|
|
|
|
|
if term.downcase[0..lTwo] == m.name.downcase + ':'
|
|
|
|
term = term[lOne..-1]
|
2014-01-29 03:46:58 +00:00
|
|
|
filterByMetacode = m
|
|
|
|
end
|
|
|
|
end
|
2016-03-12 01:09:41 +00:00
|
|
|
|
|
|
|
search = '%' + term.downcase + '%'
|
|
|
|
builder = policy_scope(Topic)
|
2016-07-26 00:14:23 +00:00
|
|
|
|
2014-01-29 03:46:58 +00:00
|
|
|
if filterByMetacode
|
2016-07-26 00:14:23 +00:00
|
|
|
if term == ''
|
2016-03-12 01:09:41 +00:00
|
|
|
builder = builder.none
|
2014-01-29 03:46:58 +00:00
|
|
|
else
|
2016-03-12 01:09:41 +00:00
|
|
|
builder = builder.where('LOWER("name") like ? OR
|
|
|
|
LOWER("desc") like ? OR
|
|
|
|
LOWER("link") like ?', search, search, search)
|
|
|
|
builder = builder.where(metacode_id: filterByMetacode.id)
|
2014-01-29 03:46:58 +00:00
|
|
|
end
|
2014-02-05 01:28:06 +00:00
|
|
|
elsif desc
|
2016-03-12 01:09:41 +00:00
|
|
|
builder = builder.where('LOWER("desc") like ?', search)
|
2014-02-05 01:28:06 +00:00
|
|
|
elsif link
|
2016-03-12 01:09:41 +00:00
|
|
|
builder = builder.where('LOWER("link") like ?', search)
|
2016-07-26 00:14:23 +00:00
|
|
|
else # regular case, just search the name
|
2016-03-12 01:09:41 +00:00
|
|
|
builder = builder.where('LOWER("name") like ? OR
|
|
|
|
LOWER("desc") like ? OR
|
|
|
|
LOWER("link") like ?', search, search, search)
|
2013-01-03 08:53:25 +00:00
|
|
|
end
|
2016-03-12 01:09:41 +00:00
|
|
|
|
|
|
|
builder = builder.where(user: user) if user
|
|
|
|
@topics = builder.order(:name)
|
2014-01-29 03:46:58 +00:00
|
|
|
else
|
|
|
|
@topics = []
|
2012-12-23 06:12:56 +00:00
|
|
|
end
|
2016-03-12 01:09:41 +00:00
|
|
|
|
2014-01-29 03:46:58 +00:00
|
|
|
render json: autocomplete_array_json(@topics)
|
|
|
|
end
|
2016-07-26 00:14:23 +00:00
|
|
|
|
2014-01-29 03:46:58 +00:00
|
|
|
# get /search/maps?term=SOMETERM
|
|
|
|
def searchmaps
|
|
|
|
term = params[:term]
|
2014-02-02 19:56:07 +00:00
|
|
|
user = params[:user] ? params[:user] : nil
|
2016-07-26 00:14:23 +00:00
|
|
|
|
|
|
|
if term && !term.empty? && term.downcase[0..5] != 'topic:' && term.downcase[0..6] != 'mapper:' && !term.casecmp('map:').zero?
|
|
|
|
|
|
|
|
# remove "map:" if appended at beginning
|
|
|
|
term = term[4..-1] if term.downcase[0..3] == 'map:'
|
|
|
|
|
|
|
|
# if desc: search desc instead
|
2014-02-05 01:28:06 +00:00
|
|
|
desc = false
|
2016-07-26 00:14:23 +00:00
|
|
|
if term.downcase[0..4] == 'desc:'
|
|
|
|
term = term[5..-1]
|
2014-02-05 01:28:06 +00:00
|
|
|
desc = true
|
|
|
|
end
|
2016-03-12 01:09:41 +00:00
|
|
|
|
2014-02-19 03:44:10 +00:00
|
|
|
search = '%' + term.downcase + '%'
|
2016-03-12 01:09:41 +00:00
|
|
|
builder = policy_scope(Map)
|
|
|
|
|
2016-07-26 00:14:23 +00:00
|
|
|
builder = if desc
|
|
|
|
builder.where('LOWER("desc") like ?', search)
|
|
|
|
else
|
|
|
|
builder.where('LOWER("name") like ?', search)
|
|
|
|
end
|
2016-03-12 01:09:41 +00:00
|
|
|
builder = builder.where(user: user) if user
|
|
|
|
@maps = builder.order(:name)
|
2014-01-29 03:46:58 +00:00
|
|
|
else
|
|
|
|
@maps = []
|
2012-12-23 06:12:56 +00:00
|
|
|
end
|
2016-07-26 00:14:23 +00:00
|
|
|
|
2014-01-29 03:46:58 +00:00
|
|
|
render json: autocomplete_map_array_json(@maps)
|
2012-12-23 06:12:56 +00:00
|
|
|
end
|
2016-07-26 00:14:23 +00:00
|
|
|
|
2014-01-29 03:46:58 +00:00
|
|
|
# get /search/mappers?term=SOMETERM
|
|
|
|
def searchmappers
|
|
|
|
term = params[:term]
|
2016-07-26 00:14:23 +00:00
|
|
|
if term && !term.empty? && term.downcase[0..3] != 'map:' && term.downcase[0..5] != 'topic:' && !term.casecmp('mapper:').zero?
|
|
|
|
|
|
|
|
# remove "mapper:" if appended at beginning
|
|
|
|
term = term[7..-1] if term.downcase[0..6] == 'mapper:'
|
2016-03-12 01:09:41 +00:00
|
|
|
search = term.downcase + '%'
|
2016-04-24 15:50:35 +00:00
|
|
|
|
2016-07-26 00:14:23 +00:00
|
|
|
skip_policy_scope # TODO: builder = policy_scope(User)
|
2016-04-24 15:50:35 +00:00
|
|
|
builder = User.where('LOWER("name") like ?', search)
|
2016-03-12 01:09:41 +00:00
|
|
|
@mappers = builder.order(:name)
|
2014-01-29 03:46:58 +00:00
|
|
|
else
|
|
|
|
@mappers = []
|
2012-10-29 17:52:29 +00:00
|
|
|
end
|
2014-01-29 03:46:58 +00:00
|
|
|
render json: autocomplete_user_array_json(@mappers)
|
2016-07-26 00:14:23 +00:00
|
|
|
end
|
|
|
|
|
2014-02-05 01:28:06 +00:00
|
|
|
# get /search/synapses?term=SOMETERM OR
|
|
|
|
# get /search/synapses?topic1id=SOMEID&topic2id=SOMEID
|
|
|
|
def searchsynapses
|
|
|
|
term = params[:term]
|
|
|
|
topic1id = params[:topic1id]
|
|
|
|
topic2id = params[:topic2id]
|
2015-10-02 09:39:53 +00:00
|
|
|
|
2014-02-05 01:28:06 +00:00
|
|
|
if term && !term.empty?
|
2016-03-12 01:09:41 +00:00
|
|
|
@synapses = policy_scope(Synapse).where('LOWER("desc") like ?', '%' + term.downcase + '%').order('"desc"')
|
2014-11-16 17:35:06 +00:00
|
|
|
|
2016-07-26 00:14:23 +00:00
|
|
|
# remove any duplicate synapse types that just differ by
|
2014-11-16 17:35:06 +00:00
|
|
|
# leading or trailing whitespaces
|
|
|
|
collectedDesc = []
|
2016-07-26 00:14:23 +00:00
|
|
|
@synapses.to_a.uniq(&:desc).delete_if do |s|
|
|
|
|
desc = s.desc.nil? || s.desc == '' ? '' : s.desc.strip
|
|
|
|
if collectedDesc.index(desc).nil?
|
2014-11-16 17:35:06 +00:00
|
|
|
collectedDesc.push(desc)
|
2016-07-26 00:14:23 +00:00
|
|
|
false # return this value
|
2014-11-16 17:35:06 +00:00
|
|
|
else
|
2016-07-26 00:14:23 +00:00
|
|
|
true # return this value
|
2014-11-16 17:35:06 +00:00
|
|
|
end
|
2016-07-26 00:14:23 +00:00
|
|
|
end
|
2014-02-05 01:28:06 +00:00
|
|
|
elsif topic1id && !topic1id.empty?
|
2016-03-12 01:09:41 +00:00
|
|
|
@one = policy_scope(Synapse).where('node1_id = ? AND node2_id = ?', topic1id, topic2id)
|
|
|
|
@two = policy_scope(Synapse).where('node2_id = ? AND node1_id = ?', topic1id, topic2id)
|
2014-02-05 01:28:06 +00:00
|
|
|
@synapses = @one + @two
|
2016-07-26 00:14:23 +00:00
|
|
|
@synapses.sort! { |s1, s2| s1.desc <=> s2.desc }.to_a
|
2014-02-05 01:28:06 +00:00
|
|
|
else
|
|
|
|
@synapses = []
|
|
|
|
end
|
2015-10-01 04:33:38 +00:00
|
|
|
|
2016-07-26 00:14:23 +00:00
|
|
|
# limit to 5 results
|
|
|
|
@synapses = @synapses.slice(0, 5)
|
2016-03-12 01:09:41 +00:00
|
|
|
|
2015-10-02 07:46:48 +00:00
|
|
|
render json: autocomplete_synapse_array_json(@synapses)
|
2016-07-26 00:14:23 +00:00
|
|
|
end
|
2012-09-23 02:39:12 +00:00
|
|
|
end
|