2017-02-13 16:10:12 +01:00
|
|
|
class API::SpacesController < API::ApiController
|
|
|
|
before_action :authenticate_user!, except: [:index, :show]
|
|
|
|
respond_to :json
|
|
|
|
|
|
|
|
def index
|
|
|
|
@spaces = Space.includes(:space_image)
|
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
|
|
|
@space = Space.includes(:space_files, :projects).friendly.find(params[:id])
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
authorize Space
|
|
|
|
@space = Space.new(space_params)
|
|
|
|
if @space.save
|
|
|
|
render :show, status: :created, location: @space
|
|
|
|
else
|
|
|
|
render json: @space.errors, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
authorize Space
|
|
|
|
@space = get_space
|
|
|
|
if @space.update(space_params)
|
|
|
|
render :show, status: :ok, location: @space
|
|
|
|
else
|
|
|
|
render json: @space.errors, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
authorize @space
|
|
|
|
@space.destroy
|
|
|
|
head :no_content
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def get_space
|
|
|
|
Space.friendly.find(params[:id])
|
|
|
|
end
|
|
|
|
|
|
|
|
def space_params
|
2017-02-14 11:28:07 +01:00
|
|
|
params.require(:space).permit(:name, :description, :characteristics, :default_places, space_image_attributes: [:attachment],
|
2017-02-13 16:10:12 +01:00
|
|
|
space_files_attributes: [:id, :attachment, :_destroy])
|
|
|
|
end
|
|
|
|
end
|