From c36230b8ba13a4fa37c62cb9ef3a4a3d5b2d473f Mon Sep 17 00:00:00 2001 From: david Date: Sat, 23 Nov 2024 21:11:01 +0100 Subject: [PATCH] edit comment and pdf export --- .../success_criteria_controller.rb | 6 +++- app/models/element.rb | 2 ++ app/models/pdf_documents/base.rb | 31 ++++++++++++---- app/models/pdf_documents/customer_report.rb | 35 +++++++++++++++---- app/models/report.rb | 13 +++++++ app/models/success_criterion.rb | 2 ++ app/views/reports/show.html.slim | 6 ++-- app/views/success_criteria/_body.html.slim | 5 ++- .../success_criteria/edit_comment.html.slim | 5 +++ config/routes.rb | 4 +++ 10 files changed, 90 insertions(+), 19 deletions(-) create mode 100644 app/views/success_criteria/edit_comment.html.slim diff --git a/app/controllers/success_criteria_controller.rb b/app/controllers/success_criteria_controller.rb index f77a931..a180f99 100644 --- a/app/controllers/success_criteria_controller.rb +++ b/app/controllers/success_criteria_controller.rb @@ -2,7 +2,7 @@ class SuccessCriteriaController < ApplicationController before_action :set_element, only: %i[new create index new_from_checklist create_from_checklist] - before_action :set_success_criterion, only: %i[show edit update destroy] + before_action :set_success_criterion, only: %i[show edit update destroy edit_comment] # GET /success_criteria def index @@ -108,6 +108,10 @@ class SuccessCriteriaController < ApplicationController end end + def edit_comment + render_modal() + end + private # Use callbacks to share common setup or constraints between actions. diff --git a/app/models/element.rb b/app/models/element.rb index e0b7ddf..84af4be 100644 --- a/app/models/element.rb +++ b/app/models/element.rb @@ -15,6 +15,8 @@ class Element < ApplicationRecord attachable.variant :thumbnail, resize_to_limit: [ 200, 200 ] end + scope :failed, -> { where(SuccessCriterion.where(result: SuccessCriterion.results[:failed]).arel.exists) } + # Calculate actual conformity level: # - if a success_criterion has result :failed -> the confirmity_level # of that success_criterion is not reached. diff --git a/app/models/pdf_documents/base.rb b/app/models/pdf_documents/base.rb index c21d790..c34aa80 100644 --- a/app/models/pdf_documents/base.rb +++ b/app/models/pdf_documents/base.rb @@ -2,7 +2,7 @@ module PdfDocuments class Base - attr_reader :params + attr_reader :params, :font def initialize(prawn_document, **params) @prawn_document = prawn_document @@ -13,7 +13,8 @@ module PdfDocuments # exta_bold: 'vendor/assets/fonts/Lexend-ExtraBold.ttf', # italic: 'vendor/assets/fonts/Lexend-Regular.ttf' # }) - @prawn_document.font "Helvetica", size: 12 + @font = "Helvetica" + @prawn_document.font @font, size: 12 @params = OpenStruct.new(params) end @@ -40,6 +41,10 @@ module PdfDocuments @prawn_document.markup "

#{text}

" end + def heading4(text) + @prawn_document.markup "

#{text}

" + end + def text(text) @prawn_document.text text, markup_options[:text] end @@ -56,10 +61,10 @@ module PdfDocuments { text: { size: 12, margin_bottom: 5 }, heading1: { style: :bold, size: 26, margin_bottom: 10, margin_top: 0 }, - heading2: { style: :bold, size: 17, margin_bottom: 10, margin_top: 5 }, - heading3: { style: :bold, size: 13, margin_bottom: 10, margin_top: 5 }, - heading4: { style: :bold, size: 12, margin_bottom: 10, margin_top: 5 }, - heading5: { style: :bold, size: 12, margin_bottom: 10, margin_top: 5 }, + heading2: { style: :bold, size: 17, margin_bottom: 10, margin_top: 15 }, + heading3: { style: :bold, size: 13, margin_bottom: 10, margin_top: 15 }, + heading4: { style: :bold, size: 12, margin_bottom: 10, margin_top: 10 }, + heading5: { style: :bold, size: 12, margin_bottom: 10, margin_top: 10 }, heading6: { style: :thin, size: 12, margin_bottom: 10, margin_top: 5 } } end @@ -70,7 +75,7 @@ module PdfDocuments end def prepare_rich_text(rich_text) - { h1: "h4" }.each do |tag, replacement| + { h1: "h5" }.each do |tag, replacement| rich_text = rich_text.to_s.gsub("<#{tag}", "<#{replacement}") rich_text = rich_text.to_s.gsub("", "") end @@ -89,5 +94,17 @@ module PdfDocuments def move_down(...) @prawn_document.move_down(...) end + + def safe_display(value, &block) + return if value.blank? + + yield + end + + def bold(text) + @prawn_document.font(@font, style: :bold) do + @prawn_document.text text + end + end end end diff --git a/app/models/pdf_documents/customer_report.rb b/app/models/pdf_documents/customer_report.rb index 463be57..694642a 100644 --- a/app/models/pdf_documents/customer_report.rb +++ b/app/models/pdf_documents/customer_report.rb @@ -13,18 +13,39 @@ module PdfDocuments heading1 params.report.name rich_text params.report.comment - params.report.elements.each.with_index(1) do |element, element_index| + params.report.export[:elements].each.with_index(1) do |(element, success_criteria), element_index| heading2 "#{element_index} #{element.title}" - formatted_text [ { text: element.path, styles: %i[bold italic underline] } ] move_down(5) - rich_text element.description_html + bold("Pfad: #{element.page.path}") + move_down(5) + rich_text element.description - element.success_criteria.each.with_index(1) do |success_criterion, sc_index| - heading3 "#{element_index}.#{sc_index} #{success_criterion.title}" - rich_text success_criterion.description_html - rich_text success_criterion.comment + success_criteria.each.with_index(1) do |success_criterion, sc_index| + success_criterion_row(success_criterion, [element_index, sc_index]) end end end + + private + + def success_criterion_row(success_criterion, index) + heading3 "#{index.join(".")} #{success_criterion.title}" + safe_display(success_criterion.test_comment) do + heading4 "Kommentar" + rich_text success_criterion.test_comment + end + safe_display(success_criterion.quick_criterion) do + heading4 "Kriterium" + rich_text success_criterion.quick_criterion + end + safe_display(success_criterion.quick_fail) do + heading4 "Fail" + rich_text success_criterion.quick_fail + end + safe_display(success_criterion.quick_fix) do + heading4 "Fix" + rich_text success_criterion.quick_fix + end + end end end diff --git a/app/models/report.rb b/app/models/report.rb index 7b15df9..2a03b91 100644 --- a/app/models/report.rb +++ b/app/models/report.rb @@ -3,7 +3,20 @@ class Report < ApplicationRecord has_many :pages, -> { order(:position) }, dependent: :destroy has_many :elements, through: :pages, dependent: :destroy + has_many :success_criteria, through: :elements, dependent: :destroy + has_rich_text :comment validates :name, presence: true + + def export + export_success_criteria = success_criteria.failed + export_elements = export_success_criteria.group_by(&:element) + export_pages = export_elements.group_by { |k, v| k } + { + pages: export_pages, + elements: export_elements, + success_criteria: export_success_criteria + } + end end diff --git a/app/models/success_criterion.rb b/app/models/success_criterion.rb index e575308..afe275a 100644 --- a/app/models/success_criterion.rb +++ b/app/models/success_criterion.rb @@ -19,6 +19,8 @@ class SuccessCriterion < ApplicationRecord validates :result, inclusion: { in: self.results.keys + [ nil ] } + scope :failed, -> { where(result: :failed) } + def level_value return nil unless level diff --git a/app/views/reports/show.html.slim b/app/views/reports/show.html.slim index 3fc7196..9533137 100644 --- a/app/views/reports/show.html.slim +++ b/app/views/reports/show.html.slim @@ -43,9 +43,9 @@ div = link_to(report_export_path(@report), class: "btn btn-secondary", target: :_blank) do i.bi.bi-filetype-html | Online HTML - / = link_to report_path(@report, format: :pdf), class: "btn btn-secondary", target: "_blank" do - / i.bi.bi-filetype-pdf - / | PDF + = link_to report_path(@report, format: :pdf), class: "btn btn-secondary", target: "_blank" do + i.bi.bi-filetype-pdf + | PDF / = link_to report_path(@report, format: :docx), class: "btn btn-secondary", target: "_blank" do / i.bi.bi-filetype-docx / | DOCX diff --git a/app/views/success_criteria/_body.html.slim b/app/views/success_criteria/_body.html.slim index e0c4190..7da29c1 100644 --- a/app/views/success_criteria/_body.html.slim +++ b/app/views/success_criteria/_body.html.slim @@ -11,7 +11,10 @@ label.btn.btn-outline-secondary for=dom_id(success_criterion, :result_not_applicable) Nicht anwendbar /= form.radio_button_without_bootstrap :result, nil, class: "btn-check", autocomplete: "off", id: dom_id(success_criterion, :result_not_applicable) /label.btn.btn-outline-secondary for=dom_id(success_criterion, :nil) Reset - / = dropdown_menu([{ text: "Bearbeiten", icon: "pencil", href: edit_success_criterion_path(success_criterion) }, { text: "Löschen", icon: "trash", href: success_criterion, color: :danger, method: :delete, confirm: "Bist du sicher?"}], klass: "mt-3 ms-auto") + - unless success_criterion.test_comment.blank? + = link_to(edit_comment_success_criterion_path(success_criterion), class: "btn btn-outline-warning my-3 ms-3", data: { turbo_frame: "modal" }) do + i.bi.bi-chat> + 'Kommentar bearbeiten = success_criterion_menu(success_criterion) .row .col diff --git a/app/views/success_criteria/edit_comment.html.slim b/app/views/success_criteria/edit_comment.html.slim new file mode 100644 index 0000000..391a34f --- /dev/null +++ b/app/views/success_criteria/edit_comment.html.slim @@ -0,0 +1,5 @@ += bootstrap_form_with(model: @success_criterion.persisted? ? @success_criterion : [:element, @success_criterion], data: { controller: "unsaved-changes" }) do |form| + = form.rich_text_area :test_comment + = form.submit class: "btn btn-primary" +- unless modal? + =< link_to "Abbrechen", @success_criterion.persisted? ? @success_criterion : @success_criterion.element, class: "btn btn-outline-secondary" diff --git a/config/routes.rb b/config/routes.rb index 9dcb17e..fd5e5e1 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -19,6 +19,10 @@ Rails.application.routes.draw do get "from_checklist", action: :new_from_checklist, as: :new_from_checklist post "from_checklist", action: :create_from_checklist, as: :create_from_checklist end + + member do + get "edit_comment" + end end end end