Skip to content

Commit e49cdc8

Browse files
committed
feat: use Rails commands to replace rake tasks with business logic + write 2 new commands to remove/rename section types
1 parent e6a128e commit e49cdc8

11 files changed

+214
-128
lines changed

app/services/maglev/remove_section_type.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ def remove_resource_sections(resource)
3636
end
3737

3838
def remove_sections_of_type(sections, type)
39+
return 0 if sections.blank?
40+
3941
original_size = sections.size
4042
sections.reject! { |section| section['type'] == type }
4143
original_size - sections.size

app/services/maglev/rename_section_type.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ def rename_resource_sections(resource)
4343
end
4444

4545
def change_section_type(sections, old_type, new_type)
46+
return if sections.blank?
47+
4648
sections.each do |section|
4749
section['type'] = new_type if section['type'] == old_type
4850
end
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails'
4+
5+
module Maglev
6+
class ChangeSiteLocalesCommand < Rails::Command::Base
7+
desc 'change_site_locales [LOCALES]', 'Change site locales (format: label:prefix)'
8+
9+
def self.banner
10+
'bin/rails maglev:change_site_locales [label:prefix label2:prefix2 ...]'
11+
end
12+
13+
def perform(*locale_args)
14+
require File.expand_path('config/environment', Rails.root)
15+
16+
site = fetch_site
17+
return if site.blank?
18+
19+
locales = build_locales(locale_args)
20+
return if locales.empty?
21+
22+
update_site_locales(site, locales)
23+
end
24+
25+
private
26+
27+
def fetch_site
28+
Maglev::Site.first.tap do |site|
29+
say("[Error] You don't seem to have an existing site. 🤔", :red) unless site
30+
end
31+
end
32+
33+
def build_locales(locale_args)
34+
build_custom_locales(locale_args).tap do |locales|
35+
validate_locales(locales)
36+
end
37+
end
38+
39+
def build_custom_locales(locale_args)
40+
(locale_args || []).map do |arg|
41+
label, prefix = arg.split(':')
42+
Maglev::Site::Locale.new(label: label, prefix: prefix)
43+
end
44+
end
45+
46+
def validate_locales(locales)
47+
return true if !locales.empty? && locales.all?(&:valid?)
48+
49+
say("[Error] make sure your locales follow the 'label:prefix' pattern. 🤓", :red)
50+
false
51+
end
52+
53+
def update_site_locales(site, locales)
54+
Maglev::ChangeSiteLocales.call(
55+
site: site,
56+
locales: locales
57+
)
58+
say('Success! 🎉🎉🎉', :green)
59+
end
60+
end
61+
end
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails'
4+
5+
module Maglev
6+
class CreateSiteCommand < Rails::Command::Base
7+
desc 'create_site', 'Create your site'
8+
9+
def self.banner
10+
'bin/rails maglev:create_site'
11+
end
12+
13+
def perform
14+
require File.expand_path('config/environment', Rails.root)
15+
16+
if Maglev::Site.exists?
17+
say '🤔 You already have a site. 🤔', :yellow
18+
return
19+
end
20+
21+
Maglev::GenerateSite.call(
22+
theme: Maglev.local_themes.first
23+
)
24+
25+
say '🎉 Your site has been created with success!'
26+
end
27+
end
28+
end
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails'
4+
5+
module Maglev
6+
module Sections
7+
class RemoveCommand < Rails::Command::Base
8+
desc 'remove TYPE', 'Remove a section type across the site and its pages'
9+
10+
def self.banner
11+
'bin/rails maglev:sections:remove TYPE'
12+
end
13+
14+
def perform(type)
15+
require File.expand_path('config/environment', Rails.root)
16+
17+
site = fetch_site
18+
19+
return if site.blank?
20+
21+
removed_count = Maglev::RemoveSectionType.call(
22+
site: site,
23+
type: type
24+
)
25+
26+
display_final_message(removed_count, type)
27+
end
28+
29+
private
30+
31+
def fetch_site
32+
Maglev::Site.first.tap do |site|
33+
say("[Error] You don't seem to have an existing site. 🤔", :red) unless site
34+
end
35+
end
36+
37+
def display_final_message(removed_count, type)
38+
if removed_count.zero?
39+
say "No section of type '#{type}' found 🤔", :yellow
40+
return
41+
end
42+
43+
say "Successfully removed #{removed_count} #{'section'.pluralize(removed_count)} of type '#{type}' 🎉",
44+
:green
45+
end
46+
end
47+
end
48+
end
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# frozen_string_literal: true
2+
3+
module Maglev
4+
module Sections
5+
class RenameCommand < Rails::Command::Base
6+
desc 'rename OLD_TYPE NEW_TYPE', 'Rename a section type across the site and its pages'
7+
8+
def self.banner
9+
'bin/rails maglev:sections:rename OLD_TYPE NEW_TYPE'
10+
end
11+
12+
def perform(old_type, new_type)
13+
require File.expand_path('config/environment', Rails.root)
14+
15+
site = fetch_site
16+
theme = fetch_theme
17+
18+
return if site.blank? || theme.blank?
19+
20+
rename_sections(site, theme, old_type, new_type)
21+
22+
say "Successfully renamed all '#{old_type}' sections to '#{new_type}' 🎉", :green
23+
end
24+
25+
private
26+
27+
def fetch_site
28+
Maglev::Site.first.tap do |site|
29+
say("[Error] You don't seem to have an existing site. 🤔", :red) unless site
30+
end
31+
end
32+
33+
def fetch_theme
34+
Maglev.local_themes&.first.tap do |theme|
35+
say('[Error] No theme found. 🤔', :red) unless theme
36+
end
37+
end
38+
39+
def rename_sections(site, theme, old_type, new_type)
40+
Maglev::RenameSectionType.call(
41+
site: site,
42+
theme: theme,
43+
old_type: old_type,
44+
new_type: new_type
45+
)
46+
end
47+
end
48+
end
49+
end

lib/maglev.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
require_relative 'maglev/i18n'
88
require_relative 'maglev/preview_constraint'
99
require_relative 'maglev/reserved_paths'
10+
require_relative 'maglev/commands_railtie'
1011

1112
require 'injectable'
1213
require 'jbuilder'

lib/maglev/commands_railtie.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# frozen_string_literal: true
2+
3+
module Maglev
4+
class CommandsRailtie < Rails::Railtie
5+
railtie_name :maglev_commands
6+
7+
initializer 'maglev.commands' do
8+
# Require all command files
9+
Dir[File.expand_path('../commands/**/*_command.rb', __dir__)].sort.each do |file|
10+
require file
11+
end
12+
end
13+
end
14+
end

lib/maglev/engine.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@ class Engine < ::Rails::Engine
3939
Rails.application.config.i18n.load_path += Dir["#{config.root}/config/locales/**/*.yml"]
4040
end
4141

42+
# initializer 'maglev.load_commands' do
43+
# if defined?(Rails::Command)
44+
# Dir.glob(File.join(__dir__, '../commands/**/*.rb')).each do |file|
45+
# puts "require #{file}"
46+
# require file
47+
# end
48+
# end
49+
# end
50+
4251
delegate :vite_ruby, to: :class
4352

4453
def self.vite_ruby

lib/tasks/maglev/base.rake

Lines changed: 0 additions & 51 deletions
This file was deleted.

lib/tasks/maglev/sections.rake

Lines changed: 0 additions & 77 deletions
This file was deleted.

0 commit comments

Comments
 (0)