From 963e9301804ab9baaf73b7d6a0d226869582ccd0 Mon Sep 17 00:00:00 2001 From: Chonlasith Jucksriporn Date: Wed, 23 Oct 2024 14:17:53 +0700 Subject: [PATCH] feat: Rename form --- cdmm/app/controllers/cdmm_controller.rb | 30 +++++++++++-------- .../controllers/editable_label_controller.js | 30 +++++++++++++++++++ cdmm/app/views/cdmm/_cdmm_form.html.erb | 4 +-- cdmm/app/views/cdmm/_form_title.html.erb | 3 ++ .../app/views/shared/_editable_title.html.erb | 24 +++++++++++++++ .../migrate/20241022142702_add_form_title.rb | 5 ++++ cdmm/db/schema.rb | 3 +- 7 files changed, 84 insertions(+), 15 deletions(-) create mode 100644 cdmm/app/javascript/controllers/editable_label_controller.js create mode 100644 cdmm/app/views/cdmm/_form_title.html.erb create mode 100644 cdmm/app/views/shared/_editable_title.html.erb create mode 100644 cdmm/db/migrate/20241022142702_add_form_title.rb diff --git a/cdmm/app/controllers/cdmm_controller.rb b/cdmm/app/controllers/cdmm_controller.rb index 2ae2236..0011fde 100644 --- a/cdmm/app/controllers/cdmm_controller.rb +++ b/cdmm/app/controllers/cdmm_controller.rb @@ -14,8 +14,10 @@ class CdmmController < ApplicationController def index form_key = generate_unique_form_key + default_table_title = Date.today.strftime("%B %d, %Y") + form_title = "Untitled - #{default_table_title}" # Auto create a draft - ev = Evaluation.new(evaluation_params.merge(form_key: form_key)) + ev = Evaluation.new(evaluation_params.merge(form_key: form_key, title: form_title)) ev.form_status = :draft if ev.save redirect_to evaluation_show_path(ev.form_key), notice: 'Draft evaluation was successfully created.' @@ -38,12 +40,11 @@ def show() def save() form_key = params[:form_key] unless params[:form_key] or params[:form_key].present? - form_key = generate_unique_form_key - ev = Evaluation.new(evaluation_params.merge(form_key: form_key)) + render_not_found else ev = Evaluation.find_by(form_key: params[:form_key]) if ev - ev.attributes = evaluation_params # Update attributes for existing record + ev.attributes = evaluation_params.except(:authenticity_token) # Update attributes for existing record else render_not_found # Handle case where form_key doesn't exist return # Prevent further execution @@ -54,11 +55,16 @@ def save() @table = evaluation_table(ev, form_key) respond_to do |format| format.turbo_stream { - render turbo_stream: - turbo_stream - .replace("evaluation_form", - partial: "form_table", - locals: { table: @table }) + render turbo_stream: [ + turbo_stream + .replace("evaluation_form", + partial: "form_table", + locals: { table: @table }), + turbo_stream + .replace("evaluation_form_title", + partial: "form_title", + locals: { text: @table[:title] }), + ] } format.html { redirect_to evaluation_show_path(ev.form_key), notice: 'Evaluation was successfully created.' @@ -77,11 +83,10 @@ def load_form_value(cell, form_data) end def evaluation_table(form_data = nil, form_key = nil) - default_table_title = Date.today.strftime("%B %d, %Y") table = { :form_key => form_key ? form_key : "", :form_status => :draft, - :title => "Untitled - #{default_table_title}", + :title => form_data[:title], :col_headers => [ "Initial", "Managed", "Defined", "Qualitatively Managed", "Optimizing" ], :row_headers => [ "Culture & Organization", "Build & Deploy", "Release", "Data Management", "Test & Verification", "Information & Reporting" ], :rows => [ @@ -664,13 +669,14 @@ def evaluation_form :real_time_graphs_on_deployment_pipeline_metrics, :dynamic_self_service_of_information, :customizable_dashboards, - :cross_reference_across_organizational_boundarie + :cross_reference_across_organizational_boundaries ] end def evaluation_params # Do not put form_status here to prevent status injection. params.permit( + :authenticity_token, :form_key, # Make sure to permit form_key :title, *evaluation_form diff --git a/cdmm/app/javascript/controllers/editable_label_controller.js b/cdmm/app/javascript/controllers/editable_label_controller.js new file mode 100644 index 0000000..aceda0a --- /dev/null +++ b/cdmm/app/javascript/controllers/editable_label_controller.js @@ -0,0 +1,30 @@ +import { Controller } from "@hotwired/stimulus" + +// Connects to data-controller="editable-label" +export default class extends Controller { + static values = { identifier: String, original: String }; + static targets = [ 'textViewer', 'textEditor', 'textEditorInput' ]; + + connect() { + this.openViewer(); + } + + openViewer() { + this.textViewerTarget.classList.remove('hidden'); + this.textEditorTarget.classList.remove('inline-flex'); + this.textEditorTarget.classList.add('hidden'); + } + + openEditor() { + this.textViewerTarget.classList.add('hidden'); + this.textEditorTarget.classList.remove('hidden') + this.textEditorTarget.classList.add('inline-flex'); + this.textEditorInputTarget.focus(); + } + + submit() { + this.textViewerTarget.innerHTML = this.textEditorInputTarget.value; + this.openViewer(); + this.dispatch('changed', { detail: { original: this.originalValue, new: this.textEditorInputTarget.value }, prefix: this.identifierValue }); + } +} diff --git a/cdmm/app/views/cdmm/_cdmm_form.html.erb b/cdmm/app/views/cdmm/_cdmm_form.html.erb index fee8ebd..d820952 100644 --- a/cdmm/app/views/cdmm/_cdmm_form.html.erb +++ b/cdmm/app/views/cdmm/_cdmm_form.html.erb @@ -1,11 +1,11 @@ -<%= form_with url: evaluation_save_path, method: :post, data: { controller: "auto-submit", action: "capability:toggled@window->auto-submit#dirty capability:toggled@window->auto-submit#submit", turbo_stream: true } do |form| %> +<%= form_with url: evaluation_save_path, method: :post, data: { controller: "auto-submit", action: "capability:toggled@window->auto-submit#dirty capability:toggled@window->auto-submit#submit title:changed@window->auto-submit#dirty title:changed@window->auto-submit#submit", turbo_stream: true } do |form| %> <% if table[:form_key] %> <% end %>
-

<%= table[:title] %>

+ <%= render "form_title", text: table[:title] %>
diff --git a/cdmm/app/views/cdmm/_form_title.html.erb b/cdmm/app/views/cdmm/_form_title.html.erb new file mode 100644 index 0000000..adfb407 --- /dev/null +++ b/cdmm/app/views/cdmm/_form_title.html.erb @@ -0,0 +1,3 @@ +<%= turbo_frame_tag "evaluation_form_title", data: { turbo_cache_control: "no-cache" } do %> + <%= render "shared/editable_title", input_name: :title, text: text, identifier: :title %> +<% end %> \ No newline at end of file diff --git a/cdmm/app/views/shared/_editable_title.html.erb b/cdmm/app/views/shared/_editable_title.html.erb new file mode 100644 index 0000000..4609c9c --- /dev/null +++ b/cdmm/app/views/shared/_editable_title.html.erb @@ -0,0 +1,24 @@ +
+
+

<%= text %>

+
+ +
+
+
+ + + +
+
\ No newline at end of file diff --git a/cdmm/db/migrate/20241022142702_add_form_title.rb b/cdmm/db/migrate/20241022142702_add_form_title.rb new file mode 100644 index 0000000..833b1de --- /dev/null +++ b/cdmm/db/migrate/20241022142702_add_form_title.rb @@ -0,0 +1,5 @@ +class AddFormTitle < ActiveRecord::Migration[7.2] + def change + add_column :evaluations, :title, :string + end +end diff --git a/cdmm/db/schema.rb b/cdmm/db/schema.rb index eba9564..e3622e1 100644 --- a/cdmm/db/schema.rb +++ b/cdmm/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.2].define(version: 2024_10_22_091416) do +ActiveRecord::Schema[7.2].define(version: 2024_10_22_142702) do create_table "evaluations", force: :cascade do |t| t.datetime "created_at", null: false t.datetime "updated_at", null: false @@ -86,6 +86,7 @@ t.string "customizable_dashboards", default: "unchecked", null: false t.string "cross_reference_across_organizational_boundaries", default: "unchecked", null: false t.string "form_status" + t.string "title" t.index ["form_key"], name: "index_evaluation_form_key" t.index ["form_status"], name: "index_evaluations_on_form_status" end