-
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 rake task for converting whn to markdown
- Loading branch information
1 parent
e3dc66a
commit 64d4a9b
Showing
1 changed file
with
50 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,50 @@ | ||
namespace :what_happens_next_markdown do | ||
include ActionView::Helpers::TextHelper | ||
|
||
def generate_markdown(text) | ||
formatted_html = simple_format( | ||
text, | ||
{}, | ||
sanitize: true, | ||
sanitize_options: { | ||
tags: %w[a ol ul li p], | ||
attributes: %w[href class rel target title], | ||
}, | ||
) | ||
ReverseMarkdown.convert(formatted_html).strip | ||
end | ||
|
||
desc "Populate what_happens_next_markdown" | ||
task populate: :environment do |_, _args| | ||
Form.find_each do |form| | ||
markdown = generate_markdown(form.what_happens_next_text) | ||
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) | ||
markdown = generate_markdown(form_blob[:what_happens_next_text]) | ||
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" | ||
task depopulate: :environment do |_, _args| | ||
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 |