metamaps--metamaps/app/controllers/items_controller.rb

154 lines
3.2 KiB
Ruby
Raw Normal View History

2012-10-18 18:24:07 +00:00
class ItemsController < ApplicationController
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
# GET /users/:user_id/items
2012-10-18 18:24:07 +00:00
def index
@user = User.find(params[:user_id])
@current = current_user
@items = Item.visibleToUser(@current, @user)
2012-10-18 18:24:07 +00:00
respond_with(@user,@items)
2012-10-18 18:24:07 +00:00
end
# 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
# GET /users/:user_id/items/:id
2012-10-18 18:24:07 +00:00
def show
@user = User.find(params[:user_id])
@current = current_user
@item = @user.items.find(params[:id]).authorize_to_show(@current)
2012-10-18 18:24:07 +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|
format.html { respond_with(@item, @user) }
2012-10-18 18:24:07 +00:00
format.json { respond_with(@relatives) }
end
end
# POST /users/:user_id/items
2012-10-18 18:24:07 +00:00
def create
@user = current_user
@addtomap = false
if params[:initem]
if params[:initem] != ""
@addtomap = true
@itemid = params[:initem]
end
end
if @addtomap
@item = Item.find(@itemid)
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-12-01 23:05:17 +00:00
@mapping = nil
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
@mapping.save
end
2012-10-18 18:24:07 +00:00
respond_to do |format|
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
# GET /users/:user_id/items/:id/edit
2012-10-18 18:24:07 +00:00
def edit
@user = User.find(params[:user_id])
@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
# PUT /users/:user_id/items/:id
2012-10-18 18:24:07 +00:00
def update
@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]
@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
respond_with(@user, location: user_item_url(@user, @item)) do |format|
2012-10-18 18:24:07 +00:00
end
end
# DELETE /users/:user_id/items/:id
2012-10-18 18:24:07 +00:00
def destroy
@user = User.find(params[:user_id])
@item = @user.items.find(params[:id])
2012-10-18 18:24:07 +00:00
@synapses = @item.synapses
@mappings = @item.mappings
2012-10-18 18:24:07 +00:00
@synapses.each do |synapse|
synapse.delete
end
@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