2012-10-10 00:23:45 +00:00
|
|
|
class SynapsesController < ApplicationController
|
2012-10-26 10:04:52 +00:00
|
|
|
include ItemsHelper
|
2012-10-10 00:23:45 +00:00
|
|
|
|
|
|
|
before_filter :require_user, only: [:new, :create, :edit, :update]
|
|
|
|
|
|
|
|
respond_to :html, :js, :json
|
|
|
|
|
2012-10-26 10:04:52 +00:00
|
|
|
# GET users/:user_id/synapses
|
2012-10-10 00:23:45 +00:00
|
|
|
def index
|
2012-10-26 10:04:52 +00:00
|
|
|
@user = User.find(params[:user_id])
|
|
|
|
|
2012-10-27 08:30:56 +00:00
|
|
|
@current = current_user
|
|
|
|
@synapses = Synapse.visibleToUser(@current, @user)
|
|
|
|
@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-10-26 10:04:52 +00:00
|
|
|
# Get users/:user_id/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-10-26 10:04:52 +00:00
|
|
|
# GET users/:user_id/synapses/:id
|
2012-10-10 00:23:45 +00:00
|
|
|
def show
|
2012-10-26 10:04:52 +00:00
|
|
|
@user = User.find(params[:user_id])
|
|
|
|
|
2012-10-27 08:30:56 +00:00
|
|
|
@current = current_user
|
|
|
|
@synapse = @user.synapses.find(params[:id]).authorize_to_show(@current)
|
|
|
|
@item1 = @synapse.item1.authorize_to_show(@current)
|
|
|
|
@item2 = @synapse.item2.authorize_to_show(@current)
|
2012-10-10 00:23:45 +00:00
|
|
|
|
2012-10-27 08:30:56 +00:00
|
|
|
if @synapse && @item1 && @item2
|
2012-10-24 06:47:08 +00:00
|
|
|
@synapsejson = @synapse.selfplusnodes_as_json.html_safe
|
2012-10-27 08:30:56 +00:00
|
|
|
else
|
|
|
|
redirect_to root_url and return
|
2012-10-10 00:23:45 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2012-10-26 10:04:52 +00:00
|
|
|
# POST users/:user_id/synapses
|
2012-10-10 00:23:45 +00:00
|
|
|
def create
|
|
|
|
|
|
|
|
@user = current_user
|
|
|
|
@synapse = Synapse.new()
|
|
|
|
@synapse.desc = params[:synapse][:desc]
|
2012-11-04 00:14:21 +00:00
|
|
|
@synapse.category = params[:synapse][:category]
|
2012-10-18 00:51:54 +00:00
|
|
|
@synapse.item1 = Item.find(params[:node1_id])
|
|
|
|
@synapse.item2 = Item.find(params[:node2_id])
|
2012-10-26 10:04:52 +00:00
|
|
|
@synapse.permission = params[:synapse][:permission]
|
2012-10-18 00:51:54 +00:00
|
|
|
@synapse.user = @user
|
2012-10-10 00:23:45 +00:00
|
|
|
@synapse.save
|
2012-10-26 10:04:52 +00:00
|
|
|
|
|
|
|
if params[:synapse][:map]
|
|
|
|
@mapping = Mapping.new()
|
|
|
|
@mapping.category = "Synapse"
|
|
|
|
@mapping.user = @user
|
|
|
|
@mapping.map = Map.find(params[:synapse][:map])
|
|
|
|
@mapping.synapse = @synapse
|
|
|
|
@mapping.save
|
|
|
|
|
|
|
|
if not Map.find(params[:synapse][:map]).items.include?(@synapse.item1)
|
|
|
|
@mapping1 = Mapping.new()
|
|
|
|
@mapping1.category = "Item"
|
|
|
|
@mapping1.user = @user
|
|
|
|
@mapping1.map = Map.find(params[:synapse][:map])
|
|
|
|
@mapping1.item = @synapse.item1
|
|
|
|
@mapping1.save
|
|
|
|
end
|
|
|
|
if not Map.find(params[:synapse][:map]).items.include?(@synapse.item2)
|
|
|
|
@mapping2 = Mapping.new()
|
|
|
|
@mapping2.category = "Item"
|
|
|
|
@mapping2.user = @user
|
|
|
|
@mapping2.map = Map.find(params[:synapse][:map])
|
|
|
|
@mapping2.item = @synapse.item2
|
|
|
|
@mapping2.save
|
|
|
|
end
|
|
|
|
end
|
2012-10-10 00:23:45 +00:00
|
|
|
|
|
|
|
respond_to do |format|
|
2012-10-26 10:04:52 +00:00
|
|
|
format.html { respond_with(@user, location: user_synapse_url(@user, @synapse)) }
|
2012-12-10 00:42:45 +00:00
|
|
|
format.js { respond_with(@synapse) }
|
2012-10-10 00:23:45 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2012-10-26 10:04:52 +00:00
|
|
|
# GET users/:user_id/synapses/:id/edit
|
2012-10-10 00:23:45 +00:00
|
|
|
def edit
|
2012-10-26 10:04:52 +00:00
|
|
|
@user = User.find(params[:user_id])
|
|
|
|
|
2012-10-27 08:30:56 +00:00
|
|
|
@current = current_user
|
|
|
|
@synapse = @user.synapses.find(params[:id]).authorize_to_edit(@current)
|
2012-10-10 00:23:45 +00:00
|
|
|
|
2012-10-18 00:51:54 +00:00
|
|
|
if @synapse
|
2012-11-04 00:14:21 +00:00
|
|
|
@items = Item.visibleToUser(@current, nil)
|
2012-10-27 08:30:56 +00:00
|
|
|
elsif not @synapse
|
|
|
|
redirect_to root_url and return
|
2012-10-10 00:23:45 +00:00
|
|
|
end
|
|
|
|
|
2012-10-18 00:51:54 +00:00
|
|
|
respond_with(@synapse, @items)
|
2012-10-10 00:23:45 +00:00
|
|
|
end
|
|
|
|
|
2012-10-26 10:04:52 +00:00
|
|
|
# PUT users/:user_id/synapses/:id
|
2012-10-10 00:23:45 +00:00
|
|
|
def update
|
2012-10-26 10:04:52 +00:00
|
|
|
@user = User.find(params[:user_id])
|
|
|
|
|
|
|
|
@synapse = @user.synapses.find(params[:id])
|
2012-10-10 00:23:45 +00:00
|
|
|
|
|
|
|
if @synapse
|
|
|
|
@synapse.desc = params[:synapse][:desc]
|
2012-11-04 00:14:21 +00:00
|
|
|
@synapse.category = params[:synapse][:category]
|
2012-10-18 00:51:54 +00:00
|
|
|
@synapse.item1 = Item.find(params[:node1_id][:node1])
|
|
|
|
@synapse.item2 = Item.find(params[:node2_id][:node2])
|
2012-10-26 10:04:52 +00:00
|
|
|
@synapse.permission = params[:synapse][:permission]
|
2012-10-10 00:23:45 +00:00
|
|
|
@synapse.save
|
|
|
|
end
|
|
|
|
|
2012-10-26 10:04:52 +00:00
|
|
|
respond_with(@user, location: user_synapse_url(@user, @synapse)) do |format|
|
2012-10-10 00:23:45 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2012-10-26 10:04:52 +00:00
|
|
|
# DELETE users/:user_id/synapses/:id
|
2012-10-10 00:23:45 +00:00
|
|
|
def destroy
|
2012-10-26 10:04:52 +00:00
|
|
|
@user = User.find(params[:user_id])
|
|
|
|
|
|
|
|
@synapse = @user.synapses.find(params[:id])
|
2012-10-10 00:23:45 +00:00
|
|
|
|
|
|
|
@synapse.delete
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|