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 @@ - - - - + + + <%= job.id %> + <%= job.fullname %> <% if job.pdf.attached? %> @@ -15,4 +15,10 @@ <%= job.status %> + + <%= link_to "edit", edit_job_url(job), class: "p-1.5 ml-2 rounded-lg bg-gray-100 font-medium" %> + <% if job.able_to_cancel? %> + <%= button_to "cancel", cancel_job_url(job), method: :patch, class: "p-1.5 ml-2 rounded-lg bg-gray-100 font-medium" %> + <% end %> + diff --git a/app/views/jobs/index.html.erb b/app/views/jobs/index.html.erb index dec5b19..b05e5c5 100644 --- a/app/views/jobs/index.html.erb +++ b/app/views/jobs/index.html.erb @@ -12,7 +12,7 @@ - + @@ -20,6 +20,7 @@ + diff --git a/config/routes.rb b/config/routes.rb index 7abe017..660af8b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,11 +1,15 @@ Rails.application.routes.draw do - resources :jobs + resources :jobs do + member do + patch 'cancel' + end + end # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html # Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500. # Can be used by load balancers and uptime monitors to verify that the app is live. - get "up" => "rails/health#show", as: :rails_health_check + get 'up' => 'rails/health#show', as: :rails_health_check # Defines the root path route ("/") - root "jobs#index" + root 'jobs#index' end
ID Auftraggeber PDF A0 A2 A3 Status Actions