Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert what happens next content to markdown #378

Merged
merged 2 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,5 @@ group :test do
# Code coverage reporter
gem "simplecov", require: false
end

gem "reverse_markdown", "~> 2.1"
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,8 @@ GEM
io-console (~> 0.5)
request_store (1.5.1)
rack (>= 1.4)
reverse_markdown (2.1.1)
nokogiri
rexml (3.2.6)
rspec-core (3.12.2)
rspec-support (~> 3.12.0)
Expand Down Expand Up @@ -332,6 +334,7 @@ DEPENDENCIES
pg (~> 1.5)
puma (~> 6.4)
rails (~> 7.0.8)
reverse_markdown (~> 2.1)
rspec-rails
rubocop-govuk
sentry-rails
Expand Down
65 changes: 65 additions & 0 deletions lib/tasks/what_happens_next_markdown.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
class TextHelpers
include ActionView::Helpers::TextHelper
end

namespace :what_happens_next_markdown do
desc "Populate what_happens_next_markdown field"
task populate: :environment do
Form.find_each do |form|
formatted_html = TextHelpers.new.simple_format(
form.what_happens_next_text,
{},
sanitize: true,
sanitize_options: {
tags: %w[a ol ul li p],
attributes: %w[href class rel target title],
},
)
markdown = ReverseMarkdown.convert(formatted_html).strip
form.what_happens_next_markdown = form.what_happens_next_text.blank? ? "" : markdown

form.made_live_forms.each do |made_live_form|
form_blob = JSON.parse(made_live_form.json_form_blob, symbolize_names: true)

formatted_html = TextHelpers.new.simple_format(
form_blob[:what_happens_next_text],
{},
sanitize: true,
sanitize_options: {
tags: %w[a ol ul li p],
attributes: %w[href class rel target title],
},
)
markdown = ReverseMarkdown.convert(formatted_html).strip
form_blob[:what_happens_next_markdown] = form_blob[:what_happens_next_text].blank? ? "" : markdown

made_live_form.update!(json_form_blob: form_blob.to_json)
end

form.save!
rescue StandardError => e
puts "Error processing form #{form.id} (#{form.name})"
puts e
end
end

desc "Depopulate what_happens_next_markdown field"
task depopulate: :environment do
Form.find_each do |form|
form.what_happens_next_markdown = nil

form.made_live_forms.each do |made_live_form|
form_blob = JSON.parse(made_live_form.json_form_blob, symbolize_names: true)

form_blob[:what_happens_next_markdown] = nil

made_live_form.update!(json_form_blob: form_blob.to_json)
end

form.save!
rescue StandardError => e
puts "Error processing form #{form.id} (#{form.name})"
puts e
end
end
end
46 changes: 46 additions & 0 deletions spec/lib/tasks/what_happens_next_markdown_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require "rails_helper"
require "rake"

RSpec.describe "what_happens_next_markdown.rake" do
describe "what_happens_next_markdown:populate", type: :task do
before do
Rake.application.rake_require "tasks/what_happens_next_markdown"
Rake::Task.define_task(:environment)
end

it "populates the what_happens_next_markdown field on the form and the live form JSON" do
form = create(:form, what_happens_next_text: "You will get a response soon.\n\nIn the meantime:<ul><li>A list item</li></ul>", what_happens_next_markdown: nil)
made_live_form = create(:made_live_form, form:)

Rake::Task["what_happens_next_markdown:populate"].invoke

form.reload
made_live_form.reload

made_live_form_blob = JSON.parse(made_live_form.json_form_blob, symbolize_names: true)
expect(form.what_happens_next_markdown).to eq "You will get a response soon.\n\nIn the meantime:\n\n- A list item"
expect(made_live_form_blob[:what_happens_next_markdown]).to eq "You will get a response soon.\n\nIn the meantime:\n\n- A list item"
end
end

describe "what_happens_next_markdown:depopulate", type: :task do
before do
Rake.application.rake_require "tasks/what_happens_next_markdown"
Rake::Task.define_task(:environment)
end

it "removes the what_happens_next_markdown content from the form and the live form JSON" do
form = create(:form, what_happens_next_text: "You will get a response soon.\n\nIn the meantime:<ul><li>A list item</li></ul>", what_happens_next_markdown: "You will get a response soon.\n\nIn the meantime:\n\n- A list item")
made_live_form = create(:made_live_form, form:)

Rake::Task["what_happens_next_markdown:depopulate"].invoke

form.reload
made_live_form.reload

made_live_form_blob = JSON.parse(made_live_form.json_form_blob, symbolize_names: true)
expect(form.what_happens_next_markdown).to eq nil
expect(made_live_form_blob[:what_happens_next_markdown]).to eq nil
end
end
end