Skip to content

Commit e855a90

Browse files
committed
Add rake task to migrate page contents to a rich text content block
1 parent fe457d3 commit e855a90

File tree

3 files changed

+32
-11
lines changed

3 files changed

+32
-11
lines changed

app/views/better_together/pages/_form.html.erb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,8 @@
104104

105105
<div class="row my-3">
106106
<div class="col">
107-
<h2>Page Contents</h2>
108-
<div class="row mb-3">
109-
<%= render partial: 'better_together/shared/translated_rich_text_field', locals: { model: page, form: form, attribute: 'content' } %>
110-
</div>
111107
<div class="row">
112-
<h2>Blocks</h2>
108+
<h2>Page Blocks</h2>
113109

114110
<div id="blocks-list" data-controller="page-blocks" data-page-blocks-target="blocks">
115111
<!-- Render existing PageBlocks -->

app/views/better_together/pages/show.html.erb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@
33
<% end %>
44

55
<% content_for :page_content do %>
6-
<%= @page.content if @page.content.present? %>
7-
8-
<% if @page.blocks.any? %>
9-
<%= render @page.blocks %>
10-
<% end %>
6+
<%= render @page.blocks if @page.blocks.any? %>
117

128
<% if params[:path].blank? %>
139
<% content_for :page_title do %>
@@ -21,7 +17,7 @@
2117
<% end %>
2218

2319
<%= render layout: @layout do %>
24-
<% if @page.template.present? && @page.content.blank? %>
20+
<% if @page.template.present? && @page.page_blocks.empty? %>
2521
<%= render template: @page.template %>
2622
<% else %>
2723
<%= yield :page_content %>

lib/tasks/better_together_tasks.rake

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,33 @@ namespace :better_together do
2121
task generate_setup_wizard: :environment do
2222
BetterTogether::SetupWizardBuilder.build(clear: true)
2323
end
24+
25+
desc 'Migrate existing page contents to blocks'
26+
task migrate_page_contents_to_blocks: :environment do
27+
puts "======================"
28+
29+
content_translations = ActionText::RichText.where(
30+
record_type: 'BetterTogether::Page',
31+
name: 'content',
32+
locale: I18n.available_locales
33+
)
34+
35+
pages = BetterTogether::Page.where(id: content_translations.pluck(:record_id))
36+
37+
puts pages.size, 'pages'
38+
content_attrs = BetterTogether::Page.localized_attribute_list.select { |attribute| attribute.to_s.start_with?('content') }
39+
40+
pages.each do |page|
41+
next if page.page_blocks.any?
42+
43+
page_block = page.page_blocks.build
44+
block = page_block.build_block(type: 'BetterTogether::Content::RichText')
45+
46+
content_attrs.each do |attr|
47+
block.public_send("#{attr}=", page.public_send(attr))
48+
end
49+
50+
page_block.save!
51+
end
52+
end
2453
end

0 commit comments

Comments
 (0)