Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add generic component block #4527

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions app/models/landing_page/block/component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
module LandingPage::Block
class Component < Base
ALLOWED_COMPONENTS = %w[
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'd start very small with this list and expand it later if needed, rather than start big and then whittle down. There's a lot of components in this list that I'd have questions around how/if they'd be used on these pages.

For a starting list I'd suggest only action_link, big_number, cards, document_list, govspeak, heading, lead_paragraph, share_links - basically the ones we've implemented already.

action_link
attachment
back_link
big_number
button
cards
chat_entry
contents_list
copy_to_clipboard
details
document_list
glance_metric
govspeak
heading
image_card
inset_text
inverse_header
lead_paragraph
list
notice
organisation_logo
page_title
previous_and_next_navigation
print_link
share_links
warning_text
].freeze

attr_reader :component_name

def initialize(block_hash, landing_page)
super

@component_name = data["component_name"]
raise "Component #{component_name} is not in the allowed list of components for this block" unless ALLOWED_COMPONENTS.include?(component_name)
end

def component_attributes
data.except("type", "component_name").symbolize_keys
end
end
end
1 change: 1 addition & 0 deletions app/views/landing_page/blocks/_component.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= render "govuk_publishing_components/components/#{block.component_name}", block.component_attributes %>
12 changes: 12 additions & 0 deletions docs/building_blocks_for_flexible_content.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Simple blocks generally render one component or "thing". They can either be rend

- [Action Link](#action-link)
- [Big Number](#big-number)
- [Component](#component)
- [Document List](#document-list)
- [Govspeak](#govspeak)
- [Heading](#heading)
Expand Down Expand Up @@ -61,6 +62,17 @@ A wrapper around the [Big number component](https://components.publishing.servic
label: amount of money that looks big
```

#### Component

Renders whatever component from the publishing components gem is specified with the `component_name` value. All attributes _except_ `component_name` and `type` are passed directly to the component. Note that `component_name` should be the component in snake case (ie as it appears at the end of the url in the component guide). The example here will render identically to the Big Number example directly above, for instance.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: no need to emphasise except.


```yaml
- type: component
component_name: big_number
number: £75m
label: amount of money that looks big
```

#### Document List

A wrapper around the [Document list component](https://components.publishing.service.gov.uk/component-guide/document_list) with a title that will only appear if there are any items in the list (if the list is entirely empty the heading will not appear either)
Expand Down
3 changes: 3 additions & 0 deletions spec/fixtures/landing_page.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ blocks:
<a href="/landing-page/sub-page-1">Visit sub-page 1</a>
- type: govspeak
content: <p>Here's some more content!</p>
- type: component
component_name: big_number
number: 555
- type: quote
text: "Here's a quote!"
cite: "I said this"
Expand Down
36 changes: 36 additions & 0 deletions spec/models/landing_page/block/component_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
RSpec.describe LandingPage::Block::Component do
it_behaves_like "it is a landing-page block"

let(:blocks_hash) do
{ "type" => "component",
"component_name" => "big_number",
"number" => 123,
"label" => "Number of things" }
end
let(:subject) { described_class.new(blocks_hash, build(:landing_page)) }

describe "allowed list of components" do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be describe "#component_name" then have the following two nested contexts/tests:

  • context "when the component_name is in the allowed list"
    • it "sets the components name"
  • context "when the component_name is not in the allowed list"
    • it "raises an error"

it "allows components on the list" do
expect { subject }.not_to raise_error
end

context "when the component_name is not in the allowed list" do
let(:blocks_hash) do
{ "type" => "component",
"component_name" => "layout_for_public",
"number" => 123,
"label" => "Number of things" }
end

it "raises an error" do
expect { subject }.to raise_error("Component layout_for_public is not in the allowed list of components for this block")
end
end
end

describe "#component_attributes" do
it "returns all attributes except the type and component_name" do
expect(subject.component_attributes).to eq(number: 123, label: "Number of things")
end
end
end
Loading