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-10-26 10:04:52 +00:00
|
|
|
# GET /users/:user_id/items
|
2012-10-18 18:24:07 +00:00
|
|
|
def index
|
2012-12-14 18:31:39 +00:00
|
|
|
@user = User.find(params[:user_id])
|
2012-10-26 10:04:52 +00:00
|
|
|
|
2012-12-14 18:31:39 +00:00
|
|
|
@current = current_user
|
2012-10-27 08:30:56 +00:00
|
|
|
@items = Item.visibleToUser(@current, @user)
|
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-10-26 10:04:52 +00:00
|
|
|
# Get /users/:user_id/items/new
|
2012-10-18 18:24:07 +00:00
|
|
|
def new
|
|
|
|
@item = Item.new
|
|
|
|
@user = current_user
|
|
|
|
|
|
|
|
respond_with(@item)
|
|
|
|
end
|
|
|
|
|
2012-10-26 10:04:52 +00:00
|
|
|
# GET /users/:user_id/items/:id
|
2012-10-18 18:24:07 +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
|
|
|
|
@item = @user.items.find(params[:id]).authorize_to_show(@current)
|
2012-10-18 18:24:07 +00:00
|
|
|
|
2012-10-27 08:30:56 +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
|
|
|
|
|
|
|
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-10-26 10:04:52 +00:00
|
|
|
# POST /users/:user_id/items
|
2012-10-18 18:24:07 +00:00
|
|
|
def create
|
|
|
|
|
|
|
|
@user = current_user
|
|
|
|
|
2012-10-31 04:25:56 +00:00
|
|
|
@addtomap = false
|
|
|
|
|
|
|
|
if params[:initem]
|
|
|
|
if params[:initem] != ""
|
|
|
|
@addtomap = true
|
|
|
|
@itemid = params[:initem]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if @addtomap
|
|
|
|
@item = Item.find(@itemid)
|
2012-10-30 03:40:38 +00:00
|
|
|
else
|
|
|
|
@item = Item.new()
|
|
|
|
@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.user = @user
|
|
|
|
|
|
|
|
@item.save
|
|
|
|
end
|
2012-10-26 10:04:52 +00:00
|
|
|
|
2012-12-01 23:05:17 +00:00
|
|
|
@mapping = nil
|
2012-10-26 10:04:52 +00:00
|
|
|
if params[:item][:map]
|
|
|
|
@mapping = Mapping.new()
|
|
|
|
@mapping.category = "Item"
|
|
|
|
@mapping.user = @user
|
|
|
|
@mapping.map = Map.find(params[:item][:map])
|
|
|
|
@mapping.item = @item
|
2012-12-01 23:05:17 +00:00
|
|
|
@mapping.xloc = 0
|
|
|
|
@mapping.yloc = 0
|
2012-10-26 10:04:52 +00:00
|
|
|
@mapping.save
|
|
|
|
end
|
2012-10-18 18:24:07 +00:00
|
|
|
|
|
|
|
respond_to do |format|
|
2012-10-26 10:04:52 +00:00
|
|
|
format.html { respond_with(@user, location: user_item_url(@user, @item)) }
|
2012-12-01 23:05:17 +00:00
|
|
|
format.js { respond_with(@item, @mapping) }
|
2012-10-18 18:24:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2012-10-26 10:04:52 +00:00
|
|
|
# GET /users/:user_id/items/:id/edit
|
2012-10-18 18:24:07 +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
|
|
|
|
@item = @user.items.find(params[:id]).authorize_to_edit(@current)
|
|
|
|
|
|
|
|
if not @item
|
|
|
|
redirect_to root_url and return
|
|
|
|
end
|
2012-10-18 18:24:07 +00:00
|
|
|
|
|
|
|
respond_with(@item)
|
|
|
|
end
|
|
|
|
|
2012-10-26 10:04:52 +00:00
|
|
|
# PUT /users/:user_id/items/:id
|
2012-10-18 18:24:07 +00:00
|
|
|
def update
|
2012-10-26 10:04:52 +00:00
|
|
|
@user = User.find(params[:user_id])
|
|
|
|
|
|
|
|
@item = @user.items.find(params[:id])
|
2012-10-18 18:24:07 +00:00
|
|
|
|
|
|
|
if @item
|
|
|
|
@item.name = params[:item][:name]
|
|
|
|
@item.desc = params[:item][:desc]
|
|
|
|
@item.link = params[:item][:link]
|
2012-10-26 10:04:52 +00:00
|
|
|
@item.permission = params[:item][:permission]
|
2012-10-18 18:24:07 +00:00
|
|
|
@item.item_category = ItemCategory.find(params[:category][:item_category_id])
|
|
|
|
|
|
|
|
@item.save
|
|
|
|
end
|
|
|
|
|
2012-10-26 10:04:52 +00:00
|
|
|
respond_with(@user, location: user_item_url(@user, @item)) do |format|
|
2012-10-18 18:24:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2012-10-26 10:04:52 +00:00
|
|
|
# DELETE /users/:user_id/items/:id
|
2012-10-18 18:24:07 +00:00
|
|
|
def destroy
|
2012-10-26 10:04:52 +00:00
|
|
|
@user = User.find(params[:user_id])
|
|
|
|
|
|
|
|
@item = @user.items.find(params[:id])
|
2012-10-18 18:24:07 +00:00
|
|
|
|
|
|
|
@synapses = @item.synapses
|
2012-10-26 10:04:52 +00:00
|
|
|
@mappings = @item.mappings
|
2012-10-18 18:24:07 +00:00
|
|
|
|
|
|
|
@synapses.each do |synapse|
|
|
|
|
synapse.delete
|
|
|
|
end
|
|
|
|
|
2012-10-26 10:04:52 +00:00
|
|
|
@mappings.each do |mapping|
|
|
|
|
mapping.delete
|
|
|
|
end
|
|
|
|
|
2012-10-18 18:24:07 +00:00
|
|
|
@item.delete
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.js
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|