2012-10-26 10:04:52 +00:00
|
|
|
class MapsController < ApplicationController
|
2012-10-27 08:30:56 +00:00
|
|
|
|
2012-12-01 23:05:17 +00:00
|
|
|
before_filter :require_user, only: [:new, :create, :edit, :update, :savelayout]
|
2012-10-26 10:04:52 +00:00
|
|
|
|
|
|
|
respond_to :html, :js, :json
|
|
|
|
|
|
|
|
# GET /users/:user_id/maps
|
|
|
|
def index
|
|
|
|
@user = User.find(params[:user_id])
|
2012-10-27 08:30:56 +00:00
|
|
|
|
|
|
|
@current = current_user
|
|
|
|
@maps = Map.visibleToUser(@current, @user)
|
2012-10-26 10:04:52 +00:00
|
|
|
|
|
|
|
respond_with(@maps,@user)
|
|
|
|
end
|
|
|
|
|
|
|
|
# GET /users/:user_id/maps/new
|
|
|
|
def new
|
|
|
|
@map = Map.new
|
|
|
|
@user = current_user
|
|
|
|
|
|
|
|
respond_with(@map)
|
|
|
|
end
|
|
|
|
|
|
|
|
# GET Get /users/:user_id/maps/:id
|
|
|
|
def show
|
|
|
|
@user = User.find(params[:user_id])
|
|
|
|
|
2012-10-27 08:30:56 +00:00
|
|
|
@current = current_user
|
|
|
|
@map = @user.maps.find(params[:id]).authorize_to_show(@current)
|
2012-10-26 10:04:52 +00:00
|
|
|
|
2012-10-27 08:30:56 +00:00
|
|
|
if not @map
|
|
|
|
redirect_to root_url and return
|
|
|
|
end
|
|
|
|
|
|
|
|
@mapjson = @map.self_as_json(@current).html_safe
|
2012-10-26 10:04:52 +00:00
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html { respond_with(@map, @user) }
|
|
|
|
format.json { respond_with(@mapjson) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# POST /users/:user_id/maps
|
|
|
|
def create
|
|
|
|
|
|
|
|
@user = current_user
|
|
|
|
@map = Map.create(params[:map])
|
|
|
|
@map.user = @user
|
2012-12-03 23:40:14 +00:00
|
|
|
@map.arranged = false
|
2012-10-26 10:04:52 +00:00
|
|
|
@map.save
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html { respond_with(@user, location: user_map_path(@user, @map)) }
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
# GET /users/:user_id/maps/:id/edit
|
|
|
|
def edit
|
|
|
|
@user = User.find(params[:user_id])
|
|
|
|
|
2012-10-27 08:30:56 +00:00
|
|
|
@current = current_user
|
|
|
|
@map = @user.maps.find(params[:id]).authorize_to_edit(@current)
|
|
|
|
|
|
|
|
if not @map
|
|
|
|
redirect_to root_url and return
|
|
|
|
end
|
2012-10-30 03:40:38 +00:00
|
|
|
|
|
|
|
@outitems = @map.items.order("name ASC").delete_if{|item| not item.authorize_to_view(@current)}
|
2012-10-26 10:04:52 +00:00
|
|
|
|
2012-10-30 03:40:38 +00:00
|
|
|
respond_with(@user, @map, @outitems)
|
2012-10-26 10:04:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# PUT /users/:user_id/maps/:id
|
|
|
|
def update
|
|
|
|
@user = User.find(params[:user_id])
|
|
|
|
|
|
|
|
@map = @user.maps.find(params[:id])
|
|
|
|
|
|
|
|
@map.attributes = params[:map]
|
|
|
|
@map.save
|
|
|
|
|
2012-10-30 03:40:38 +00:00
|
|
|
if params[:outitems]
|
|
|
|
@outitems = params[:outitems]
|
|
|
|
@outitems.each do |item|
|
|
|
|
@mapping = Mapping.where("map_id = ? AND item_id = ?", @map.id, item).first
|
|
|
|
@mapping.delete
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-10-26 10:04:52 +00:00
|
|
|
respond_with(@user, location: user_map_path(@user, @map)) do |format|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2012-12-01 23:05:17 +00:00
|
|
|
# PUT /users/:user_id/maps/:id/savelayout
|
|
|
|
def savelayout
|
|
|
|
@user = User.find(params[:user_id])
|
|
|
|
|
|
|
|
@map = @user.maps.find(params[:id])
|
|
|
|
|
|
|
|
if params[:map][:coordinates]
|
|
|
|
@all = params[:map][:coordinates]
|
|
|
|
@all = @all.split(',')
|
|
|
|
@all.each do |item|
|
|
|
|
item = item.split('/')
|
|
|
|
@mapping = Mapping.find(item[0])
|
|
|
|
@mapping.xloc = item[1]
|
|
|
|
@mapping.yloc = item[2]
|
|
|
|
@mapping.save
|
|
|
|
end
|
2012-12-03 23:40:14 +00:00
|
|
|
@map.arranged = true
|
|
|
|
@map.save
|
2012-12-01 23:05:17 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-10-26 10:04:52 +00:00
|
|
|
# DELETE /users/:user_id/maps/:id
|
|
|
|
def destroy
|
|
|
|
@user = User.find(params[:user_id])
|
|
|
|
|
|
|
|
@map = @user.maps.find(params[:id])
|
|
|
|
|
|
|
|
@mappings = @map.mappings
|
|
|
|
|
|
|
|
@mappings.each do |mapping|
|
|
|
|
mapping.delete
|
|
|
|
end
|
|
|
|
|
|
|
|
@map.delete
|
|
|
|
|
2012-12-03 23:40:14 +00:00
|
|
|
respond_to do |format|
|
|
|
|
format.js
|
2012-10-26 10:04:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|