2019-01-16 16:28:25 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# API Controller for resources of type Space
|
2023-02-24 17:26:55 +01:00
|
|
|
class API::SpacesController < API::APIController
|
2019-01-16 16:28:25 +01:00
|
|
|
before_action :authenticate_user!, except: %i[index show]
|
2022-11-23 11:59:06 +01:00
|
|
|
before_action :set_space, only: %i[update destroy]
|
2017-02-13 16:10:12 +01:00
|
|
|
respond_to :json
|
|
|
|
|
|
|
|
def index
|
2023-07-28 16:16:44 +02:00
|
|
|
@spaces = Space.includes(:space_image, :machines).where(deleted_at: nil)
|
|
|
|
@spaces_indexed_with_parent = @spaces.index_with { |space| @spaces.find { |s| s.id == space.parent_id } }
|
|
|
|
@spaces_grouped_by_parent_id = @spaces.group_by(&:parent_id)
|
2017-02-13 16:10:12 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
|
|
|
@space = Space.includes(:space_files, :projects).friendly.find(params[:id])
|
2022-11-22 14:17:25 +01:00
|
|
|
|
|
|
|
head :not_found if @space.deleted_at
|
2017-02-13 16:10:12 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
authorize Space
|
|
|
|
@space = Space.new(space_params)
|
|
|
|
if @space.save
|
2023-07-28 16:16:44 +02:00
|
|
|
update_space_children(@space, params[:space][:child_ids])
|
2017-02-13 16:10:12 +01:00
|
|
|
render :show, status: :created, location: @space
|
|
|
|
else
|
|
|
|
render json: @space.errors, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2022-12-12 14:19:57 +01:00
|
|
|
authorize @space
|
2017-02-13 16:10:12 +01:00
|
|
|
if @space.update(space_params)
|
2023-07-28 16:16:44 +02:00
|
|
|
update_space_children(@space, params[:space][:child_ids])
|
2017-02-13 16:10:12 +01:00
|
|
|
render :show, status: :ok, location: @space
|
|
|
|
else
|
|
|
|
render json: @space.errors, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
authorize @space
|
2022-11-23 11:59:06 +01:00
|
|
|
method = @space.destroyable? ? :destroy : :soft_destroy!
|
|
|
|
@space.send(method)
|
2017-02-13 16:10:12 +01:00
|
|
|
head :no_content
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2022-11-23 11:59:06 +01:00
|
|
|
def set_space
|
2022-12-12 14:19:57 +01:00
|
|
|
@space = Space.friendly.find(params[:id])
|
2019-01-16 16:28:25 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def space_params
|
|
|
|
params.require(:space).permit(:name, :description, :characteristics, :default_places, :disabled,
|
2023-07-28 16:16:44 +02:00
|
|
|
machine_ids: [],
|
2023-01-09 10:45:37 +01:00
|
|
|
space_image_attributes: %i[id attachment],
|
2022-11-10 16:14:49 +01:00
|
|
|
space_files_attributes: %i[id attachment _destroy],
|
|
|
|
advanced_accounting_attributes: %i[code analytical_section])
|
2019-01-16 16:28:25 +01:00
|
|
|
end
|
2023-07-28 16:16:44 +02:00
|
|
|
|
|
|
|
def update_space_children(parent_space, child_ids)
|
|
|
|
Space.transaction do
|
|
|
|
parent_space.children.each { |child| child.update!(parent: nil) }
|
|
|
|
child_ids.to_a.select(&:present?).each do |child_id|
|
|
|
|
Space.find(child_id).update!(parent: parent_space)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-02-13 16:10:12 +01:00
|
|
|
end
|