diff --git a/README.md b/README.md index eea5b79..896864c 100644 --- a/README.md +++ b/README.md @@ -60,14 +60,18 @@ From there, you can create a `./packs` folder and structure it using the convent If you wish to use a different directory name, eg `components` instead of `packs`, you can customize this by configuring `packs.yml`. See [`packs`](https://github.com/rubyatscale/packs) for more information. ### Splitting routes -`packs-rails` allows you to split your application routes for every pack. You just have to create a file describing your routes and then `draw` them in your root `config/routes.rb` file. +`packs-rails` allows you to split your application routes for every pack. You just have to create a file describing your routes and then `draw` them in your root `config/routes.rb` file (NOTE: the `draw` function is only in Rails 6.1+). ```ruby # packs/my_domain/config/routes/my_domain.rb -resources :my_resource +Rails.application.routes.draw do + resources :my_resource +end # config/routes.rb -draw(:my_domain) +Rails.application.routes.draw do + draw(:my_domain) +end ``` ### Making your Package an Engine diff --git a/lib/packs/rails/integrations/rails.rb b/lib/packs/rails/integrations/rails.rb index 607285c..505b487 100644 --- a/lib/packs/rails/integrations/rails.rb +++ b/lib/packs/rails/integrations/rails.rb @@ -7,6 +7,8 @@ module Packs module Rails module Integrations class Rails + CONFIG_ROUTES_PATH = "config/routes".freeze + def initialize(app) @app = app @@ -27,6 +29,10 @@ def create_engines def inject_paths Packs.all.reject(&:is_gem?).each do |pack| Packs::Rails.config.paths.each do |path| + # Prior to Rails 6.1, the "config/routes" app path is nil and was not added until drawable routes feature was implemented + # https://github.com/rails/rails/pull/37892/files#diff-a785e41df3f87063a8fcffcac726856a25d8eae6d1f9bca2b36989fe88613f8eR62 + next if pre_rails_6_1? && path == CONFIG_ROUTES_PATH + @app.paths[path] << pack.relative_path.join(path) end end @@ -34,6 +40,11 @@ def inject_paths private + def pre_rails_6_1? + return @_pre_rails_6_1 if defined?(@_pre_rails_6_1) + @_pre_rails_6_1 = ::Rails.gem_version < Gem::Version.new("6.1") + end + def create_namespace(name) namespace = ActiveSupport::Inflector.camelize(name) namespace.split("::").reduce(Object) do |base, mod|