2012-10-18 18:24:07 +00:00
|
|
|
class ItemsController < ApplicationController
|
|
|
|
|
|
|
|
before_filter :require_user, only: [:new, :create, :edit, :update]
|
|
|
|
|
|
|
|
respond_to :html, :js, :json
|
|
|
|
|
|
|
|
# GET /items
|
|
|
|
def index
|
|
|
|
@user = current_user
|
|
|
|
@items = Item.all
|
|
|
|
|
|
|
|
respond_with(@items)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Get /item/new
|
|
|
|
def new
|
|
|
|
@item = Item.new
|
|
|
|
@user = current_user
|
|
|
|
|
|
|
|
respond_with(@item)
|
|
|
|
end
|
|
|
|
|
|
|
|
# GET /item/:id
|
|
|
|
def show
|
|
|
|
@item = Item.find(params[:id])
|
|
|
|
|
2012-10-19 04:06:16 +00:00
|
|
|
@relatives = @item.map_as_json.html_safe
|
2012-10-18 18:24:07 +00:00
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html { respond_with(@item) }
|
|
|
|
format.json { respond_with(@relatives) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# POST /items
|
|
|
|
def create
|
|
|
|
|
|
|
|
@user = current_user
|
|
|
|
@item = Item.new()
|
|
|
|
@item.name = params[:item][:name]
|
|
|
|
@item.desc = params[:item][:desc]
|
|
|
|
@item.link = params[:item][:link]
|
|
|
|
@item.item_category = ItemCategory.find(params[:category])
|
|
|
|
@item.user = @user
|
|
|
|
|
|
|
|
@item.save
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html { respond_with(@user, location: item_url(@item)) }
|
|
|
|
format.js { respond_with(@item) }
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
# GET /items/:id/edit
|
|
|
|
def edit
|
|
|
|
@item = Item.find_by_id(params[:id])
|
|
|
|
|
|
|
|
respond_with(@item)
|
|
|
|
end
|
|
|
|
|
|
|
|
# PUT /actions/:id
|
|
|
|
def update
|
|
|
|
@item = Item.find_by_id(params[:id])
|
|
|
|
|
|
|
|
if @item
|
|
|
|
@item.name = params[:item][:name]
|
|
|
|
@item.desc = params[:item][:desc]
|
|
|
|
@item.link = params[:item][:link]
|
|
|
|
@item.item_category = ItemCategory.find(params[:category][:item_category_id])
|
|
|
|
|
|
|
|
@item.save
|
|
|
|
end
|
|
|
|
|
|
|
|
respond_with(@user, location: item_url(@item)) do |format|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
# DELETE /items/:id
|
|
|
|
def destroy
|
|
|
|
@item = Item.find_by_id(params[:id])
|
|
|
|
|
|
|
|
@synapses = @item.synapses
|
|
|
|
|
|
|
|
@synapses.each do |synapse|
|
|
|
|
synapse.delete
|
|
|
|
end
|
|
|
|
|
|
|
|
@item.delete
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.js
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|