From 4d79134a7275569764104165b7913e0874f7799e Mon Sep 17 00:00:00 2001 From: Harriet H-W Date: Tue, 7 Jan 2025 16:42:46 +0000 Subject: [PATCH] refactor Content Block Document validation because we have these lines ``` belongs_to :document, touch: true ... accepts_nested_attributes_for :document ``` the `document` should never be nil when creating an Edition. --- .../content_block/edition/documentable.rb | 11 ++--------- .../app/validators/organisation_validator_test.rb | 2 +- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/lib/engines/content_block_manager/app/models/content_block_manager/content_block/edition/documentable.rb b/lib/engines/content_block_manager/app/models/content_block_manager/content_block/edition/documentable.rb index 3e6fb23860e..c34506714f7 100644 --- a/lib/engines/content_block_manager/app/models/content_block_manager/content_block/edition/documentable.rb +++ b/lib/engines/content_block_manager/app/models/content_block_manager/content_block/edition/documentable.rb @@ -12,19 +12,12 @@ module ContentBlock::Edition::Documentable end def block_type - document&.block_type || @block_type + @block_type ||= document&.block_type end def ensure_presence_of_document - if document.blank? - self.document = ContentBlock::Document.new( - content_id: create_random_id, - block_type: @block_type, - sluggable_string: title, - ) - elsif document.new_record? + if document.new_record? document.content_id = create_random_id if document.content_id.blank? - document.block_type = @block_type if document.block_type.blank? document.sluggable_string = title if document.sluggable_string.blank? end end diff --git a/lib/engines/content_block_manager/test/unit/app/validators/organisation_validator_test.rb b/lib/engines/content_block_manager/test/unit/app/validators/organisation_validator_test.rb index 5c779cddf7b..7877c0b6fad 100644 --- a/lib/engines/content_block_manager/test/unit/app/validators/organisation_validator_test.rb +++ b/lib/engines/content_block_manager/test/unit/app/validators/organisation_validator_test.rb @@ -4,7 +4,7 @@ class ContentBlockManager::OrganisationValidatorTest < ActiveSupport::TestCase extend Minitest::Spec::DSL test "it validates presence of a lead organisation" do - content_block_edition = build(:content_block_edition, organisation: nil) + content_block_edition = build(:content_block_edition, organisation: nil, document: build(:content_block_document, :email_address)) assert_equal false, content_block_edition.valid? assert_equal ["cannot be blank"], content_block_edition.errors.messages[:lead_organisation]