diff --git a/Gemfile.lock b/Gemfile.lock index b73babd..f5efb32 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -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) @@ -315,6 +318,7 @@ GEM zeitwerk (2.6.18) PLATFORMS + arm64-darwin-23 x86_64-darwin-22 x86_64-linux diff --git a/lib/generators/maglev/install_generator.rb b/lib/generators/maglev/install_generator.rb index a581003..00a60c6 100644 --- a/lib/generators/maglev/install_generator.rb +++ b/lib/generators/maglev/install_generator.rb @@ -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 diff --git a/lib/generators/maglev/setting_type_generator.rb b/lib/generators/maglev/setting_type_generator.rb new file mode 100644 index 0000000..dadddd4 --- /dev/null +++ b/lib/generators/maglev/setting_type_generator.rb @@ -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 diff --git a/lib/generators/maglev/templates/plugin/packages/%table_name%/index.js.tt b/lib/generators/maglev/templates/plugin/packages/%table_name%/index.js.tt index 9ef6a53..31d7444 100644 --- a/lib/generators/maglev/templates/plugin/packages/%table_name%/index.js.tt +++ b/lib/generators/maglev/templates/plugin/packages/%table_name%/index.js.tt @@ -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') } \ No newline at end of file diff --git a/lib/generators/maglev/templates/plugin/packages/%table_name%/lib/%table_name%/engine.rb.tt b/lib/generators/maglev/templates/plugin/packages/%table_name%/lib/%table_name%/engine.rb.tt index 30f68dd..0a10587 100644 --- a/lib/generators/maglev/templates/plugin/packages/%table_name%/lib/%table_name%/engine.rb.tt +++ b/lib/generators/maglev/templates/plugin/packages/%table_name%/lib/%table_name%/engine.rb.tt @@ -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 \ No newline at end of file diff --git a/lib/generators/maglev/templates/setting_type/packages/%plugin_name%/app/components/maglev/content/%table_name%.rb.tt b/lib/generators/maglev/templates/setting_type/packages/%plugin_name%/app/components/maglev/content/%table_name%.rb.tt new file mode 100644 index 0000000..f5aee40 --- /dev/null +++ b/lib/generators/maglev/templates/setting_type/packages/%plugin_name%/app/components/maglev/content/%table_name%.rb.tt @@ -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 \ No newline at end of file diff --git a/lib/generators/maglev/templates/setting_type/packages/%plugin_name%/app/frontend/editor/components/kit/%component_name%.vue.tt b/lib/generators/maglev/templates/setting_type/packages/%plugin_name%/app/frontend/editor/components/kit/%component_name%.vue.tt new file mode 100644 index 0000000..d1075bb --- /dev/null +++ b/lib/generators/maglev/templates/setting_type/packages/%plugin_name%/app/frontend/editor/components/kit/%component_name%.vue.tt @@ -0,0 +1,31 @@ + + + diff --git a/lib/generators/maglev/templates/setting_type/packages/%plugin_name%/app/models/maglev/setting_types/%table_name%.rb.tt b/lib/generators/maglev/templates/setting_type/packages/%plugin_name%/app/models/maglev/setting_types/%table_name%.rb.tt new file mode 100644 index 0000000..0342779 --- /dev/null +++ b/lib/generators/maglev/templates/setting_type/packages/%plugin_name%/app/models/maglev/setting_types/%table_name%.rb.tt @@ -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 diff --git a/spec/lib/generators/maglev/setting_type_generator_spec.rb b/spec/lib/generators/maglev/setting_type_generator_spec.rb new file mode 100644 index 0000000..a4c41f0 --- /dev/null +++ b/spec/lib/generators/maglev/setting_type_generator_spec.rb @@ -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