metamaps--metamaps/app/controllers/synapses_controller.rb

127 lines
3.1 KiB
Ruby
Raw Normal View History

2012-10-10 00:23:45 +00:00
class SynapsesController < ApplicationController
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-12-21 00:24:27 +00:00
autocomplete :synapse, :desc, :full => true
# GET synapses
# or GET users/:user_id/synapses
2012-10-10 00:23:45 +00:00
def index
@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
respond_to do |format|
format.html
format.json { respond_with(@synapsesjson) }
end
2012-10-10 00:23:45 +00:00
end
# 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
respond_with(@synapse)
2012-10-10 00:23:45 +00:00
end
# GET synapses/:id
2012-10-10 00:23:45 +00:00
def show
@current = current_user
@synapse = Synapse.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
if @synapse && @item1 && @item2
@synapsejson = @synapse.selfplusnodes_as_json.html_safe
else
redirect_to root_url and return
end
2012-10-10 00:23:45 +00:00
respond_to do |format|
format.html
format.json { respond_with(@synapsejson) }
2012-10-10 00:23:45 +00:00
end
end
# POST synapses
2012-10-10 00:23:45 +00:00
def create
@user = current_user
@synapse = Synapse.new()
@synapse.desc = params[:synapse][:desc]
@synapse.item1 = Item.find(params[:synapse][:item1id])
@synapse.item2 = Item.find(params[:synapse][:item2id])
@synapse.permission = "commons"
@synapse.category = "from-to"
@synapse.weight = 5
@synapse.user = @user
@synapse.save
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
end
2012-10-10 00:23:45 +00:00
respond_to do |format|
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
# GET synapses/:id/edit
2012-10-10 00:23:45 +00:00
def edit
@current = current_user
@synapse = Synapse.find(params[:id]).authorize_to_edit(@current)
2012-10-10 00:23:45 +00:00
if @synapse
@items = Item.visibleToUser(@current, nil)
elsif not @synapse
redirect_to root_url and return
end
2012-10-10 00:23:45 +00:00
respond_with(@synapse, @items)
2012-10-10 00:23:45 +00:00
end
# PUT synapses/:id
2012-10-10 00:23:45 +00:00
def update
@current = current_user
@synapse = Synapse.find(params[:id]).authorize_to_edit(@current)
2012-10-10 00:23:45 +00:00
if @synapse
@synapse.desc = params[:synapse][:desc]
@synapse.category = params[:synapse][:category]
@synapse.item1 = Item.find(params[:node1_id][:node1])
@synapse.item2 = Item.find(params[:node2_id][:node2])
@synapse.permission = params[:synapse][:permission]
@synapse.save
2012-10-10 00:23:45 +00:00
end
respond_with(@user, location: synapse_url(@synapse)) do |format|
2012-10-10 00:23:45 +00:00
end
end
# DELETE synapses/:id
2012-10-10 00:23:45 +00:00
def destroy
@current = current_user
@synapse = Synapse.find(params[:id]).authorize_to_edit(@current)
2012-10-10 00:23:45 +00:00
@synapse.delete if @synapse
2012-10-10 00:23:45 +00:00
end
end