Skip to content

Commit

Permalink
Add migration for converting whn to markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidBiddle committed Dec 4, 2023
1 parent 4afc134 commit 0006406
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions db/migrate/20231201144634_populate_what_happens_next_markdown.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
class PopulateWhatHappensNextMarkdown < ActiveRecord::Migration[7.0]
include ActionView::Helpers::TextHelper

def change
reversible do |direction|
Form.find_each do |form|
direction.up do
formatted_html = 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
end
direction.down do
form.what_happens_next_markdown = nil
end

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

direction.up do
formatted_html = 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
end
direction.down do
form_blob[:what_happens_next_markdown] = nil
end

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

form.save!
end
end
end
end

0 comments on commit 0006406

Please sign in to comment.