-
Notifications
You must be signed in to change notification settings - Fork 20
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
module LandingPage::Block | ||
class Component < Base | ||
ALLOWED_COMPONENTS = %w[ | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<%= render "govuk_publishing_components/components/#{block.component_name}", block.component_attributes %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
|
@@ -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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick: no need to emphasise |
||
|
||
```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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be
|
||
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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.