
Speed Up Tests with Build Stubbed: Best Practices
Rspec is a great tool for the Ruby community, but tests can become slow in large projects. When test suites take over 30 minutes, something has gone wrong.

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:
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 |
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')] |
With that line of code we can split our routes.rb file into small compact pieces and forget about huge and long file.
| 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 :)
Subscribe and get stories like these right into your inbox.

Rspec is a great tool for the Ruby community, but tests can become slow in large projects. When test suites take over 30 minutes, something has gone wrong.

Carrierwave is a popular Rails gem for image uploads. I recently needed to upload two image types differing only by target directory. Sounds simple, right?

This article will introduce you the Ruby tool that we have built at Anadea to automate some repetitive processes.
Let's explore how our expertise can help you achieve your goals! Drop us a line, and we'll get back to you shortly.