2012-10-10 00:23:45 +00:00
|
|
|
class SynapsesController < ApplicationController
|
2013-01-01 22:45:35 +00:00
|
|
|
include TopicsHelper
|
2012-10-10 00:23:45 +00:00
|
|
|
|
2013-01-25 05:47:32 +00:00
|
|
|
before_filter :require_user, only: [:new, :create, :edit, :update, :removefrommap, :destroy]
|
2012-10-10 00:23:45 +00:00
|
|
|
|
|
|
|
respond_to :html, :js, :json
|
|
|
|
|
2013-03-16 17:30:54 +00:00
|
|
|
def autocomplete_synapse_desc
|
|
|
|
term = params[:term]
|
|
|
|
if term && !term.empty?
|
|
|
|
items = Synapse.select('DISTINCT "desc"').
|
|
|
|
where('LOWER("desc") like ?', term.downcase + '%').
|
|
|
|
limit(10).order('"desc"')
|
|
|
|
else
|
|
|
|
items = {}
|
|
|
|
end
|
|
|
|
render :json => json_for_autocomplete(items, :desc)
|
|
|
|
end
|
2012-12-21 00:24:27 +00:00
|
|
|
|
2012-12-21 23:07:13 +00:00
|
|
|
# GET synapses
|
|
|
|
# or GET users/:user_id/synapses
|
2012-10-10 00:23:45 +00:00
|
|
|
def index
|
2012-12-21 23:07:13 +00:00
|
|
|
@current = current_user
|
|
|
|
|
|
|
|
if params[:user_id]
|
|
|
|
@user = User.find(params[:user_id])
|
|
|
|
@synapses = Synapse.visibleToUser(@current, @user)
|
|
|
|
elsif
|
|
|
|
@synapses = Synapse.visibleToUser(@current, nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
@synapsesjson = synapses_as_json(@current, @synapses).html_safe
|
2012-10-26 10:04:52 +00:00
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html
|
|
|
|
format.json { respond_with(@synapsesjson) }
|
|
|
|
end
|
2012-10-10 00:23:45 +00:00
|
|
|
end
|
|
|
|
|
2012-12-21 23:07:13 +00:00
|
|
|
# Get synapses/new
|
2012-10-10 00:23:45 +00:00
|
|
|
def new
|
2012-10-18 00:51:54 +00:00
|
|
|
@synapse = Synapse.new
|
2012-10-10 00:23:45 +00:00
|
|
|
@user = current_user
|
|
|
|
|
2012-10-24 06:47:08 +00:00
|
|
|
respond_with(@synapse)
|
2012-10-10 00:23:45 +00:00
|
|
|
end
|
|
|
|
|
2012-12-21 23:07:13 +00:00
|
|
|
# GET synapses/:id
|
2012-10-10 00:23:45 +00:00
|
|
|
def show
|
2012-12-21 23:07:13 +00:00
|
|
|
@current = current_user
|
|
|
|
@synapse = Synapse.find(params[:id]).authorize_to_show(@current)
|
2013-01-01 22:45:35 +00:00
|
|
|
@topic1 = @synapse.topic1.authorize_to_show(@current)
|
|
|
|
@topic2 = @synapse.topic2.authorize_to_show(@current)
|
2012-10-10 00:23:45 +00:00
|
|
|
|
2013-01-01 22:45:35 +00:00
|
|
|
if @synapse && @topic1 && @topic2
|
2012-12-21 23:07:13 +00:00
|
|
|
@synapsejson = @synapse.selfplusnodes_as_json.html_safe
|
|
|
|
else
|
|
|
|
redirect_to root_url and return
|
|
|
|
end
|
2012-10-10 00:23:45 +00:00
|
|
|
|
2012-12-21 23:07:13 +00:00
|
|
|
respond_to do |format|
|
2012-10-19 21:26:46 +00:00
|
|
|
format.html
|
|
|
|
format.json { respond_with(@synapsejson) }
|
2012-10-10 00:23:45 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-18 22:08:06 +00:00
|
|
|
# GET synapses/:id/json
|
|
|
|
def json
|
|
|
|
@current = current_user
|
|
|
|
@synapse = Synapse.find(params[:id]).authorize_to_show(@current)
|
|
|
|
|
|
|
|
if not @synapse
|
|
|
|
redirect_to root_url and return
|
|
|
|
end
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.json { render :json => @synapse.selfplusnodes_as_json }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-21 23:07:13 +00:00
|
|
|
# POST synapses
|
2012-10-10 00:23:45 +00:00
|
|
|
def create
|
|
|
|
@user = current_user
|
2012-12-21 04:45:44 +00:00
|
|
|
@synapse = Synapse.new()
|
|
|
|
@synapse.desc = params[:synapse][:desc]
|
2013-01-01 22:45:35 +00:00
|
|
|
@synapse.topic1 = Topic.find(params[:synapse][:topic1id])
|
|
|
|
@synapse.topic2 = Topic.find(params[:synapse][:topic2id])
|
2012-12-21 04:45:44 +00:00
|
|
|
@synapse.permission = "commons"
|
|
|
|
@synapse.category = "from-to"
|
|
|
|
@synapse.weight = 5
|
|
|
|
@synapse.user = @user
|
|
|
|
@synapse.save
|
2012-10-26 10:04:52 +00:00
|
|
|
|
2013-03-01 02:46:08 +00:00
|
|
|
if params[:synapse][:map]
|
|
|
|
@map = Map.find(params[:synapse][:map])
|
2013-01-18 22:08:06 +00:00
|
|
|
@mapping = Mapping.new()
|
2013-03-01 02:46:08 +00:00
|
|
|
@mapping.category = "Synapse"
|
|
|
|
@mapping.user = @user
|
|
|
|
@mapping.map = @map
|
|
|
|
@mapping.synapse = @synapse
|
|
|
|
@mapping.save
|
2013-01-18 22:08:06 +00:00
|
|
|
|
2013-04-26 04:07:29 +00:00
|
|
|
#push add to map to realtime viewers of the map
|
|
|
|
@mapping.message 'create',@user.id
|
|
|
|
|
2013-03-01 02:46:08 +00:00
|
|
|
# set the permission of the synapse to whatever the permission of the
|
|
|
|
#map is
|
2013-01-18 22:08:06 +00:00
|
|
|
@synapse.permission = @map.permission
|
|
|
|
@synapse.save
|
2013-04-26 04:07:29 +00:00
|
|
|
end
|
2012-10-10 00:23:45 +00:00
|
|
|
|
|
|
|
respond_to do |format|
|
2012-12-21 23:07:13 +00:00
|
|
|
format.html { respond_with(@user, location: synapse_url(@synapse)) }
|
2012-12-21 00:24:27 +00:00
|
|
|
format.js { respond_with(@synapse) }
|
2012-10-10 00:23:45 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2012-12-21 23:07:13 +00:00
|
|
|
# GET synapses/:id/edit
|
2012-10-10 00:23:45 +00:00
|
|
|
def edit
|
2013-01-27 22:21:19 +00:00
|
|
|
@current = current_user
|
2012-12-21 23:07:13 +00:00
|
|
|
@synapse = Synapse.find(params[:id]).authorize_to_edit(@current)
|
2012-10-10 00:23:45 +00:00
|
|
|
|
2013-01-27 22:21:19 +00:00
|
|
|
if @synapse
|
|
|
|
@topics = Topic.visibleToUser(@current, nil)
|
|
|
|
elsif not @synapse
|
|
|
|
redirect_to root_url and return
|
|
|
|
end
|
2012-10-10 00:23:45 +00:00
|
|
|
|
2013-01-01 22:45:35 +00:00
|
|
|
respond_with(@synapse, @topics)
|
2012-10-10 00:23:45 +00:00
|
|
|
end
|
|
|
|
|
2012-12-21 23:07:13 +00:00
|
|
|
# PUT synapses/:id
|
2012-10-10 00:23:45 +00:00
|
|
|
def update
|
2013-02-04 02:12:15 +00:00
|
|
|
@current = current_user
|
2012-12-21 23:07:13 +00:00
|
|
|
@synapse = Synapse.find(params[:id]).authorize_to_edit(@current)
|
2012-10-10 00:23:45 +00:00
|
|
|
|
2013-02-04 02:12:15 +00:00
|
|
|
if @synapse
|
2013-04-26 04:07:29 +00:00
|
|
|
@permissionBefore = @synapse.permission
|
|
|
|
|
2013-01-08 00:43:12 +00:00
|
|
|
if params[:synapse]
|
|
|
|
@synapse.desc = params[:synapse][:desc] if params[:synapse][:desc]
|
2013-02-04 02:12:15 +00:00
|
|
|
@synapse.category = params[:synapse][:category] if params[:synapse][:category]
|
|
|
|
@synapse.permission = params[:synapse][:permission] if params[:synapse][:permission]
|
2013-01-08 00:43:12 +00:00
|
|
|
end
|
|
|
|
if params[:node1_id] and params[:node1_id][:node1]
|
2013-02-04 02:12:15 +00:00
|
|
|
@synapse.topic1 = Topic.find(params[:node1_id][:node1])
|
2013-01-08 00:43:12 +00:00
|
|
|
end
|
2013-01-27 22:21:19 +00:00
|
|
|
if params[:node2_id] and params[:node2_id][:node2]
|
2013-02-04 02:12:15 +00:00
|
|
|
@synapse.topic2 = Topic.find(params[:node2_id][:node2])
|
2013-01-08 00:43:12 +00:00
|
|
|
end
|
2013-02-04 02:12:15 +00:00
|
|
|
@synapse.save
|
2013-04-26 04:07:29 +00:00
|
|
|
|
|
|
|
@permissionAfter = @synapse.permission
|
|
|
|
|
|
|
|
#push notify to anyone viewing this synapse on a map in realtime (see mapping.rb to understand the 'message' action)
|
|
|
|
# if the topic was private and is being switched to PU or CO it is the same as being created for other viewers
|
|
|
|
if @permissionBefore == "private" and @permissionAfter != "private"
|
|
|
|
@synapse.message 'create',@current.id
|
|
|
|
elsif @permissionBefore != "private" and @permissionAfter == "private"
|
|
|
|
@synapse.message 'destroy',@current.id
|
|
|
|
else
|
|
|
|
@synapse.message 'update',@current.id
|
|
|
|
end
|
2012-10-10 00:23:45 +00:00
|
|
|
end
|
|
|
|
|
2013-02-04 02:12:15 +00:00
|
|
|
respond_to do |format|
|
|
|
|
format.js
|
|
|
|
format.json { respond_with(@synapse) }
|
|
|
|
end
|
2012-10-10 00:23:45 +00:00
|
|
|
end
|
2013-01-06 03:40:10 +00:00
|
|
|
|
2013-01-06 04:37:24 +00:00
|
|
|
# POST mappings/:map_id/:synapse_id/removefrommap
|
|
|
|
def removefrommap
|
2013-04-26 04:07:29 +00:00
|
|
|
@user = current_user
|
|
|
|
|
2013-01-06 04:37:24 +00:00
|
|
|
@mapping = Mapping.find_by_synapse_id_and_map_id(params[:synapse_id],params[:map_id])
|
2013-04-26 04:07:29 +00:00
|
|
|
|
|
|
|
#push notify to anyone viewing same map in realtime (see mapping.rb to understand the 'message' action)
|
|
|
|
@mapping.message 'destroy',@user.id
|
|
|
|
|
2013-01-06 04:37:24 +00:00
|
|
|
@mapping.delete
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.js
|
|
|
|
end
|
|
|
|
end
|
2012-10-10 00:23:45 +00:00
|
|
|
|
2012-12-21 23:07:13 +00:00
|
|
|
# DELETE synapses/:id
|
2012-10-10 00:23:45 +00:00
|
|
|
def destroy
|
2013-01-06 03:40:10 +00:00
|
|
|
@current = current_user
|
|
|
|
@synapse = Synapse.find(params[:id]).authorize_to_edit(@current)
|
|
|
|
|
|
|
|
@synapse.mappings.each do |m|
|
2013-04-26 04:07:29 +00:00
|
|
|
#push notify to anyone viewing same map in realtime (see mapping.rb to understand the 'message' action)
|
|
|
|
m.message 'destroy',@current.id
|
|
|
|
|
2013-01-06 03:40:10 +00:00
|
|
|
m.delete
|
|
|
|
end
|
|
|
|
|
|
|
|
@synapse.delete if @synapse
|
2012-10-10 00:23:45 +00:00
|
|
|
end
|
|
|
|
end
|