Skip to content

Instantly share code, notes, and snippets.

@dhh
Created April 25, 2012 18:44
Show Gist options
  • Save dhh/2492118 to your computer and use it in GitHub Desktop.
Save dhh/2492118 to your computer and use it in GitHub Desktop.
class ActionDispatch::Routing::Mapper
def draw(routes_name)
instance_eval(File.read(Rails.root.join("config/routes/#{routes_name}.rb")))
end
end
BCX::Application.routes.draw do
draw :api
draw :account
draw :session
draw :people_and_groups
draw :projects
draw :calendars
draw :legacy_slugs
draw :ensembles_and_buckets
draw :globals
draw :monitoring
draw :mail_attachments
draw :message_preview
draw :misc
root to: 'projects#index'
end
@joxxoxo
Copy link

joxxoxo commented Sep 8, 2015

@sharipov-ru, it's much better but I'd like to add a small improvement:
ActiveSupport::FileUpdateChecker.new([], 'config/routes' => 'rb') do ... end
This way it should find new files as well.

@iGEL
Copy link

iGEL commented Nov 12, 2015

Guys, it's very outdated. You now can just require other routes files without monkey patching, they just have to repeat the Rails.application.routes.draw do:
config/routes.rb:

Rails.application.routes.draw do
  load Rails.root.join("config/routes/dev.rb")
  #...
end

config/routes/dev.rb:

Rails.application.routes.draw do
  scope "dev" do
    # ...
  end
end

No dynamic reloading tho, probably @sharipov-ru's reloader still works.

@h0jeZvgoxFepBQ2C
Copy link

h0jeZvgoxFepBQ2C commented Dec 7, 2016

Anyone found a nice solution on how to seperate route files and still make them auto reloading during development?

Edit: Going now with a combination of the mentioned solutions...

# config/environments/development.rb
class RoutesReloader
  def initialize(app)
    @app = app

    @routes_reloader = ActiveSupport::FileUpdateChecker.new([], 'config/routes' => 'rb') do
      Rails.application.reload_routes!
    end
  end

  def call(env)
    @routes_reloader.execute_if_updated

    @app.call(env)
  end
end

Rails.application.routes.draw do
  ...
  config.middleware.use RoutesReloader
end
# config/routes.rb
Rails.application.routes.draw do
  routes = [:admin_routes, :admin_routes_old, :public_routes, :api_routes]
  routes.each{ |route_file| load Rails.root.join("config", "routes", "#{route_file}.rb") }
end

@JosueMerlos
Copy link

How can I use concern with this conven?
https://guides.rubyonrails.org/routing.html#routing-concerns

concern :commentable do
  resources :comments
end
resources :messages, concerns: :commentable

can I do this?

draw :api, concerns: :commentable

or there another way?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment