From ab7fa4d3f709d6feb3cf53e0c5bf5c7eb45fc0d7 Mon Sep 17 00:00:00 2001 From: Tom Hipkin Date: Tue, 16 Jul 2024 16:24:02 +0100 Subject: [PATCH] Refactor ContentBlockEdition create to accepted nested document attributes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We see that Whitehall proper are taking this approach[1]. It means we are more aligned but can also have our own create and edit logic that handle params for editions and it’s document changes as a single object. We won’t need to encode the logic to take out the document title and making separate Document update requests. [https://github.com/alphagov/whitehall/blob/7cbd2b9b6d97df9741befcc4700fb573fb2e1960/app/models/edition.rb#L82] --- .../content_block_editions_controller.rb | 2 +- .../app/models/content_object_store/content_block_edition.rb | 2 ++ .../services/content_object_store/create_edition_service.rb | 5 ++++- .../content_block_editions/_form.html.erb | 4 ++-- 4 files changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/engines/content_object_store/app/controllers/content_object_store/content_block_editions_controller.rb b/lib/engines/content_object_store/app/controllers/content_object_store/content_block_editions_controller.rb index 110c3edc3c65..83f1704f7cc1 100644 --- a/lib/engines/content_object_store/app/controllers/content_object_store/content_block_editions_controller.rb +++ b/lib/engines/content_object_store/app/controllers/content_object_store/content_block_editions_controller.rb @@ -34,6 +34,6 @@ def root_params end def edition_params - root_params.permit(:title, :block_type, details: @schema.fields) + root_params.permit(:block_type, content_block_document_attributes: [:title], details: @schema.fields) end end diff --git a/lib/engines/content_object_store/app/models/content_object_store/content_block_edition.rb b/lib/engines/content_object_store/app/models/content_object_store/content_block_edition.rb index 98c8c8fefc6e..4acea0577ce3 100644 --- a/lib/engines/content_object_store/app/models/content_object_store/content_block_edition.rb +++ b/lib/engines/content_object_store/app/models/content_object_store/content_block_edition.rb @@ -1,3 +1,5 @@ class ContentObjectStore::ContentBlockEdition < ApplicationRecord include ContentObjectStore::Identifiable + + accepts_nested_attributes_for :content_block_document end diff --git a/lib/engines/content_object_store/app/services/content_object_store/create_edition_service.rb b/lib/engines/content_object_store/app/services/content_object_store/create_edition_service.rb index 6acf0b115217..d5c8d44c5448 100644 --- a/lib/engines/content_object_store/app/services/content_object_store/create_edition_service.rb +++ b/lib/engines/content_object_store/app/services/content_object_store/create_edition_service.rb @@ -7,7 +7,10 @@ def initialize(schema) end def call(edition_params) - publish_with_rollback(schema: @schema, title: edition_params[:title], details: edition_params[:details]) do + title = edition_params[:content_block_document_attributes][:title] + details = edition_params[:details] + + publish_with_rollback(schema: @schema, title:, details:) do ContentObjectStore::ContentBlockEdition.create!(edition_params) end end diff --git a/lib/engines/content_object_store/app/views/content_object_store/content_block_editions/_form.html.erb b/lib/engines/content_object_store/app/views/content_object_store/content_block_editions/_form.html.erb index 0d723aa391fa..c58463b87362 100644 --- a/lib/engines/content_object_store/app/views/content_object_store/content_block_editions/_form.html.erb +++ b/lib/engines/content_object_store/app/views/content_object_store/content_block_editions/_form.html.erb @@ -7,8 +7,8 @@ label: { text: "Title", }, - name: "content_object_store_content_block_edition[title]", - id: "content_object_store/content_block_edition_title", + name: "content_object_store_content_block_edition[content_block_document_attributes][title]", + id: "content_object_store_content_block_edition_document_attributes_title", value: content_block_edition.title, error_items: errors_for(content_block_edition.errors, :title), } %>