Skip to content

Commit f640ea6

Browse files
committed
Make title errors use the correct translation
This ensures that when a title is missing, the correct translation is used for the title field, rather than `Enter a title`
1 parent 2a9e6de commit f640ea6

4 files changed

Lines changed: 38 additions & 17 deletions

File tree

app/models/edition.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
class Edition < ApplicationRecord
2-
validates :title, presence: true
2+
include ApplicationHelper
3+
4+
validates :title, presence: {
5+
message: lambda do |edition, _|
6+
I18n.t("activerecord.errors.models.edition.blank",
7+
attribute_with_indefinite_article: edition.title_name_with_indefinite_article)
8+
end,
9+
}
310
validates :change_note, presence: true, if: :major_change?, on: :change_note
411
validates :major_change, inclusion: [true, false], on: :change_note
512

@@ -111,6 +118,12 @@ def is_deletable?
111118
can_transition?(:delete)
112119
end
113120

121+
def title_name_with_indefinite_article
122+
attribute_key = "activerecord.attributes.edition/document.title.#{block_type || 'default'}"
123+
attribute_name = I18n.t(attribute_key, default: I18n.t("activerecord.attributes.edition/document.title.default"))
124+
add_indefinite_article(attribute_name.downcase)
125+
end
126+
114127
private
115128

116129
def remove_destroyed(item)

config/locales/en.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ en:
4949
bands:
5050
name:
5151
blank: "Enter a band name"
52-
title:
53-
blank: "Enter a title"
5452
change_note:
5553
blank: "Enter a change note"
5654
lead_organisation_id:

spec/requests/workflow_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ def self.it_shows_the_correct_context
333333
}
334334

335335
expect(response).to render_template("editions/workflow/edit_draft")
336-
expect(page).to have_text(I18n.t("activerecord.errors.models.edition.attributes.title.blank"))
336+
expect(page).to have_text(I18n.t("activerecord.errors.models.edition.blank", attribute_with_indefinite_article: "a title"))
337337
end
338338
end
339339
end

spec/unit/app/models/edition/edition_spec.rb

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -118,20 +118,30 @@
118118
end
119119
end
120120

121-
it "validates the presence of an edition title" do
122-
edition = build(
123-
:edition,
124-
created_at:,
125-
updated_at:,
126-
details:,
127-
document_attributes: {},
128-
lead_organisation_id: organisation.id.to_s,
129-
title: nil,
130-
)
121+
{
122+
pension: "a title",
123+
contact: "a contact name",
124+
tax: "a tax name",
125+
time_period: "a time period name",
126+
}.each do |block_type, attribute_name|
127+
context "when the edition is a #{block_type}" do
128+
it "validates the presence of an edition title with the correct error message" do
129+
edition = build(
130+
:edition,
131+
block_type,
132+
created_at:,
133+
updated_at:,
134+
details:,
135+
document: build(:document, block_type: block_type),
136+
lead_organisation_id: organisation.id.to_s,
137+
title: nil,
138+
)
131139

132-
aggregate_failures do
133-
expect_model_not_to_be_valid(model: edition)
134-
expect(edition.errors.full_messages).to include("Enter a title")
140+
aggregate_failures do
141+
expect_model_not_to_be_valid(model: edition)
142+
expect(edition.errors.full_messages).to include("Enter #{attribute_name}")
143+
end
144+
end
135145
end
136146
end
137147

0 commit comments

Comments
 (0)