Skip to content

Commit

Permalink
Merge pull request #291 from DFE-Digital/allow-extra-content-to-be-in…
Browse files Browse the repository at this point in the history
…jected-into-error-summaries

Allow extra content to be injected into error summaries
  • Loading branch information
peteryates authored Jun 14, 2021
2 parents d052c0e + 8bbf9db commit 4ee6a3b
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 13 deletions.
2 changes: 2 additions & 0 deletions guide/content/building-blocks/injecting-content.slim
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ ul.govuk-list.govuk-list--bullet
code #govuk_date_field
li
code #govuk_email_field
li
code #govuk_error_summary
li
code #govuk_file_field
li
Expand Down
5 changes: 3 additions & 2 deletions lib/govuk_design_system_formbuilder/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,7 @@ def govuk_date_field(attribute_name, hint: {}, legend: {}, caption: {}, date_of_
# attributes will appear first and unordered ones will be last, sorted in the default manner (in
# which they were defined on the model).
# @option kwargs [Hash] kwargs additional arguments are applied as attributes to the error summary +div+ element
# @param block [Block] arbitrary HTML that will be rendered between title and error message list
#
# @note Only the first error in the +#errors+ array for each attribute will
# be included.
Expand All @@ -942,8 +943,8 @@ def govuk_date_field(attribute_name, hint: {}, legend: {}, caption: {}, date_of_
# = f.govuk_error_summary 'Uh-oh, spaghettios'
#
# @see https://design-system.service.gov.uk/components/error-summary/ GOV.UK error summary
def govuk_error_summary(title = config.default_error_summary_title, link_base_errors_to: nil, order: nil, **kwargs)
Elements::ErrorSummary.new(self, object_name, title, link_base_errors_to: link_base_errors_to, order: order, **kwargs).html
def govuk_error_summary(title = config.default_error_summary_title, link_base_errors_to: nil, order: nil, **kwargs, &block)
Elements::ErrorSummary.new(self, object_name, title, link_base_errors_to: link_base_errors_to, order: order, **kwargs, &block).html
end

# Generates a fieldset containing the contents of the block
Expand Down
4 changes: 2 additions & 2 deletions lib/govuk_design_system_formbuilder/builder_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ def govuk_field_id(object, attribute_name, object_name = nil, value: nil, link_e
# = govuk_error_summary(@registration)
#
# @see https://design-system.service.gov.uk/components/error-summary/ GOV.UK error summary
def govuk_error_summary(object, object_name = nil, *args, **kwargs)
def govuk_error_summary(object, object_name = nil, *args, **kwargs, &block)
(object_name = retrieve_object_name(object)) if object_name.nil?

proxy_builder(object, object_name, self, {}).govuk_error_summary(*args, **kwargs)
proxy_builder(object, object_name, self, {}).govuk_error_summary(*args, **kwargs, &block)
end

private
Expand Down
14 changes: 5 additions & 9 deletions lib/govuk_design_system_formbuilder/elements/error_summary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ class ErrorSummary < Base
include Traits::Error
include Traits::HTMLAttributes

def initialize(builder, object_name, title, link_base_errors_to:, order:, **kwargs)
super(builder, object_name, nil)
def initialize(builder, object_name, title, link_base_errors_to:, order:, **kwargs, &block)
super(builder, object_name, nil, &block)

@title = title
@link_base_errors_to = link_base_errors_to
Expand All @@ -28,16 +28,12 @@ def title
end

def summary
tag.div(class: summary_class('body')) do
tag.ul(class: [%(#{brand}-list), summary_class('list')]) do
safe_join(list)
end
end
tag.div(class: summary_class('body')) { safe_join([@block_content, list]) }
end

def list
error_messages.map do |attribute, messages|
list_item(attribute, messages.first)
tag.ul(class: [%(#{brand}-list), summary_class('list')]) do
safe_join(error_messages.map { |attribute, messages| list_item(attribute, messages.first) })
end
end

Expand Down
20 changes: 20 additions & 0 deletions spec/govuk_design_system_formbuilder/builder/error_summary_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -404,5 +404,25 @@
let(:expected_class) { 'govuk-error-summary' }
end
end

context 'when a block of html is supplied' do
let(:custom_content_tag) { :marquee }
let(:custom_content_text) { "Fix the things below" }
subject do
builder.send(*args) do
builder.content_tag(custom_content_tag, custom_content_text)
end
end

before { object.valid? }

specify "the custom content should be present in the error summary" do
expect(subject).to have_tag("div", with: { class: "govuk-error-summary" }) do
with_tag("div", with: { class: "govuk-error-summary__body" }) do
with_tag(custom_content_tag, text: custom_content_text)
end
end
end
end
end
end
16 changes: 16 additions & 0 deletions spec/govuk_design_system_formbuilder/helper/builder_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,21 @@ def initialize
is_expected.to render_an_error_summary.with(object.errors.size).errors
end
end

context 'when a block is provided' do
let(:custom_content_text) { "Fix the things below" }

subject do
helper.govuk_error_summary(*args) do
custom_content_text
end
end

specify "the custom content should be present in the error summary" do
expect(subject).to have_tag("div", with: { class: "govuk-error-summary" }) do
with_tag("div", with: { class: "govuk-error-summary__body" }, text: Regexp.new(custom_content_text))
end
end
end
end
end

0 comments on commit 4ee6a3b

Please sign in to comment.