-
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.
- Loading branch information
1 parent
8f839a4
commit 8021f17
Showing
3 changed files
with
105 additions
and
51 deletions.
There are no files selected for viewing
51 changes: 0 additions & 51 deletions
51
db/migrate/20231201144634_populate_what_happens_next_markdown.rb
This file was deleted.
Oops, something went wrong.
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,59 @@ | ||
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! | ||
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! | ||
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 |