-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create the setting type generator (WIP)
- Loading branch information
Showing
9 changed files
with
167 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
6 changes: 1 addition & 5 deletions
6
lib/generators/maglev/templates/plugin/packages/%table_name%/index.js.tt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
...ates/setting_type/packages/%plugin_name%/app/components/maglev/content/%table_name%.rb.tt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
31 changes: 31 additions & 0 deletions
31
...ng_type/packages/%plugin_name%/app/frontend/editor/components/kit/%component_name%.vue.tt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
19 changes: 19 additions & 0 deletions
19
...es/setting_type/packages/%plugin_name%/app/models/maglev/setting_types/%table_name%.rb.tt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |