Skip to content

Commit

Permalink
Refactor ContentBlockEdition create to accepted nested document attri…
Browse files Browse the repository at this point in the history
…butes

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]
  • Loading branch information
tahb committed Jul 16, 2024
1 parent 7cbd2b9 commit ab7fa4d
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
class ContentObjectStore::ContentBlockEdition < ApplicationRecord
include ContentObjectStore::Identifiable

accepts_nested_attributes_for :content_block_document
end
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
} %>
Expand Down

0 comments on commit ab7fa4d

Please sign in to comment.