diff --git a/README.md b/README.md index 47879aa..e7d8467 100644 --- a/README.md +++ b/README.md @@ -85,6 +85,17 @@ generally contain namespaced classes. These are exempted from `automatic_namespa * javascript * views +Additional directories can be excluded by adding them to the `automatic_pack_namespace_exclusions` key in your +`package.yml` file. This is useful if you require files in your pack that sit outside of your packs namespace: + +```yml +metadata: + automatic_pack_namespace: true + automatic_pack_namespace_exclusions: + - app/policies # Exclude pundit policies + - app/admin # Exclude active admin definition files +``` + If your package / namespace name requires ActiveSupport inflections, you will probably need to tell `automatic_namespaces` what the correct namespace name should be in that package: diff --git a/lib/automatic_namespaces/autoloader.rb b/lib/automatic_namespaces/autoloader.rb index 34f90a4..eb51714 100644 --- a/lib/automatic_namespaces/autoloader.rb +++ b/lib/automatic_namespaces/autoloader.rb @@ -1,11 +1,13 @@ require 'yaml' class AutomaticNamespaces::Autoloader + DEFAULT_EXCLUDED_DIRS = %w[/app/helpers /app/inputs /app/javascript /app/views].freeze + PACKAGE_EXCLUDED_DIRS_KEY = "automatic_pack_namespace_exclusions".freeze def enable_automatic_namespaces namespaced_packages.each do |pack, metadata| package_namespace = define_namespace(pack, metadata) - pack_directories(pack.path).each do |pack_dir| + pack_directories(pack.path, metadata).each do |pack_dir| set_namespace_for(pack_dir, package_namespace) end end @@ -20,16 +22,16 @@ def set_namespace_for(pack_dir, package_namespace) Rails.application.config.watchable_dirs[pack_dir] = [:rb] end - def pack_directories(pack_root_dir) - Dir.glob("#{pack_root_dir}/**/app/*").reject { |dir| non_namspaced_directory(dir) } + def pack_directories(pack_root_dir, metadata) + Dir.glob("#{pack_root_dir}/**/app/*").select { |dir| namespaced_directory?(dir, metadata) } end - def non_namspaced_directory(dir) - dir.include?('/app/assets') || - dir.include?('/app/helpers') || # Rails assumes helpers are global, not namespaced - dir.include?('/app/inputs') || # Not sure how to namespace form inputs - dir.include?('/app/javascript') || - dir.include?('/app/views') + def namespaced_directory?(dir, metadata) + excluded_directories(metadata).none? { |excluded_dir| dir.include?(excluded_dir) } + end + + def excluded_directories(metadata) + DEFAULT_EXCLUDED_DIRS + metadata.fetch(PACKAGE_EXCLUDED_DIRS_KEY, []) end def define_namespace(pack, metadata) @@ -59,7 +61,3 @@ def package_metadata(pack) package_description["metadata"] end end - - - - diff --git a/spec/automatic_namespaces_spec.rb b/spec/automatic_namespaces_spec.rb index 8c73890..7afc1ef 100644 --- a/spec/automatic_namespaces_spec.rb +++ b/spec/automatic_namespaces_spec.rb @@ -39,4 +39,14 @@ it "doesn't crash when a package yml is corrupted" do expect(defined?(Shirts::Tshirt)).to eq("constant") end + + it 'excludes helpers from automatic namespacing' do + expect(defined?(ShirtHelper)).to eq("constant") + end + + context 'when automatic_namespaces_exclusions is provided' do + it 'does not add the namespace to files in those directories' do + expect(defined?(Sneaker)).to eq("constant") + end + end end diff --git a/spec/fixtures/rails-7.0/packs/shirts/app/helpers/shirt_helper.rb b/spec/fixtures/rails-7.0/packs/shirts/app/helpers/shirt_helper.rb new file mode 100644 index 0000000..05b3caf --- /dev/null +++ b/spec/fixtures/rails-7.0/packs/shirts/app/helpers/shirt_helper.rb @@ -0,0 +1 @@ +module ShirtHelper; end diff --git a/spec/fixtures/rails-7.0/packs/shoes_ui/app/excluded/sneaker.rb b/spec/fixtures/rails-7.0/packs/shoes_ui/app/excluded/sneaker.rb new file mode 100644 index 0000000..8b04100 --- /dev/null +++ b/spec/fixtures/rails-7.0/packs/shoes_ui/app/excluded/sneaker.rb @@ -0,0 +1 @@ +class Sneaker; end diff --git a/spec/fixtures/rails-7.0/packs/shoes_ui/package.yml b/spec/fixtures/rails-7.0/packs/shoes_ui/package.yml index 2a409c6..c0c1339 100644 --- a/spec/fixtures/rails-7.0/packs/shoes_ui/package.yml +++ b/spec/fixtures/rails-7.0/packs/shoes_ui/package.yml @@ -1,3 +1,5 @@ metadata: automatic_pack_namespace: true namespace_override: ShoesUI + automatic_pack_namespace_exclusions: + - app/excluded