Skip to content

Commit

Permalink
Merge pull request #4338 from alphagov/namespace-block-models
Browse files Browse the repository at this point in the history
Namespace all block models under LandingPage
  • Loading branch information
leenagupte authored Oct 25, 2024
2 parents fd29a61 + 28e6a3b commit 1a660fb
Show file tree
Hide file tree
Showing 31 changed files with 90 additions and 84 deletions.
5 changes: 0 additions & 5 deletions app/models/block/blocks_container.rb

This file was deleted.

5 changes: 0 additions & 5 deletions app/models/block/columns_layout.rb

This file was deleted.

5 changes: 0 additions & 5 deletions app/models/block/grid_container.rb

This file was deleted.

11 changes: 0 additions & 11 deletions app/models/block/layout_base.rb

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Block
module LandingPage::Block
class Base
attr_reader :id, :type, :data

Expand Down
5 changes: 5 additions & 0 deletions app/models/landing_page/block/blocks_container.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module LandingPage::Block
class BlocksContainer < LayoutBase
alias_method :children, :blocks
end
end
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Block
module LandingPage::Block
CardImage = Data.define(:alt, :source)

class Card < Block::Base
class Card < Base
attr_reader :image, :card_content

def initialize(block_hash)
Expand All @@ -12,7 +12,7 @@ def initialize(block_hash)
@image = CardImage.new(alt:, source:)
end

@card_content = BlockFactory.build_all(data.dig("card_content", "blocks"))
@card_content = LandingPage::BlockFactory.build_all(data.dig("card_content", "blocks"))
end
end
end
5 changes: 5 additions & 0 deletions app/models/landing_page/block/columns_layout.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module LandingPage::Block
class ColumnsLayout < LayoutBase
alias_method :columns, :blocks
end
end
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Block
class DocumentList < Block::Base
module LandingPage::Block
class DocumentList < Base
def full_width?
false
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module Block
module LandingPage::Block
FeaturedImageSources = Data.define(:desktop, :desktop_2x, :tablet, :tablet_2x, :mobile, :mobile_2x)
FeaturedImage = Data.define(:alt, :sources)

class Featured < Block::Base
class Featured < Base
attr_reader :image, :featured_content

def initialize(block_hash)
Expand All @@ -11,7 +11,7 @@ def initialize(block_hash)
alt, sources = data.fetch("image").values_at("alt", "sources")
sources = FeaturedImageSources.new(**sources)
@image = FeaturedImage.new(alt:, sources:)
@featured_content = data.dig("featured_content", "blocks")&.map { |subblock_hash| BlockFactory.build(subblock_hash) }
@featured_content = data.dig("featured_content", "blocks")&.map { |subblock_hash| LandingPage::BlockFactory.build(subblock_hash) }
end

def full_width?
Expand Down
5 changes: 5 additions & 0 deletions app/models/landing_page/block/grid_container.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module LandingPage::Block
class GridContainer < LayoutBase
alias_method :children, :blocks
end
end
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module Block
module LandingPage::Block
HeroImageSources = Data.define(:desktop, :desktop_2x, :tablet, :tablet_2x, :mobile, :mobile_2x)
HeroImage = Data.define(:alt, :sources)

class Hero < Block::Base
class Hero < Base
attr_reader :image, :hero_content

def initialize(block_hash)
Expand All @@ -11,7 +11,7 @@ def initialize(block_hash)
alt, sources = data.fetch("image").values_at("alt", "sources")
sources = HeroImageSources.new(**sources)
@image = HeroImage.new(alt:, sources:)
@hero_content = BlockFactory.build_all(data.dig("hero_content", "blocks"))
@hero_content = LandingPage::BlockFactory.build_all(data.dig("hero_content", "blocks"))
end

def full_width?
Expand Down
11 changes: 11 additions & 0 deletions app/models/landing_page/block/layout_base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module LandingPage::Block
class LayoutBase < Base
attr_reader :blocks

def initialize(block_hash)
super(block_hash)

@blocks = LandingPage::BlockFactory.build_all(data["blocks"])
end
end
end
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Block
class MainNavigation < Block::Base
module LandingPage::Block
class MainNavigation < Base
attr_reader :title, :title_link, :links

def initialize(block_hash)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Block
class ShareLinks < Block::Base
module LandingPage::Block
class ShareLinks < Base
attr_reader :links

def initialize(block_hash)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Block
class SideNavigation < Block::Base
module LandingPage::Block
class SideNavigation < Base
LINKS_FILE_PATH = "lib/data/landing_page_content_items/links/side_navigation.yaml".freeze

def links
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "csv"

module Block
class Statistics < Block::Base
module LandingPage::Block
class Statistics < Base
STATISTICS_DATA_PATH = "lib/data/landing_page_content_items/statistics".freeze

def x_axis_keys
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Block
class TwoColumnLayout < Block::LayoutBase
module LandingPage::Block
class TwoColumnLayout < LayoutBase
attr_reader :left, :right, :theme
alias_method :columns, :blocks

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class BlockFactory
class LandingPage::BlockFactory
def self.build_all(block_array)
(block_array || []).map { |block| build(block) }
end
Expand All @@ -8,9 +8,9 @@ def self.build(block_hash)
end

def self.block_class(type)
klass = "Block::#{type.camelize}".constantize
klass.ancestors.include?(Block::Base) ? klass : Block::Base
klass = "LandingPage::Block::#{type.camelize}".constantize
klass.ancestors.include?(LandingPage::Block::Base) ? klass : LandingPage::Block::Base
rescue StandardError
Block::Base
LandingPage::Block::Base
end
end
18 changes: 0 additions & 18 deletions spec/models/block/side_navigation_spec.rb

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
RSpec.describe Block::Base do
RSpec.describe LandingPage::Block::Base do
describe "#full_width?" do
it "is false by default" do
expect(described_class.new({}).full_width?).to eq(false)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
RSpec.describe Block::Card do
RSpec.describe LandingPage::Block::Card do
let(:blocks_hash) do
{ "type" => "card",
"image" => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
RSpec.describe Block::DocumentList do
RSpec.describe LandingPage::Block::DocumentList do
let(:blocks_hash) do
{ "type" => "document_list",
"items" => [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
RSpec.describe Block::Featured do
RSpec.describe LandingPage::Block::Featured do
let(:blocks_hash) do
{ "type" => "featured",
"image" => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
RSpec.describe Block::Hero do
RSpec.describe LandingPage::Block::Hero do
let(:blocks_hash) do
{ "type" => "hero",
"image" => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
RSpec.describe Block::LayoutBase do
RSpec.describe LandingPage::Block::LayoutBase do
describe "#blocks" do
let(:blocks_hash) do
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
RSpec.describe Block::MainNavigation do
RSpec.describe LandingPage::Block::MainNavigation do
let(:blocks_hash) do
{ "type" => "main_navigation",
"title" => "Service Name",
Expand Down
24 changes: 24 additions & 0 deletions spec/models/landing_page/block/side_navigation_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
RSpec.describe LandingPage::Block::SideNavigation do
let(:block_hash) do
{ "type" => "side_navigation" }
end

before do
LandingPage::Block::SideNavigation.send(:remove_const, "LINKS_FILE_PATH")
LandingPage::Block::SideNavigation.const_set("LINKS_FILE_PATH", "spec/fixtures/landing_page/links/side_navigation.yaml")
end

after do
LandingPage::Block::SideNavigation.send(:remove_const, "LINKS_FILE_PATH")
LandingPage::Block::SideNavigation.const_set("LINKS_FILE_PATH", "lib/data/landing_page_content_items/links/side_navigation.yaml")
end

describe "#links" do
it "returns all of the side navigation links" do
links = described_class.new(block_hash).links
expect(links.count).to eq(2)
expect(links.first["text"]).to eq("Landing Page")
expect(links.first["href"]).to eq("/landing-page")
end
end
end
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
RSpec.describe Block::Statistics do
RSpec.describe LandingPage::Block::Statistics do
let(:blocks_hash) do
{
"type" => "statistics",
Expand All @@ -11,13 +11,13 @@
end

before do
Block::Statistics.send(:remove_const, "STATISTICS_DATA_PATH")
Block::Statistics.const_set("STATISTICS_DATA_PATH", "spec/fixtures/landing_page_statistics_data")
LandingPage::Block::Statistics.send(:remove_const, "STATISTICS_DATA_PATH")
LandingPage::Block::Statistics.const_set("STATISTICS_DATA_PATH", "spec/fixtures/landing_page_statistics_data")
end

after do
Block::Statistics.send(:remove_const, "STATISTICS_DATA_PATH")
Block::Statistics.const_set("STATISTICS_DATA_PATH", "lib/data/landing_page_content_items/statistics")
LandingPage::Block::Statistics.send(:remove_const, "STATISTICS_DATA_PATH")
LandingPage::Block::Statistics.const_set("STATISTICS_DATA_PATH", "lib/data/landing_page_content_items/statistics")
end

describe "#x_axis_keys" do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
RSpec.describe Block::TwoColumnLayout do
RSpec.describe LandingPage::Block::TwoColumnLayout do
let(:blocks_hash) do
{ "type" => "two_column_layout",
"theme" => "two_thirds_one_third",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
RSpec.describe BlockFactory do
RSpec.describe LandingPage::BlockFactory do
describe ".build" do
it "builds blocks of the correct type" do
expect(described_class.build({ "type" => "govspeak" }).type).to eq("govspeak")
Expand Down

0 comments on commit 1a660fb

Please sign in to comment.