Skip to content

Commit

Permalink
Allow compound blocks without child blocks
Browse files Browse the repository at this point in the history
This is useful in some cases, for example with hero blocks where we
might only want the image and no content overlaid.

Probably not so useful for featured blocks, but as we're not using those
right now, I think it's fine for them not to have this validation.
  • Loading branch information
richardTowers committed Dec 4, 2024
1 parent bd116fa commit 9ba887f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
7 changes: 5 additions & 2 deletions app/models/landing_page/compound_block.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class LandingPage::CompoundBlock < LandingPage::BaseBlock

attr_reader :content_blocks, :content_block_key

validates :content_blocks, presence: true
validate do
content_blocks.each { |b| errors.merge!(b.errors) if b.invalid? }
end
Expand All @@ -30,6 +29,10 @@ def initialize(source, images, content_block_key, content_blocks)
end

def present_for_publishing_api
super.merge({ content_block_key => { "blocks" => content_blocks.map(&:present_for_publishing_api) } })
if content_blocks.blank?
super
else
super.merge({ content_block_key => { "blocks" => content_blocks.map(&:present_for_publishing_api) } })
end
end
end
15 changes: 12 additions & 3 deletions test/unit/app/models/landing_page/compound_block_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,24 @@ class CompoundBlockTest < ActiveSupport::TestCase
assert_equal(expected_result, subject.present_for_publishing_api)
end

test "invalid when missing content blocks" do
test "valid when missing content blocks" do
subject = LandingPage::CompoundBlock.new(
@valid_block_config,
EMPTY_IMAGES,
"compound_block_content",
nil,
)
assert subject.invalid?
assert_equal ["Content blocks can't be blank"], subject.errors.to_a
assert subject.valid?
end

test "presents without missing content blocks" do
subject = LandingPage::CompoundBlock.new(
@valid_block_config.except("compound_block_content"),
EMPTY_IMAGES,
"compound_block_content",
nil,
)
assert_equal({ "type" => "some-compound-block" }, subject.present_for_publishing_api)
end

test "invalid when content blocks are invalid" do
Expand Down
5 changes: 2 additions & 3 deletions test/unit/app/models/landing_page/featured_block_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,9 @@ class FeaturedBlockTest < ActiveSupport::TestCase
], subject.errors.to_a
end

test "invalid when missing featured content blocks" do
test "valid when missing featured content blocks" do
subject = LandingPage::FeaturedBlock.new(@valid_featured_block_config, @valid_featured_images, nil)
assert subject.invalid?
assert_equal ["Content blocks can't be blank"], subject.errors.to_a
assert subject.valid?
end

test "invalid when featured content blocks are invalid" do
Expand Down
5 changes: 2 additions & 3 deletions test/unit/app/models/landing_page/hero_block_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,9 @@ class HeroBlockTest < ActiveSupport::TestCase
], subject.errors.to_a
end

test "invalid when missing hero content blocks" do
test "valid when missing hero content blocks" do
subject = LandingPage::HeroBlock.new(@valid_hero_block_config, @valid_hero_block_images, nil)
assert subject.invalid?
assert_equal ["Content blocks can't be blank"], subject.errors.to_a
subject.valid?
end

test "invalid when hero content blocks are invalid" do
Expand Down

0 comments on commit 9ba887f

Please sign in to comment.