2023-11-16 20:23:09 +00:00
|
|
|
class JobsController < ApplicationController
|
|
|
|
before_action :set_project, only: [:index, :new, :show, :edit]
|
|
|
|
before_action :set_job, only: [:show, :edit, :update, :destroy]
|
|
|
|
|
|
|
|
def index
|
2023-11-22 20:11:25 +00:00
|
|
|
@jobs = Job.project_or_parent(@project)
|
2023-11-16 20:23:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
|
|
|
end
|
|
|
|
|
|
|
|
def new
|
2023-11-24 15:05:38 +00:00
|
|
|
@job = Job.new.with_all_time_budgets
|
2023-11-16 20:23:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def edit
|
2023-11-24 15:05:38 +00:00
|
|
|
@job.with_all_time_budgets
|
2023-11-16 20:23:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2023-11-24 15:05:38 +00:00
|
|
|
if @job.update(remove_empty_time_budgets(job_params))
|
2023-11-21 14:09:51 +00:00
|
|
|
redirect_to job_path(@job, project_id: @job.project.id)
|
2023-11-16 20:23:09 +00:00
|
|
|
else
|
|
|
|
render :edit
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
if @job = Job.create(job_params)
|
2023-11-21 14:09:51 +00:00
|
|
|
redirect_to job_path(@job, project_id: @job.project.id)
|
2023-11-16 20:23:09 +00:00
|
|
|
else
|
|
|
|
render :edit
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
2023-11-21 14:09:51 +00:00
|
|
|
@project = @job.project
|
2023-11-16 20:23:09 +00:00
|
|
|
if @job.destroy
|
|
|
|
redirect_to jobs_path(project_id: @project.id)
|
|
|
|
else
|
|
|
|
render :show
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def job_params
|
|
|
|
params.require(:job).permit(
|
|
|
|
:starts_on,
|
|
|
|
:ends_on,
|
|
|
|
:project_id,
|
|
|
|
:budget,
|
|
|
|
:external_project_id,
|
|
|
|
:name,
|
2023-11-24 15:05:38 +00:00
|
|
|
:description,
|
|
|
|
time_budgets_attributes: [:id, :activity_id, :hours, :job_id, :_destroy]
|
2023-11-16 20:23:09 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_project
|
|
|
|
@project = Project.find(params[:project_id])
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_job
|
|
|
|
@job = Job.find(params[:id])
|
|
|
|
end
|
2023-11-24 15:05:38 +00:00
|
|
|
|
|
|
|
# If a time budget is set to 0, remove it
|
|
|
|
def remove_empty_time_budgets(params)
|
|
|
|
params[:time_budgets_attributes].each do |key, value|
|
|
|
|
params[:time_budgets_attributes][key]["_destroy"] = true if params[:time_budgets_attributes][key]["hours"] == "0"
|
|
|
|
end
|
|
|
|
params
|
|
|
|
end
|
2023-11-16 20:23:09 +00:00
|
|
|
end
|