-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add task to convert what_happens_next to markdown
Adds a rake task which uses the ReverseMarkdown gem to convert each form and made_live_form's what_happens_next_text content to markdown, and save this markdown to the what_happens_next_markdown field. Also adds a task to clear the content in the what_happens_next_markdown field in case we need to revert the change.
- Loading branch information
1 parent
58ebd6b
commit f532286
Showing
2 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
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,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 |
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,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 |