generated from DFE-Digital/govuk-rails-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…out-user Handle `nil` users when rendering validation errors
- Loading branch information
Showing
4 changed files
with
90 additions
and
25 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
app/components/admin/performance/validation_error_component.html.erb
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,23 @@ | ||
<div class="govuk-summary-card"> | ||
<div class="govuk-summary-card__title-wrapper"> | ||
<h2 class="govuk-summary-card__title govuk-!-font-weight-regular"> | ||
<%= header_label %> | ||
</h2> | ||
</div> | ||
<div class="govuk-summary-card__content"> | ||
<dl class="govuk-summary-list"> | ||
<% error.details.each do |field, info| %> | ||
<div class="govuk-summary-list__row"> | ||
<dt class="govuk-summary-list__key"> | ||
<%= field %> | ||
</dt> | ||
<dd class="govuk-summary-list__value"> | ||
<div class="govuk-error-message govuk-!-font-weight-regular"><%= info['messages'].join(", ") %></div> | ||
<code class="app-user-input-example"><%= info['value'] %></code> | ||
<br> | ||
</dd> | ||
</div> | ||
<% end %> | ||
</dl> | ||
</div> | ||
</div> |
33 changes: 33 additions & 0 deletions
33
app/components/admin/performance/validation_error_component.rb
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,33 @@ | ||
# frozen_string_literal: true | ||
|
||
module Admin | ||
module Performance | ||
class ValidationErrorComponent < BaseComponent | ||
attr_reader :error | ||
|
||
def initialize(error:) | ||
@error = error | ||
end | ||
|
||
def header_label | ||
"#{validation_error_label}#{created_at_label}#{user_label}" | ||
end | ||
|
||
private | ||
|
||
def validation_error_label | ||
"Validation error ##{error.id.slice(0, 8)} – " | ||
end | ||
|
||
def created_at_label | ||
error.created_at.strftime("%d %B %Y at %H:%M") | ||
end | ||
|
||
def user_label | ||
if error.user | ||
" by user #{error.user.full_name}" | ||
end | ||
end | ||
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
33 changes: 33 additions & 0 deletions
33
spec/components/performance/validation_error_component_spec.rb
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,33 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe Admin::Performance::ValidationErrorComponent, type: :component do | ||
subject { described_class.new(error: validation_error) } | ||
|
||
let(:user) { create(:user, full_name: "Performance User") } | ||
let(:validation_error) do | ||
build( | ||
:validation_error, | ||
id: "0d8f4556-1c3d-4e3e-8e3e-3e3e3e3e3e3e", | ||
created_at: "2023-06-16 03:27:00", | ||
user:, | ||
) | ||
end | ||
|
||
describe "validation error with a user" do | ||
it "produces header_label with user info" do | ||
expect(subject.header_label).to eq( | ||
"Validation error #0d8f4556 – 16 June 2023 at 03:27 by user Performance User", | ||
) | ||
end | ||
end | ||
|
||
describe "validation error without a user" do | ||
let(:user) { nil } | ||
|
||
it "produces header_label without user info" do | ||
expect(subject.header_label).to eq( | ||
"Validation error #0d8f4556 – 16 June 2023 at 03:27", | ||
) | ||
end | ||
end | ||
end |