A Simple Way to Decrease Complexity of Routes in Rails

Sometimes, when you are working with a large codebase, your routes.rb files grow really fast and become very dirty.

There are several things that make your routes file bigger:

  • Customization of devise;
  • Specific gems that mount additional routes (Active Admin, ActionCable);
  • Sidekiq + SidekiqWeb + Basic authentication;
  • Namespaces with APP or API.

All these points lead to one fact  - your file grows and it is getting hard to maintain it.

require 'sidekiq/web'
Sidekiq::Web.use Rack::Auth::Basic do |username, password|
# NOTE: https://github.com/mperham/sidekiq/wiki/Monitoring
ActiveSupport::SecurityUtils.secure_compare(::Digest::SHA256.hexdigest(username), ::Digest::SHA256.hexdigest(ENV.fetch('ADMIN_NAME'))) &
ActiveSupport::SecurityUtils.secure_compare(::Digest::SHA256.hexdigest(password), ::Digest::SHA256.hexdigest(ENV.fetch('ADMIN_PASSWORD')))
end if Rails.env.production?
Rails.application.routes.draw do
mount Sidekiq::Web, at: '/sidekiq'
root to: 'dashboards#show'
ActiveAdmin.routes(self)
devise_for :users, skip: [:sessions, :registrations, :passwords, :confirmations]
devise_scope :user do
# Sessions:
post 'auth/sign_in', to: 'auth/sessions#create', as: :user_session, defaults: { format: 'js' }
get 'auth/sign_out', to: 'auth/sessions#destroy', as: :destroy_user_session, defaults: { format: 'html' }
# Registrations:
post 'auth', to: 'auth/registrations#create', as: :user_registration, defaults: { format: 'js' }
# Passwords
post 'auth/password', to: 'auth/passwords#create', defaults: { format: 'js' }
get 'auth/password', to: 'auth/passwords#edit', as: :edit_user_password, defaults: { format: 'html' }
put 'auth/password', to: 'auth/passwords#update', as: :user_password, defaults: { format: 'html' }
# Confirmations
get 'auth/confirmation', to: 'auth/confirmations#show', as: :user_confirmation, defaults: { format: 'html' }
post 'auth/confirmation', to: 'auth/confirmations#create', defaults: { format: 'html' }
end
# API
# Namespaces..
# Etc...
end
view raw routes.rb hosted with ❤ by GitHub

However, do you need all this staff in regular work, when you add some controller with another resource? My answer is No!

config.paths['config/routes.rb'] = Dir[Rails.root.join('config/routes/*.rb')]
view raw application.rb hosted with ❤ by GitHub

With that line of code we can split our routes.rb file into small compact pieces and forget about huge and long file.

After routes splitting
Rails.application.routes.draw do
ActiveAdmin.routes(self)
devise_for :users, skip: [:sessions, :registrations, :passwords, :confirmations]
devise_scope :user do
# Sessions:
post 'auth/sign_in', to: 'auth/sessions#create', as: :user_session, defaults: { format: 'js' }
get 'auth/sign_out', to: 'auth/sessions#destroy', as: :destroy_user_session, defaults: { format: 'html' }
# Registrations:
post 'auth', to: 'auth/registrations#create', as: :user_registration, defaults: { format: 'js' }
# Passwords
post 'auth/password', to: 'auth/passwords#create', defaults: { format: 'js' }
get 'auth/password', to: 'auth/passwords#edit', as: :edit_user_password, defaults: { format: 'html' }
put 'auth/password', to: 'auth/passwords#update', as: :user_password, defaults: { format: 'html' }
# Confirmations
get 'auth/confirmation', to: 'auth/confirmations#show', as: :user_confirmation, defaults: { format: 'html' }
post 'auth/confirmation', to: 'auth/confirmations#create', defaults: { format: 'html' }
end
end
Rails.application.routes.draw do
root to: 'dashboards#show'
# API
# Namespaces..
# Etc...
end
require 'sidekiq/web'
Sidekiq::Web.use Rack::Auth::Basic do |username, password|
# NOTE: https://github.com/mperham/sidekiq/wiki/Monitoring
ActiveSupport::SecurityUtils.secure_compare(::Digest::SHA256.hexdigest(username), ::Digest::SHA256.hexdigest(ENV.fetch('ADMIN_NAME'))) &
ActiveSupport::SecurityUtils.secure_compare(::Digest::SHA256.hexdigest(password), ::Digest::SHA256.hexdigest(ENV.fetch('ADMIN_PASSWORD')))
end if Rails.env.production?
Rails.application.routes.draw do
mount Sidekiq::Web, at: '/sidekiq'
end

Solution is pretty simple and elegant. At the same time, it makes life easier, especially in rails development :)

Don't want to miss anything?

Subscribe and get stories like these right into your inbox.

Keep reading

Contact us

Let's explore how our expertise can help you achieve your goals! Drop us a line, and we'll get back to you shortly.