Skip to content

Commit 3210bfc

Browse files
committed
feat: rake task to upgrade to v2 + pimp files from the Maglev generator
1 parent 8fbdf99 commit 3210bfc

File tree

6 files changed

+68
-20
lines changed

6 files changed

+68
-20
lines changed

app/services/maglev/upgrade_from_v1.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def upgrade_layout_template
5252
template = load_old_layout_template
5353
.gsub('data-maglev-dropzone',
5454
"data-maglev-#{default_layout_group.id}-dropzone")
55-
.gsub('render_maglev_sections', "render_maglev_group :#{default_layout_group.id} %>")
55+
.gsub('render_maglev_sections', "render_maglev_group :#{default_layout_group.id}")
5656

5757
persist_layout_template(template, "#{default_layout.id}.html.erb")
5858
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# frozen_string_literal: true
2+
3+
module Maglev
4+
class MigrationV2Error < StandardError; end
5+
end
6+
7+
Rails.application.config.after_initialize do
8+
# Skip in test environment
9+
# Only check if we're running the server or console
10+
if !Rails.env.test? && (defined?(Rails::Server) || defined?(Rails::Console))
11+
site_exists = Maglev::Site.exists?
12+
store_exists = Maglev::SectionsContentStore.exists?
13+
14+
if site_exists && !store_exists
15+
raise Maglev::MigrationV2Error,
16+
'Your Maglev site needs to be migrated to V2. Please run: rails maglev:upgrade_from_v1'
17+
end
18+
end
19+
end

lib/generators/maglev/templates/theme/app/theme/theme.yml.tt

+25-15
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,40 @@ name: "theme"
66
description: "A brief description of the theme"
77

88
section_categories:
9-
- name: content
9+
- name: Headers
10+
id: headers
11+
- name: Content
12+
id: content
13+
- name: Footers
14+
id: footers
1015

1116
# Properties of your theme such as the primary color, font name, ...etc.
1217
style_settings: []
1318

14-
pages:
15-
- title: "Home page"
16-
path: "/index"
17-
18-
# List of CSS class names used by your library of icons (font awesome, remixicons, ...etc)
19-
icons: []
20-
21-
# Enable the section mirroring feature (default: false)
22-
# mirror_section: true
23-
19+
# Define layouts with organized groups
2420
layouts:
2521
- label: "Default"
2622
id: "default"
2723
groups:
2824
- label: "Header"
2925
id: "header"
30-
store: "header"
31-
# accept: ["navbar"]
26+
accept: ["headers/*"] # Only allow sections from the headers group
27+
recoverable: ["navbar"] # Make navbar recoverable
28+
# mirror_section: false # Disable mirroring feature for consistency
3229
- label: "Main"
3330
id: "main"
34-
store: "page"
35-
- "Footer"
31+
store: "page" # Store content per-page
32+
- label: "Footer"
33+
id: "footer"
34+
accept: ["footers/*"] # Only allow sections from the footers group
35+
36+
pages:
37+
- title: "Home page"
38+
path: "/index"
39+
layout_id: "default" # Use the default layout
40+
41+
# List of CSS class names used by your library of icons (font awesome, remixicons, ...etc)
42+
icons: []
43+
44+
# Enable the section mirroring feature (default: false)
45+
# mirror_section: true

lib/generators/maglev/templates/theme/app/views/theme/layouts/default.html.erb.tt

+7-4
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,19 @@
1515

1616
<script src="https://cdn.tailwindcss.com?plugins=forms,typography,aspect-ratio,line-clamp"></script>
1717
</head>
18-
<body class="bg-white">
19-
<header data-maglev-header-dropzone>
18+
<body class="min-h-screen flex flex-col">
19+
<%# Header group for site-wide navigation and announcements %>
20+
<header class="bg-white shadow-sm" data-maglev-header-dropzone>
2021
<%%= render_maglev_group :header %>
2122
</header>
2223

23-
<main data-maglev-main-dropzone>
24+
<%# Main content group for page-specific content %>
25+
<main class="flex-grow" data-maglev-main-dropzone>
2426
<%%= render_maglev_group :main %>
2527
</main>
2628

27-
<footer data-maglev-footer-dropzone>
29+
<%# Footer group for site-wide footer content %>
30+
<footer class="bg-gray-50" data-maglev-footer-dropzone>
2831
<%%= render_maglev_group :footer %>
2932
</footer>
3033
</body>

lib/tasks/maglev_tasks.rake

+9
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@ namespace :maglev do
5555
end
5656
end
5757

58+
desc 'Upgrade Maglev from V1 to V2'
59+
task upgrade_from_v1: :environment do
60+
Maglev::UpgradeFromV1.call(
61+
site: Maglev::Site.first,
62+
theme: Maglev.local_themes.first
63+
)
64+
puts '🛠️ Your site has been upgraded to V2 with success!'
65+
end
66+
5867
namespace :vite do
5968
desc 'Bundle frontend entrypoints using ViteRuby'
6069
task build: :'vite:verify_install' do

spec/dummy/db/schema.rb

+7
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,17 @@
100100
t.string "name"
101101
t.datetime "created_at", null: false
102102
t.datetime "updated_at", null: false
103+
t.jsonb "navigation", default: []
103104
t.jsonb "locales", default: []
104105
t.jsonb "sections_translations", default: {}
105106
t.integer "lock_version"
107+
t.string "siteable_type"
108+
t.bigint "siteable_id"
109+
t.string "handle"
110+
t.string "theme_id"
111+
t.string "domain"
106112
t.jsonb "style", default: []
113+
t.index ["siteable_type", "siteable_id"], name: "index_maglev_sites_on_siteable"
107114
end
108115

109116
create_table "products", force: :cascade do |t|

0 commit comments

Comments
 (0)