Skip to content

Commit

Permalink
Generate error message content from locale files
Browse files Browse the repository at this point in the history
Previously the error messages for Service Feedback and Assisted Digital Feedback were either set in the model or generated automatically by Rails. They have now been set to read from locale files. This will mean that error messages won't render in English on Welsh pages.

- TODO - currently waiting on Welsh translations from an external agency
  • Loading branch information
Rosa-Fox committed Feb 2, 2023
1 parent 93e594c commit 8c61ef5
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
20 changes: 10 additions & 10 deletions app/models/assisted_digital_feedback.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,59 +16,59 @@ class AssistedDigitalFeedback < Ticket
:assistance_improvement_comments_other

validates :assistance_received,
presence: { message: "You must select if you received assistance with this service" },
presence: { message: I18n.translate("activemodel.errors.models.assisted_digital_feedback.select_assistance_received") },
inclusion: { in: %w[yes no] }

validates :assistance_received_comments,
length: {
maximum: FIELD_MAXIMUM_CHARACTER_COUNT,
message: "The message field can be max #{FIELD_MAXIMUM_CHARACTER_COUNT} characters",
message: I18n.translate("activemodel.errors.models.assisted_digital_feedback.max_character_count", field_maximum_character_count: FIELD_MAXIMUM_CHARACTER_COUNT),
},
presence: true,
if: :assistance_received?

validates :assistance_provided_by,
presence: true,
presence: { message: I18n.translate("activemodel.errors.models.assisted_digital_feedback.cant_be_blank") },
inclusion: { in: %w[friend-relative work-colleague government-staff other] },
if: :assistance_received?

validates :assistance_provided_by_other,
presence: true,
presence: { message: I18n.translate("activemodel.errors.models.assisted_digital_feedback.cant_be_blank") },
length: { maximum: 512 },
if: :assistance_provided_by_other?

validates :assistance_satisfaction_rating,
presence: true,
presence: { message: I18n.translate("activemodel.errors.models.assisted_digital_feedback.select_rating") },
inclusion: { in: ("1".."5").to_a },
if: :assistance_provided_by_government_staff?

validates :assistance_satisfaction_rating_other,
presence: true,
presence: { message: I18n.translate("activemodel.errors.models.assisted_digital_feedback.select_rating") },
inclusion: { in: ("1".."5").to_a },
if: :assistance_provided_by_other?

validates :assistance_improvement_comments,
length: {
maximum: FIELD_MAXIMUM_CHARACTER_COUNT,
message: "The message field can be max #{FIELD_MAXIMUM_CHARACTER_COUNT} characters",
message: I18n.translate("activemodel.errors.models.assisted_digital_feedback.max_character_count", field_maximum_character_count: FIELD_MAXIMUM_CHARACTER_COUNT),
},
if: :assistance_provided_by_government_staff?

validates :assistance_improvement_comments_other,
length: {
maximum: FIELD_MAXIMUM_CHARACTER_COUNT,
message: "The message field can be max #{FIELD_MAXIMUM_CHARACTER_COUNT} characters",
message: I18n.translate("activemodel.errors.models.assisted_digital_feedback.max_character_count", field_maximum_character_count: FIELD_MAXIMUM_CHARACTER_COUNT),
},
if: :assistance_provided_by_other?

validates :service_satisfaction_rating,
presence: { message: "You must select a rating" },
presence: { message: I18n.translate("activemodel.errors.models.assisted_digital_feedback.select_rating") },
inclusion: { in: ("1".."5").to_a }

validates :improvement_comments,
length: {
maximum: FIELD_MAXIMUM_CHARACTER_COUNT,
message: "The message field can be max #{FIELD_MAXIMUM_CHARACTER_COUNT} characters",
message: I18n.translate("activemodel.errors.models.assisted_digital_feedback.max_character_count", field_maximum_character_count: FIELD_MAXIMUM_CHARACTER_COUNT),
}

validates :slug, length: { maximum: 512 }
Expand Down
4 changes: 2 additions & 2 deletions app/models/service_feedback.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ class ServiceFeedback < Ticket
attr_accessor :service_satisfaction_rating, :slug, :javascript_enabled, :referrer
attr_writer :improvement_comments

validates :service_satisfaction_rating, presence: { message: "You must select a rating" }
validates :service_satisfaction_rating, presence: { message: I18n.translate("activemodel.errors.models.service_feedback.select_rating") }
validates :service_satisfaction_rating, inclusion: { in: ("1".."5").to_a }
validates :improvement_comments, length: { maximum: FIELD_MAXIMUM_CHARACTER_COUNT, message: "The message field can be max #{FIELD_MAXIMUM_CHARACTER_COUNT} characters" }
validates :improvement_comments, length: { maximum: FIELD_MAXIMUM_CHARACTER_COUNT, message: I18n.translate("activemodel.errors.models.service_feedback.max_character_count", field_maximum_character_count: FIELD_MAXIMUM_CHARACTER_COUNT) }
validates :slug, length: { maximum: 512 }

def improvement_comments
Expand Down
9 changes: 9 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ en:
attributes:
survey_id:
is_not_currently_running: "The survey is not currently active"
service_feedback:
select_rating: You must select a rating
max_character_count: The message field can be max %{field_maximum_character_count} characters
assisted_digital_feedback:
select_assistance_received: You must select if you received assistance with this service
max_character_count: The message field can be max %{field_maximum_character_count} characters
cant_be_blank: Can't be blank
select_rating: You must select a rating

controllers:
contact:
govuk:
Expand Down
4 changes: 2 additions & 2 deletions spec/requests/assisted_digital_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@
click_on I18n.translate("controllers.contact.govuk.assisted_digital_feedback.send_feedback")
end

expect(page).to have_content "Assistance satisfaction rating: Can't be blank"
expect(page).to have_content "Assistance satisfaction rating: You must select a rating"
end

it "displays an error message when assistance was provided by government staff and how could we improve this service? is too long" do
Expand Down Expand Up @@ -262,7 +262,7 @@
end

expect(page).to have_content "Assistance provided by other: Can't be blank"
expect(page).to have_content "Assistance satisfaction rating other: Can't be blank"
expect(page).to have_content "Assistance satisfaction rating other: You must select a rating"
end

it "displays an error message when assistance was provided by other and How could we improve this service? is too long" do
Expand Down

0 comments on commit 8c61ef5

Please sign in to comment.