Skip to content

Commit c92484c

Browse files
Add rich text field to school profile
1 parent 1613a85 commit c92484c

File tree

8 files changed

+91
-13
lines changed

8 files changed

+91
-13
lines changed

app/models/organisation.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ class Organisation < ApplicationRecord
66
include PgSearch::Model
77
extend FriendlyId
88

9+
has_rich_text :description
10+
911
SPECIAL_SCHOOL_TYPES = ["Community special school", "Foundation special school", "Non-maintained special school", "Academy special converter", "Academy special sponsor led", "Free schools special"].freeze
1012
NON_FAITH_RELIGIOUS_CHARACTER_TYPES = ["", "None", "Does not apply", "null"].freeze
1113
OUT_OF_SCOPE_DETAILED_SCHOOL_TYPES = ["Further education", "Other independent school", "Miscellaneous", "Special post 16 institution", "Other independent special school", "Higher education institutions", "Welsh establishment"].freeze
@@ -144,7 +146,7 @@ def has_ofsted_report?
144146
end
145147

146148
def profile_complete?
147-
%i[email description safeguarding_information logo photo url].all? { |attribute| send(attribute).present? }
149+
%i[email safeguarding_information logo photo url].all? { |attr| send(attr).present? } && description.body.present?
148150
end
149151

150152
def self.update_all_searchable_content!

app/views/publishers/organisations/_organisation.html.slim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
- summary_list.with_row(html_attributes: { id: "description" }) do |row|
4545
- row.with_key text: t("publishers.organisations.organisation.description.label.#{organisation.school? ? :school : :organisation}")
4646
- row.with_value do
47-
= required_profile_info(value: truncate(organisation.description, length: 130), missing_prompt: t("publishers.organisations.organisation.description.missing_prompt.#{organisation.school? ? :school : :organisation}"))
47+
= required_profile_info(value: sanitize(organisation.description.to_s), missing_prompt: t("publishers.organisations.organisation.description.missing_prompt.#{organisation.school? ? :school : :organisation}"))
4848
- row.with_action text: t("buttons.change"), href: edit_publishers_organisation_description_path(organisation), classes: "govuk-link--no-visited-state", visually_hidden_text: t("publishers.organisations.organisation.description.label.#{organisation.school? ? :school : :organisation}")
4949

5050
- summary_list.with_row(html_attributes: { id: "safeguarding_information" }) do |row|

app/views/publishers/organisations/description/edit.html.slim

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,16 @@
1212
- if params["vacancy_id"]
1313
= f.hidden_field "vacancy_id", value: params["vacancy_id"]
1414

15-
= f.govuk_text_area :description,
16-
label: { text: t("helpers.label.publishers_organisation_form.description", organisation_type: organisation_type_basic(@organisation)), size: "s" },
17-
hint: { text: t("helpers.hint.publishers_organisation_form.description", organisation_type: organisation_type_basic(@organisation)) },
18-
rows: 10
15+
.govuk-form-group
16+
= f.label :description, t("helpers.label.publishers_organisation_form.description", organisation_type: organisation_type_basic(@organisation)), class: "govuk-label govuk-label--s"
17+
18+
.govuk-hint
19+
= t("helpers.hint.publishers_organisation_form.description", organisation_type: organisation_type_basic(@organisation))
20+
21+
div class="school-description-form"
22+
= f.rich_text_area :description, aria: { label: t("helpers.label.publishers_organisation_form.description"), required: true }, class: "govuk-!-margin-bottom-6"
23+
24+
noscript
25+
= f.govuk_text_area :description, rows: 10, class: "govuk-!-margin-bottom-6"
1926

2027
= f.govuk_submit t("buttons.save_changes")
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
desc "Migrates organisation descriptions from legacy string column to ActionText"
2+
task migrate_school_description_to_rich_text: :environment do
3+
Organisation.find_each do |org|
4+
legacy_text = org.read_attribute(:description)
5+
6+
next if legacy_text.blank? || org.description.present?
7+
8+
begin
9+
org.update!(description: legacy_text)
10+
rescue StandardError => e
11+
Rails.logger.error "Error migrating Organisation #{org.id}: #{e.message}"
12+
end
13+
end
14+
puts "Migration complete."
15+
end
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
require "rails_helper"
2+
3+
RSpec.describe "migrate_school_description_to_rich_text" do
4+
include_context "rake"
5+
6+
let!(:school) { create(:school) }
7+
8+
before do
9+
Rake::Task["migrate_school_description_to_rich_text"].reenable
10+
11+
school.rich_text_description&.destroy!
12+
school.update_column(:description, "Legacy text description")
13+
end
14+
15+
# rubocop:disable RSpec/NamedSubject
16+
it "migrates the legacy string to ActionText" do
17+
expect {
18+
subject.invoke
19+
}.to change(ActionText::RichText, :count).by(1)
20+
21+
expect(school.reload.description.to_plain_text).to eq("Legacy text description")
22+
end
23+
24+
it "does not overwrite existing rich text" do
25+
school.reload.update!(description: "Modern Rich Text")
26+
27+
expect {
28+
subject.invoke
29+
}.not_to change(ActionText::RichText, :count)
30+
31+
expect(school.reload.description.to_plain_text).to eq("Modern Rich Text")
32+
end
33+
34+
it "logs an error if the migration fails for a specific organisation" do
35+
school.reload
36+
allow(Organisation).to receive(:find_each).and_yield(school)
37+
allow(school).to receive(:update!).and_raise(StandardError, "Database timeout")
38+
39+
expect(Rails.logger).to receive(:error).with("Error migrating Organisation #{school.id}: Database timeout")
40+
41+
expect {
42+
subject.invoke
43+
}.not_to change(ActionText::RichText, :count)
44+
end
45+
# rubocop:enable RSpec/NamedSubject
46+
end

spec/system/publishers/publishers_can_manage_a_profile_spec.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,10 @@
110110
click_link("Change")
111111
end
112112

113-
expect(find_field("publishers_organisation_description_form[description]").value).to eq(organisation.description)
113+
expected_text = organisation.description.body.to_plain_text
114114

115+
expect(find_field("publishers_organisation_description_form[description]").value)
116+
.to eq("#{expected_text}\n")
115117
fill_in "publishers_organisation_description_form[description]", with: school_description
116118
click_on I18n.t("buttons.save_changes")
117119

@@ -135,7 +137,10 @@
135137
click_link("Change")
136138
end
137139

138-
expect(find_field("publishers_organisation_description_form[description]").value).to eq(organisation.description)
140+
expected_text = organisation.description.body.to_plain_text
141+
142+
expect(find_field("publishers_organisation_description_form[description]").value)
143+
.to eq("#{expected_text}\n")
139144

140145
fill_in "publishers_organisation_description_form[description]", with: new_trust_description
141146
click_on I18n.t("buttons.save_changes")
@@ -152,7 +157,10 @@
152157
click_link("Change")
153158
end
154159

155-
expect(find_field("publishers_organisation_description_form[description]").value).to eq(school1.description)
160+
expected_text = school1.description.body.to_plain_text
161+
162+
expect(find_field("publishers_organisation_description_form[description]").value)
163+
.to eq("#{expected_text}\n")
156164

157165
fill_in "publishers_organisation_description_form[description]", with: new_school_description
158166
click_on I18n.t("buttons.save_changes")

spec/views/organisations/show.html.slim_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
end
7979

8080
it "displays the organisation's description" do
81-
expect(show_view).to have_content(organisation.description)
81+
expect(show_view).to have_content(organisation.description.to_plain_text)
8282
end
8383

8484
it "displays the organisation's safeguarding information" do

spec/views/publishers/organisations/preview.html.slim_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
end
1717

1818
it "displays the organisation's description" do
19-
expect(rendered).to have_content(organisation.description)
19+
expect(rendered).to have_content(organisation.description.to_plain_text)
2020
end
2121

2222
it "displays the organisation's safeguarding information" do
@@ -48,7 +48,7 @@
4848
end
4949

5050
it "displays the organisation's description" do
51-
expect(rendered).to have_content(organisation.description)
51+
expect(rendered).to have_content(organisation.description.to_plain_text)
5252
end
5353

5454
it "displays the organisation's safeguarding information" do
@@ -82,7 +82,7 @@
8282
end
8383

8484
it "displays the organisation's description" do
85-
expect(rendered).to have_content(school_one.description)
85+
expect(rendered).to have_content(school_one.description.to_plain_text)
8686
end
8787

8888
it "displays the organisation's safeguarding information" do

0 commit comments

Comments
 (0)