Skip to content

Commit

Permalink
refactor Content Block Document validation
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Harriethw committed Jan 8, 2025
1 parent e052614 commit 4d79134
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 4d79134

Please sign in to comment.