2012-10-18 18:24:07 +00:00
|
|
|
class ItemsController < ApplicationController
|
2012-10-27 08:30:56 +00:00
|
|
|
|
2012-10-18 18:24:07 +00:00
|
|
|
before_filter :require_user, only: [:new, :create, :edit, :update]
|
|
|
|
|
|
|
|
respond_to :html, :js, :json
|
|
|
|
|
2012-12-14 07:16:17 +00:00
|
|
|
autocomplete :item, :name, :full => true, :extra_data => [:user_id]
|
|
|
|
|
2012-12-14 07:22:41 +00:00
|
|
|
|
2012-12-21 23:07:13 +00:00
|
|
|
# GET items
|
|
|
|
# or GET /users/:user_id/items
|
2012-10-18 18:24:07 +00:00
|
|
|
def index
|
2012-12-14 18:31:39 +00:00
|
|
|
@current = current_user
|
2012-12-21 23:07:13 +00:00
|
|
|
|
|
|
|
if params[:user_id]
|
|
|
|
@user = User.find(params[:user_id])
|
|
|
|
@items = Item.visibleToUser(@current, @user)
|
|
|
|
elsif
|
|
|
|
@items = Item.visibleToUser(@current, nil)
|
|
|
|
end
|
2012-10-18 18:24:07 +00:00
|
|
|
|
2012-10-26 10:04:52 +00:00
|
|
|
respond_with(@user,@items)
|
2012-10-18 18:24:07 +00:00
|
|
|
end
|
|
|
|
|
2012-12-21 23:07:13 +00:00
|
|
|
# Get items/new
|
2012-10-18 18:24:07 +00:00
|
|
|
def new
|
|
|
|
@item = Item.new
|
|
|
|
@user = current_user
|
|
|
|
|
|
|
|
respond_with(@item)
|
|
|
|
end
|
|
|
|
|
2012-12-21 23:07:13 +00:00
|
|
|
# GET items/:id
|
2012-10-18 18:24:07 +00:00
|
|
|
def show
|
2012-12-21 23:07:13 +00:00
|
|
|
@current = current_user
|
|
|
|
@item = Item.find(params[:id]).authorize_to_show(@current)
|
2012-10-18 18:24:07 +00:00
|
|
|
|
2012-12-21 23:07:13 +00:00
|
|
|
if @item
|
|
|
|
@relatives = @item.network_as_json(@current).html_safe
|
|
|
|
else
|
|
|
|
redirect_to root_url and return
|
|
|
|
end
|
2012-10-18 18:24:07 +00:00
|
|
|
|
2012-12-21 23:07:13 +00:00
|
|
|
respond_to do |format|
|
2012-10-21 17:19:59 +00:00
|
|
|
format.html { respond_with(@item, @user) }
|
2012-10-18 18:24:07 +00:00
|
|
|
format.json { respond_with(@relatives) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-21 23:07:13 +00:00
|
|
|
# POST items
|
2012-10-18 18:24:07 +00:00
|
|
|
def create
|
|
|
|
|
|
|
|
@user = current_user
|
2012-12-16 20:00:43 +00:00
|
|
|
|
|
|
|
# if the topic exists grab it and return it
|
|
|
|
if params[:item][:grabItem] != "null"
|
|
|
|
@item = Item.find(params[:item][:grabItem])
|
|
|
|
# if the topic doesn't exist yet, create it
|
|
|
|
else
|
|
|
|
@item = Item.new()
|
|
|
|
@item.name = params[:item][:name]
|
|
|
|
@item.desc = ""
|
|
|
|
@item.link = ""
|
|
|
|
@item.permission = 'commons'
|
|
|
|
@item.item_category = ItemCategory.all.first
|
|
|
|
#@item.item_category = ItemCategory.find(params[:category])
|
|
|
|
@item.user = @user
|
2012-12-16 00:46:39 +00:00
|
|
|
|
2012-12-16 20:00:43 +00:00
|
|
|
@item.save
|
2012-12-21 00:24:27 +00:00
|
|
|
end
|
|
|
|
|
2012-12-21 04:45:44 +00:00
|
|
|
# pass on to the item create js whether it's being created with a synapse
|
|
|
|
@synapse = "false"
|
|
|
|
if params[:item][:addSynapse] == "true"
|
|
|
|
@synapse = "true"
|
2012-10-30 03:40:38 +00:00
|
|
|
end
|
2012-12-16 00:46:39 +00:00
|
|
|
|
2012-12-16 20:00:43 +00:00
|
|
|
# also create an object to return the position to the canvas
|
2012-12-16 00:46:39 +00:00
|
|
|
@position = Hash.new()
|
2012-12-16 06:12:41 +00:00
|
|
|
@position['x'] = params[:item][:x]
|
|
|
|
@position['y'] = params[:item][:y]
|
2012-12-16 20:00:43 +00:00
|
|
|
|
|
|
|
# set this for the case where the item is being created on a map.
|
|
|
|
@mapping = Mapping.new()
|
|
|
|
if params[:item][:map]
|
|
|
|
@mapping.category = "Item"
|
|
|
|
@mapping.user = @user
|
|
|
|
@mapping.map = Map.find(params[:item][:map])
|
|
|
|
@mapping.item = @item
|
|
|
|
@mapping.xloc = params[:item][:x]
|
|
|
|
@mapping.yloc = params[:item][:y]
|
|
|
|
@mapping.save
|
|
|
|
end
|
2012-10-18 18:24:07 +00:00
|
|
|
|
|
|
|
respond_to do |format|
|
2012-12-21 23:07:13 +00:00
|
|
|
format.html { respond_with(@user, location: item_url(@item)) }
|
2012-12-21 00:24:27 +00:00
|
|
|
format.js { respond_with(@item, @mapping, @synapse, @position) }
|
2012-10-18 18:24:07 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-21 23:07:13 +00:00
|
|
|
# GET items/:id/edit
|
2012-10-18 18:24:07 +00:00
|
|
|
def edit
|
2012-12-21 23:07:13 +00:00
|
|
|
@current = current_user
|
|
|
|
@item = Item.find(params[:id]).authorize_to_edit(@current)
|
2012-10-27 08:30:56 +00:00
|
|
|
|
2012-12-21 23:07:13 +00:00
|
|
|
if not @item
|
|
|
|
redirect_to root_url and return
|
|
|
|
end
|
2012-10-18 18:24:07 +00:00
|
|
|
|
2012-12-21 23:07:13 +00:00
|
|
|
respond_with(@item)
|
2012-10-18 18:24:07 +00:00
|
|
|
end
|
|
|
|
|
2012-12-21 23:07:13 +00:00
|
|
|
# PUT items/:id
|
2012-10-18 18:24:07 +00:00
|
|
|
def update
|
2012-12-21 23:07:13 +00:00
|
|
|
@current = current_user
|
|
|
|
@item = Item.find(params[:id]).authorize_to_edit(@current)
|
2012-10-18 18:24:07 +00:00
|
|
|
|
2012-12-21 23:07:13 +00:00
|
|
|
if @item
|
|
|
|
@item.name = params[:item][:name]
|
|
|
|
@item.desc = params[:item][:desc]
|
|
|
|
@item.link = params[:item][:link]
|
|
|
|
@item.permission = params[:item][:permission]
|
|
|
|
@item.item_category = ItemCategory.find(params[:category][:item_category_id])
|
|
|
|
@item.save
|
2012-10-18 18:24:07 +00:00
|
|
|
end
|
|
|
|
|
2012-12-21 23:07:13 +00:00
|
|
|
respond_with(@user, location: item_url(@item)) do |format|
|
2012-10-18 18:24:07 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-21 23:07:13 +00:00
|
|
|
# DELETE items/:id
|
2012-10-18 18:24:07 +00:00
|
|
|
def destroy
|
2012-12-21 23:07:13 +00:00
|
|
|
@current = current_user
|
|
|
|
@item = Item.find(params[:id]).authorize_to_edit(@current)
|
|
|
|
|
|
|
|
if @item
|
|
|
|
@synapses = @item.synapses
|
|
|
|
@mappings = @item.mappings
|
|
|
|
|
|
|
|
@synapses.each do |synapse|
|
|
|
|
synapse.delete
|
|
|
|
end
|
|
|
|
|
|
|
|
@mappings.each do |mapping|
|
|
|
|
mapping.delete
|
|
|
|
end
|
|
|
|
|
|
|
|
@item.delete
|
|
|
|
end
|
|
|
|
|
|
|
|
respond_to do |format|
|
2012-10-18 18:24:07 +00:00
|
|
|
format.js
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|