# config/initializers/routing_draw.rb
class ActionDispatch::Routing::Mapper
def draw(routes_name, sub_path=nil)
if sub_path.present?
instance_eval(File.read(Rails.root.join("config/routes/#{sub_path}/#{routes_name}.rb")))
else
instance_eval(File.read(Rails.root.join("config/routes/#{routes_name}.rb")))
end
end
end
# routes.rb
Rails.application.routes.draw do
draw :api
resources :users
root to: 'users#index'
end
# config/routes/api.rb
require 'api_constraints'
namespace :api, compress: false do
[:v1, :v2].map { |api| draw api, :api }
end
# config/routes/api/v1.rb
scope module: :v1, constraints: ApiConstraints.new(version: 1, default: false) do
resources :users
end