From bfe5193a617595da19375f91f9d61db59186f0c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20B=C3=B6hm?= Date: Mon, 12 Aug 2024 03:01:13 +0200 Subject: [PATCH] Generated operator controller and cleaned job controller --- app/controllers/jobs_controller.rb | 58 ++--------- app/controllers/operator/jobs_controller.rb | 95 +++++++++++++++++++ app/helpers/operator/jobs_helper.rb | 2 + app/views/jobs/_form.html.erb | 24 ----- app/views/operator/jobs/_form.html.erb | 55 +++++++++++ app/views/{ => operator}/jobs/_job.html.erb | 0 .../{ => operator}/jobs/_job.json.jbuilder | 0 app/views/operator/jobs/_job_tr.html.erb | 66 +++++++++++++ .../operator/jobs/cancel.turbo_stream.erb | 1 + app/views/{ => operator}/jobs/edit.html.erb | 0 app/views/operator/jobs/index.html.erb | 32 +++++++ app/views/{ => operator}/jobs/show.html.erb | 0 .../{ => operator}/jobs/show.json.jbuilder | 0 config/routes.rb | 3 + .../operator/jobs_controller_test.rb | 7 ++ 15 files changed, 270 insertions(+), 73 deletions(-) create mode 100644 app/controllers/operator/jobs_controller.rb create mode 100644 app/helpers/operator/jobs_helper.rb create mode 100644 app/views/operator/jobs/_form.html.erb rename app/views/{ => operator}/jobs/_job.html.erb (100%) rename app/views/{ => operator}/jobs/_job.json.jbuilder (100%) create mode 100644 app/views/operator/jobs/_job_tr.html.erb create mode 100644 app/views/operator/jobs/cancel.turbo_stream.erb rename app/views/{ => operator}/jobs/edit.html.erb (100%) create mode 100644 app/views/operator/jobs/index.html.erb rename app/views/{ => operator}/jobs/show.html.erb (100%) rename app/views/{ => operator}/jobs/show.json.jbuilder (100%) create mode 100644 test/controllers/operator/jobs_controller_test.rb diff --git a/app/controllers/jobs_controller.rb b/app/controllers/jobs_controller.rb index fcbfe46..490a641 100644 --- a/app/controllers/jobs_controller.rb +++ b/app/controllers/jobs_controller.rb @@ -1,30 +1,22 @@ class JobsController < ApplicationController - before_action :set_job, only: %i[show edit update destroy cancel] - # GET /jobs or /jobs.json def index @jobs = Job.currently_working_on end - # GET /jobs/1 or /jobs/1.json - def show; end - - # GET /jobs/new + # GET /jobs/new def new @job = Job.new end - # GET /jobs/1/edit - def edit; end - # POST /jobs or /jobs.json def create @job = Job.new(job_params) respond_to do |format| if @job.save - flash[:notice] = 'Job was successfully created.' - Turbo::StreamsChannel.broadcast_prepend_later_to 'jobs', target: :jobs, partial: 'jobs/job_tr', + flash[:notice] = "Job was successfully created." + Turbo::StreamsChannel.broadcast_prepend_later_to "jobs", target: :jobs, partial: "jobs/job_tr", locals: { job: @job } format.html { redirect_to jobs_url } format.json { render :show, status: :created, location: @job } @@ -35,38 +27,14 @@ class JobsController < ApplicationController end end - # PATCH/PUT /jobs/1 or /jobs/1.json - def update - respond_to do |format| - if @job.update(job_params) - broadcast_update_job - 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 } - format.json { render json: @job.errors, status: :unprocessable_entity } - end - end - end - - # DELETE /jobs/1 or /jobs/1.json - def destroy - @job.destroy! - - respond_to do |format| - Turbo::StreamsChannel.broadcast_remove_to 'jobs', target: @job - format.html { redirect_to jobs_url, notice: 'Job was successfully destroyed.' } - format.json { head :no_content } - end - end - def cancel + @job = Job.find(params[:id]) if @job.canceled! - flash[:notice] = 'Job successfully canceled' + flash[:notice] = "Job successfully canceled" else - flash[:alert] = 'Job could not be canceled' + flash[:alert] = "Job could not be canceled" end - + respond_to do |format| broadcast_update_job format.turbo_stream @@ -77,20 +45,12 @@ class JobsController < ApplicationController private - # Use callbacks to share common setup or constraints between actions. - def set_job - @job = Job.find(params[:id]) - end - def broadcast_update_job - Turbo::StreamsChannel.broadcast_replace_later_to 'jobs', target: @job, partial: 'jobs/job_tr', locals: { job: @job } + Turbo::StreamsChannel.broadcast_replace_later_to "jobs", target: @job, partial: "jobs/job_tr", locals: { job: @job } end # Only allow a list of trusted parameters through. def job_params - params.require(:job).permit(:operator_id, :costumer_id, :operator_firstname, :operator_lastname, - :costumer_firstname, :costumer_lastname, :paid, :printed_at, :intern, - :cost_center, :number_of_plans_a0, :number_of_plans_a1, - :number_of_plans_a2, :number_of_plans_a3, :costum_qm_plan, :privacy_policy_accepted, :pdf) + params.require(:job).permit(:costumer_id, :costumer_firstname, :costumer_lastname, :privacy_policy_accepted, :pdf) end end diff --git a/app/controllers/operator/jobs_controller.rb b/app/controllers/operator/jobs_controller.rb new file mode 100644 index 0000000..daae288 --- /dev/null +++ b/app/controllers/operator/jobs_controller.rb @@ -0,0 +1,95 @@ +class Operator::JobsController < ApplicationController + before_action :set_job, only: %i[show edit update destroy cancel] + + # GET /jobs or /jobs.json + def index + @jobs = Job.currently_working_on + end + + + # GET /jobs/1 or /jobs/1.json + def show; end + + # GET /jobs/new + def new + @job = Job.new + end + + # GET /jobs/1/edit + def edit; end + + # POST /jobs or /jobs.json + def create + @job = Job.new(job_params) + + respond_to do |format| + if @job.save + flash[:notice] = "Job was successfully created." + Turbo::StreamsChannel.broadcast_prepend_later_to "jobs", target: :jobs, partial: "jobs/job_tr", + locals: { job: @job } + format.html { redirect_to jobs_url } + format.json { render :show, status: :created, location: @job } + else + format.html { render :new, status: :unprocessable_entity } + format.json { render json: @job.errors, status: :unprocessable_entity } + end + end + end + + # PATCH/PUT /jobs/1 or /jobs/1.json + def update + respond_to do |format| + if @job.update(job_params) + broadcast_update_job + 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 } + format.json { render json: @job.errors, status: :unprocessable_entity } + end + end + end + + def cancel + if @job.canceled! + flash[:notice] = "Job successfully canceled" + else + flash[:alert] = "Job could not be canceled" + end + + respond_to do |format| + broadcast_update_job + format.turbo_stream + format.html { redirect_to jobs_url } + format.json { head :no_content } + end + end + + # DELETE /jobs/1 or /jobs/1.json + def destroy + @job.destroy! + + respond_to do |format| + Turbo::StreamsChannel.broadcast_remove_to "jobs", target: @job + format.html { redirect_to jobs_url, notice: "Job was successfully destroyed." } + format.json { head :no_content } + end + end + + private + + # Use callbacks to share common setup or constraints between actions. + def set_job + @job = Job.find(params[:id]) + end + + def broadcast_update_job + Turbo::StreamsChannel.broadcast_replace_later_to "jobs", target: @job, partial: "jobs/job_tr", locals: { job: @job } + end + + # Only allow a list of trusted parameters through. + def job_params + params.require(:job).permit(:operator_id, :costumer_id, :opertator_firstname, :operator_lastname, :costumer_firstname, :costumer_lastname) + end +end + diff --git a/app/helpers/operator/jobs_helper.rb b/app/helpers/operator/jobs_helper.rb new file mode 100644 index 0000000..4c274c1 --- /dev/null +++ b/app/helpers/operator/jobs_helper.rb @@ -0,0 +1,2 @@ +module Operator::JobsHelper +end diff --git a/app/views/jobs/_form.html.erb b/app/views/jobs/_form.html.erb index 1f1b26a..39fdbd8 100644 --- a/app/views/jobs/_form.html.erb +++ b/app/views/jobs/_form.html.erb @@ -18,30 +18,6 @@ <%= form.label :costumer_lastname, 'Nachname' %> <%= form.text_field :costumer_lastname, class: "block shadow-lg rounded-md border border-hsrm-gray outline-none px-3 py-2 mt-2 w-full" %> - - - - - - - - - - - - - - - - - - - - - - - -
<%= form.label :pdf, "Plan auswählen (PDF-Format)" %> <%= form.file_field :pdf, accept: "application/pdf", class: "block shadow-lg rounded-md border border-hsrm-gray outline-none px-3 py-2 mt-2 w-full" %> diff --git a/app/views/operator/jobs/_form.html.erb b/app/views/operator/jobs/_form.html.erb new file mode 100644 index 0000000..1f1b26a --- /dev/null +++ b/app/views/operator/jobs/_form.html.erb @@ -0,0 +1,55 @@ +<%= form_with(model: job, class: "contents") do |form| %> + <% if job.errors.any? %> +
+

<%= pluralize(job.errors.count, "error") %> + prohibited this job from being saved:

+
    + <% job.errors.each do |error| %> +
  • <%= error.full_message %>
  • + <% end %> +
+
+ <% end %> +
+ <%= form.label :costumer_firstname, 'Vorname' %> + <%= form.text_field :costumer_firstname, class: "block shadow-lg rounded-md border border-hsrm-gray outline-none px-3 py-2 mt-2 w-full" %> +
+
+ <%= form.label :costumer_lastname, 'Nachname' %> + <%= form.text_field :costumer_lastname, class: "block shadow-lg rounded-md border border-hsrm-gray outline-none px-3 py-2 mt-2 w-full" %> +
+ + + + + + + + + + + + + + + + + + + + + + + + +
+ <%= form.label :pdf, "Plan auswählen (PDF-Format)" %> + <%= form.file_field :pdf, accept: "application/pdf", class: "block shadow-lg rounded-md border border-hsrm-gray outline-none px-3 py-2 mt-2 w-full" %> +
+
+ <%= form.check_box :privacy_policy_accepted, class: "mt-2 h-5 w-5" %> <%= form.label :privacy_policy_accepted, "Datenschutzerklärung akzeptiert", class: "p-2" %> +
+
+ <%= form.submit "Plottauftrag abschicken", class: "py-2 px-3 bg-hsrm-red hover:bg-hsrm-red-light shadow-lg text-white inline-block font-medium cursor-pointer" %> +
+<% end %> diff --git a/app/views/jobs/_job.html.erb b/app/views/operator/jobs/_job.html.erb similarity index 100% rename from app/views/jobs/_job.html.erb rename to app/views/operator/jobs/_job.html.erb diff --git a/app/views/jobs/_job.json.jbuilder b/app/views/operator/jobs/_job.json.jbuilder similarity index 100% rename from app/views/jobs/_job.json.jbuilder rename to app/views/operator/jobs/_job.json.jbuilder diff --git a/app/views/operator/jobs/_job_tr.html.erb b/app/views/operator/jobs/_job_tr.html.erb new file mode 100644 index 0000000..9554337 --- /dev/null +++ b/app/views/operator/jobs/_job_tr.html.erb @@ -0,0 +1,66 @@ + + + + <%= job.id %> + + + + <%= job.costumer_fullname %> + + + <% if job.pdf.attached? %> + <%= image_tag url_for(job.pdf.preview(resize_to_limit: [100, 100])) %> + <% end %> + + + <% if job.pdf.attached? %> + <%#= link_to job.pdf.filename, rails_blob_path(job.pdf, disposition: "attachment") %> + <%= link_to job.pdf.filename, job.pdf, download:true %> + + <%=number_to_human_size job.pdf.blob.byte_size%> + + <% end %> + + + + <%= job.number_of_plans_a0 %> + + + + + <%= job.number_of_plans_a1 %> + + + + + <%= job.number_of_plans_a2 %> + + + + + <%= job.number_of_plans_a3 %> + + + + + <%= job.costum_qm_plan.round(2) %> m² + + + + + <%= job.cost.round(2) %> € + + + + + <%= job.status %> + + + + <% if job.able_to_cancel? %> + <%= button_to icon("x-circle", class: "text-hsrm-red size-8 inline", title: "Abbrechen"), cancel_job_path(job), method: :patch, form: {data: {turbo_confirm: 'Den Plottauftrag wirklich abbrechen?'}}, form_class: "inline" %> + <% else %> + <%= icon("x-circle", class: "text-hsrm-gray text-opacity-50 size-8 inline", title: "Kann nicht mehr abgebrochen werden") %> + <% end %> + + diff --git a/app/views/operator/jobs/cancel.turbo_stream.erb b/app/views/operator/jobs/cancel.turbo_stream.erb new file mode 100644 index 0000000..406a77a --- /dev/null +++ b/app/views/operator/jobs/cancel.turbo_stream.erb @@ -0,0 +1 @@ +<%= turbo_stream.update "flash", partial: "layouts/flash" %> diff --git a/app/views/jobs/edit.html.erb b/app/views/operator/jobs/edit.html.erb similarity index 100% rename from app/views/jobs/edit.html.erb rename to app/views/operator/jobs/edit.html.erb diff --git a/app/views/operator/jobs/index.html.erb b/app/views/operator/jobs/index.html.erb new file mode 100644 index 0000000..62d564f --- /dev/null +++ b/app/views/operator/jobs/index.html.erb @@ -0,0 +1,32 @@ +<%= turbo_stream_from 'jobs' %> +
+ <%#= render partial: 'layouts/flash' %> + <% content_for :title, "Current Print Jobs" %> +
+

Aktuelle Plottaufträge <%= Date.today.strftime("%d.%m.%Y") %>

+ <%= link_to "Plottauftrag aufgeben", new_job_path, class: "px-3 py-2 bg-hsrm-red drop-shadow-lg transition-colors hover:bg-hsrm-red-light text-white block font-medium" %> +
+
+ + + + + + + + + + + + + + + + + + <%= render partial: "job_tr", collection: @jobs, as: :job %> + <%#= link_to "Show this job", job, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %> + +
ID Auftraggeber PDF A0 A1 A2 A3 noDIN Kosten Status
+
+
diff --git a/app/views/jobs/show.html.erb b/app/views/operator/jobs/show.html.erb similarity index 100% rename from app/views/jobs/show.html.erb rename to app/views/operator/jobs/show.html.erb diff --git a/app/views/jobs/show.json.jbuilder b/app/views/operator/jobs/show.json.jbuilder similarity index 100% rename from app/views/jobs/show.json.jbuilder rename to app/views/operator/jobs/show.json.jbuilder diff --git a/config/routes.rb b/config/routes.rb index 88f4666..54956ab 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,6 +4,9 @@ Rails.application.routes.draw do patch "cancel" end end + namespace :operator do + resources :jobs + 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. diff --git a/test/controllers/operator/jobs_controller_test.rb b/test/controllers/operator/jobs_controller_test.rb new file mode 100644 index 0000000..62d3a18 --- /dev/null +++ b/test/controllers/operator/jobs_controller_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class Operator::JobsControllerTest < ActionDispatch::IntegrationTest + # test "the truth" do + # assert true + # end +end