From fc6eccd822ff8f12c40ec9cc0bf30545d3815629 Mon Sep 17 00:00:00 2001 From: Tim Date: Tue, 31 Oct 2023 23:26:02 +0100 Subject: [PATCH] Added possibility to set custom engine name (#65) --- README.md | 12 ++++++++++++ lib/packs/rails/integrations/rails.rb | 2 +- .../jeans/app/models/pants/jeans/bootcut.rb | 6 ++++++ .../rails-7.0/packs/pants/jeans/package.yml | 3 +++ spec/packs-rails_spec.rb | 16 ++++++++++++++++ 5 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 spec/fixtures/rails-7.0/packs/pants/jeans/app/models/pants/jeans/bootcut.rb create mode 100644 spec/fixtures/rails-7.0/packs/pants/jeans/package.yml diff --git a/README.md b/README.md index 38762f4..c5a955f 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,18 @@ metadata: engine: true ``` +Add `engine_name: ` to your `package.yml` to use a specific, maybe namespaced, engine name instead of the last package folder name. +```yml +# packs/my_pack/package.yml +enforce_dependencies: true +enforce_privacy: true +metadata: + engine: true + engine_name: namespaced/my_pack +``` + +The engine is created as `Namespaced::MyPack::Engine` instead of `MyPack::Engine`. + ## Ecosystem and Integrations ### RSpec Integration diff --git a/lib/packs/rails/integrations/rails.rb b/lib/packs/rails/integrations/rails.rb index 607285c..5f0c639 100644 --- a/lib/packs/rails/integrations/rails.rb +++ b/lib/packs/rails/integrations/rails.rb @@ -46,7 +46,7 @@ def create_namespace(name) end def create_engine(pack) - name = pack.last_name + name = pack.metadata.fetch("engine_name", pack.last_name) namespace = create_namespace(name) stim = Stim.new(pack, namespace) namespace.const_set("Engine", Class.new(::Rails::Engine)).include(stim) diff --git a/spec/fixtures/rails-7.0/packs/pants/jeans/app/models/pants/jeans/bootcut.rb b/spec/fixtures/rails-7.0/packs/pants/jeans/app/models/pants/jeans/bootcut.rb new file mode 100644 index 0000000..603192d --- /dev/null +++ b/spec/fixtures/rails-7.0/packs/pants/jeans/app/models/pants/jeans/bootcut.rb @@ -0,0 +1,6 @@ +module Pants + module Jeans + class Bootcut + end + end +end diff --git a/spec/fixtures/rails-7.0/packs/pants/jeans/package.yml b/spec/fixtures/rails-7.0/packs/pants/jeans/package.yml new file mode 100644 index 0000000..83e79ff --- /dev/null +++ b/spec/fixtures/rails-7.0/packs/pants/jeans/package.yml @@ -0,0 +1,3 @@ +metadata: + engine: true + engine_name: pants/jeans diff --git a/spec/packs-rails_spec.rb b/spec/packs-rails_spec.rb index e874fd9..45a5b21 100644 --- a/spec/packs-rails_spec.rb +++ b/spec/packs-rails_spec.rb @@ -39,6 +39,22 @@ end end + context 'custom engine name' do + it "autoloads classes in autoload paths" do + expect(defined?(Pants::Jeans::Bootcut)).to eq("constant") + end + + it "adds pack paths to the application" do + Packs::Rails.config.paths.each do |path| + expect(Rails.application.paths[path].paths).to include(rails_dir.join('packs', "pants", "jeans", path)) + end + end + + it "creates engines namespace for engine packs" do + expect(defined?(Pants::Jeans::Engine)).to eq("constant") + end + end + context 'alternate roots' do it "autoloads classes in autoload paths" do expect(defined?(Belts::Brown)).to eq("constant")