Redmine-Jobs/app/controllers/jobs_controller.rb

78 lines
1.6 KiB
Ruby
Raw Normal View History

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
@jobs = Job.project_or_parent(@project)
2023-11-16 20:23:09 +00:00
end
def show
end
def new
2023-11-26 20:25:27 +00:00
@job = Job.new(project_id: @project.id).with_all_time_budgets
2023-11-16 20:23:09 +00:00
end
def edit
@job.with_all_time_budgets
2023-11-16 20:23:09 +00:00
end
def update
if @job.update(remove_empty_time_budgets(job_params))
redirect_to project_job_path(@job.project, @job)
2023-11-16 20:23:09 +00:00
else
render :edit
end
end
def create
@job = Job.new(job_params)
if @job.save
redirect_to project_job_path(@job.project, @job)
2023-11-16 20:23:09 +00:00
else
@project = @job.project
render :new
2023-11-16 20:23:09 +00:00
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 project_jobs_path(@project)
2023-11-16 20:23:09 +00:00
else
render :show
end
end
private
def job_params
params.require(:job).permit(
:starts_on,
:ends_on,
:project_id,
:budget,
:name,
:description,
2023-11-30 19:08:55 +00:00
:category_id,
time_budgets_attributes: [:id, :category_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
# 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