diff --git a/app/controllers/jobs_controller.rb b/app/controllers/jobs_controller.rb index 2906473..98ba3f3 100644 --- a/app/controllers/jobs_controller.rb +++ b/app/controllers/jobs_controller.rb @@ -1,5 +1,5 @@ class JobsController < ApplicationController - before_action :set_job, only: %i[show edit update destroy] + before_action :set_job, only: %i[show edit update destroy cancel] # GET /jobs or /jobs.json def index @@ -36,7 +36,7 @@ class JobsController < ApplicationController def update respond_to do |format| if @job.update(job_params) - format.html { redirect_to job_url(@job), notice: 'Job was successfully updated.' } + format.html { redirect_to jobs_url, notice: 'Job was successfully updated.' } format.json { render :show, status: :ok, location: @job } else format.html { render :edit, status: :unprocessable_entity } @@ -55,6 +55,16 @@ class JobsController < ApplicationController end end + def cancel + @job.canceled! if @job.able_to_cancel? + + respond_to do |format| + format.turbo_stream {} # view is updated from model broadcast + format.html { redirect_to jobs_url, notice: 'Job was successfully canceled.' } + format.json { head :no_content } + end + end + private # Use callbacks to share common setup or constraints between actions. diff --git a/app/models/job.rb b/app/models/job.rb index 618db7c..eba873a 100644 --- a/app/models/job.rb +++ b/app/models/job.rb @@ -62,6 +62,16 @@ class Job < ApplicationRecord errors.add(:pdf, 'must be a PDF') end + def able_to_cancel? + open? + end + + # cancel job only if it is still open + def canceled! + self.status = :canceled if open? + save + end + private def update_printed_at diff --git a/app/views/jobs/_job_tr.html.erb b/app/views/jobs/_job_tr.html.erb index 1d2cfa2..0968bb4 100644 --- a/app/views/jobs/_job_tr.html.erb +++ b/app/views/jobs/_job_tr.html.erb @@ -1,7 +1,7 @@ -
| ID | Auftraggeber | A0 | @@ -20,6 +20,7 @@A2 | A3 | Status | +Actions |
|---|