Skip to content

Commit

Permalink
feat: create the setting type generator (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
did committed Oct 17, 2024
1 parent 7f6fe61 commit a7ef19b
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 13 deletions.
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ GEM
factory_bot_rails (6.2.0)
factory_bot (~> 6.2.0)
railties (>= 5.0.0)
ffi (1.17.0-arm64-darwin)
ffi (1.17.0-x86_64-darwin)
ffi (1.17.0-x86_64-linux-gnu)
generator_spec (0.10.0)
Expand Down Expand Up @@ -161,6 +162,8 @@ GEM
net-smtp (0.5.0)
net-protocol
nio4r (2.7.3)
nokogiri (1.16.6-arm64-darwin)
racc (~> 1.4)
nokogiri (1.16.6-x86_64-darwin)
racc (~> 1.4)
nokogiri (1.16.6-x86_64-linux)
Expand Down Expand Up @@ -315,6 +318,7 @@ GEM
zeitwerk (2.6.18)

PLATFORMS
arm64-darwin-23
x86_64-darwin-22
x86_64-linux

Expand Down
4 changes: 2 additions & 2 deletions lib/generators/maglev/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ def generate_blank_theme

def mount_engine
inject_into_file 'config/routes.rb', before: /^end/ do
<<-CODE
<<-RUBY
mount Maglev::Engine => '/maglev'
get '/sitemap', to: 'maglev/sitemap#index', defaults: { format: 'xml' }
get '(*path)', to: 'maglev/page_preview#index', defaults: { path: 'index' }, constraints: Maglev::PreviewConstraint.new
CODE
RUBY
end
end

Expand Down
43 changes: 43 additions & 0 deletions lib/generators/maglev/setting_type_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# frozen_string_literal: true

module Maglev
class SettingTypeGenerator < Rails::Generators::NamedBase
source_root File.expand_path('templates/setting_type', __dir__)

class_option :plugin, type: :string, default: nil

def plugin_name
(@plugin_name ||= options[:plugin]).tap do
raise 'Missing plugin option' if @plugin_name.blank?
end
end

def component_name
@component_name ||= table_name.dasherize
end

def generate
directory 'packages'
end

def register_setting_type_in_ruby
inject_into_file "packages/#{options[:plugin]}/lib/#{options[:plugin]}/engine.rb", before: / end\n^end/ do
<<-RUBY
config.to_prepare do
Maglev.register_setting_type(id: :#{table_name})
end
RUBY
end
end

def register_setting_type_in_javascript
raise 'TODO'
end

private

def pluralize_table_names?
false
end
end
end
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
// import { registerInput } from '@/misc/dynamic-inputs'

export default function() {
console.log('Hello from the <%= human_name %> plugin 👋 v0.0.1')

// registerInput('text', MyOwnTextInputComponent)
console.log('👋 Hello from the <%= human_name %> plugin v0.0.1')
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ module <%= class_name %>
class Engine < ::Rails::Engine
isolate_namespace <%= class_name %>

Maglev.register_plugin(
id: '<%= name %>',
name: '<%= human_name %>',
version: <%= class_name %>::VERSION,
root_path: root
)
initializer 'maglev.plugins.<%= table_name %>' do
Maglev.register_plugin(
id: '<%= name %>',
name: '<%= human_name %>',
version: <%= class_name %>::VERSION,
root_path: root
)
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

module Maglev
module Content
class <%= class_name %> < Maglev::Content::Base
# TODO: write methods here that will be exposed to the section template
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<template>
<div class="space-y-1 flex flex-col">
<label
class="block font-semibold text-gray-800"
:for="name"
>
<span>{{ label }}</span>
</label>

<!-- TODO: your input here -->
</div>
</template>

<script>
import FocusedInputMixin from '@/mixins/focused-input'

export default {
name: 'UIKit<%= class_name %>Input',
mixins: [FocusedInputMixin],
props: {
label: { type: String, default: 'Label' },
name: { type: String, default: 'text' },
value: { type: String }
},
methods: {
focus() {
// TODO: called when the editor clicks on the highlighted content in the preview window
},
}
}
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

# rubocop:disable Style/ClassAndModuleChildren
class Maglev:SettingTypes::<%= class_name %> < Maglev::SettingTypes::Base
# def cast_value(value)
# value
# end

# # rubocop:disable Lint/UnusedMethodArgument
# def default_for(label:, default:)
# default
# end
# # rubocop:enable Lint/UnusedMethodArgument

# def content_class
# @content_class ||= "Maglev::Content::#{self.class.name.demodulize}".constantize
# end
end
# rubocop:enable Style/ClassAndModuleChildren
51 changes: 51 additions & 0 deletions spec/lib/generators/maglev/setting_type_generator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# frozen_string_literal: true

require 'rails_helper'
require 'generator_spec'
require 'generators/maglev/setting_type_generator'

destination_path = File.expand_path('../tmp', __dir__)

describe Maglev::SettingTypeGenerator, type: :generator do
destination destination_path

arguments %w[radio_button --plugin=maglev_dummy]

before(:all) do
prepare_destination
plugin_path = File.join(destination_path, 'packages', 'maglev_dummy')
FileUtils.mkdir_p(File.join(plugin_path, 'lib', 'maglev_dummy'))
File.open(File.join(plugin_path, 'lib', 'maglev_dummy', 'engine.rb'), 'w+') do |f|
f.write(
<<-RUBY
module MaglevDummy
class Engine < ::Rails::Engine
isolate_namespace MaglevDummy
end
end
RUBY
)
end
File.open(File.join(plugin_path, 'index.js'), 'w+') do |f|
f.write(
<<-JAVASCRIPT
export default function() {
console.log('👋 Hello from the <%= human_name %> plugin v0.0.1')
}
JAVASCRIPT
)
end
run_generator
end

it 'generates all the files required for a new custom setting type' do
assert_file 'packages/maglev_dummy/app/models/maglev/setting_types/radio_button.rb', /RadioButton < Maglev::SettingTypes::Base/
assert_file 'packages/maglev_dummy/app/components/maglev/content/radio_button.rb', /class RadioButton < Maglev::Content::Base/

assert_file 'packages/maglev_dummy/lib/maglev_dummy/engine.rb', /Maglev\.register_setting_type\(id: :radio_button\)/

assert_file 'packages/maglev_dummy/app/frontend/editor/components/kit/radio-button.vue', /name: 'UIKitRadioButtonInput'/

assert_file 'packages/maglev_dummy/index.js', /registerInput('radio_button', UIKitRadioButtonInput, (props, _options) => props)/
end
end

0 comments on commit a7ef19b

Please sign in to comment.