A Template for Starting New Projects on Ruby on Rails

On this tale, I wish to display you find out how to deliver your Ruby on Rails building to the following stage.

Like regularly in programming, all it takes is automating repetitive stuff.

One job that begs for automation within the Ruby on Rails building is beginning new tasks.

If from day one you care about:

  1. Code high quality,
  2. Simple upkeep,
  3. Just right functionality,
  4. Code that is delightful to paintings with,

then you could have come to the fitting position – let’s examine why.

All these things we want…

Starting new Ruby on Rails undertaking is like:

No, significantly – such a lot stuff to do:

  1. Set up the newest strong Ruby model – take a look at.
  2. Set up the newest strong Ruby on Rails model – take a look at.
  3. Set up the newest strong database model: MySQL, PostgreSQL, you identify it – take a look at.
  4. Get ready strict separation of config from code: Rails credentials, Dotenv, Figaro – take a look at.
  5. Create a brand new git faraway repository: Github, Gitlab, Bitbucket – take a look at.
  6. Set up the newest strong Redis model – take a look at.
  7. Configure ActionCable to make use of Redis in building to stay the distance between manufacturing and building small – take a look at
  8. Async code execution, like Sidekiq – take a look at.
  9. Create a brand new undertaking on the tracking cloud: Rollbar, Sentry, Honeybadger, Bugsnag, Airbrake, Skylight – take a look at.
  10. Get ready a cloud garage bucket: Microsoft Azure Blob Garage, Google Cloud Garage, Amazon S3 – take a look at.
  11. Get ready SMTP or e-mail sending API settings: Amazon SES, Gmail, Mailgun, Mandrill, SendGrid – take a look at.
  12. Ship emails in building: MailCatcher, LetterOpener – take a look at.
  13. Get ready RSpec configuration for checking out, and take a look at now not forgetting any of the next: DatabaseCleaner, FactoryBot, TimeCop, Rspec Sidekiq Trying out, VCR – take a look at.
  14. Oh yeah, code protection with SimpleCov and Coveralls – yeah, I forgot when speaking about RSpec – take a look at.
  15. Stay the bar up: RuboCop, RuboCop Efficiency, RuboCop RSpec, RuboCop Rails, Fasterer, Brakeman, Dawnscanner, Package Audit, Rails Perfect Practices – take a look at.
  16. Arrange this entire standardized-code-Third-reich within the Steady Integration – Github Movements, GitlabCI, Bamboo, CircleCI, Travis – take a look at.

Rattling, that is a handful.

Now, take a look at for instance not to stay the bar up from the primary devote, and any person will take a crap in the midst of your code base – thankfully.

To not point out sending emails in building to a few random other people, and doing it synchronously, reason “ah,


, yeah I heard”.

Or, let’s skip logging mistakes and depart the default logger handiest. Then sign up the MVP blindfolded.

And.. have you ever spotted? Now not a phrase but about:

  1. Deployment, to not point out a Steady Supply and






  2. Entrance-end setup, that this present day, with wealthy JavaScript doubles a few of the ones issues: like linting, separate checks.

Do not get me improper, I really like Ruby on Rails, it is nonetheless my favourite – such a lot stuff out of the field.

Simply all this configuration is what, like two, three workdays?

Default Ruby on Rails choices

Sooner than we get to the template, one cool factor value citing with Rails are the default



When the usage of

rails new

, we’re in a position to inform Rails to do / don’t use sure portions of the framework.

$ rails new --lend a hand 
Utilization: rails new APP_PATH [options] Choices: [--skip-namespace], [--no-skip-namespace] # Skip namespace (impacts handiest remoted programs) -r, [--ruby=PATH] # Trail to the Ruby binary of your selection # Default: /usr/proportion/rvm/rubies/ruby-2.6.1/bin/ruby -m, [--template=TEMPLATE] # Trail to a few application template (generally is a filesystem trail or URL) -d, [--database=DATABASE] # Preconfigure for decided on database (choices: mysql/postgresql/sqlite3/oracle/frontbase/ibm_db/sqlserver/jdbcmysql/jdbcsqlite3/jdbcpostgresql/jdbc) # Default: sqlite3 [--skip-gemfile], [--no-skip-gemfile] # Do not create a Gemfile -G, [--skip-git], [--no-skip-git] # Skip .gitignore record [--skip-keeps], [--no-skip-keeps] # Skip supply regulate .stay recordsdata -M, [--skip-action-mailer], [--no-skip-action-mailer] # Skip Motion Mailer recordsdata [--skip-action-mailbox], [--no-skip-action-mailbox] # Skip Motion Mailbox gem [--skip-action-text], [--no-skip-action-text] # Skip Motion Textual content gem -O, [--skip-active-record], [--no-skip-active-record] # Skip Lively File recordsdata [--skip-active-storage], [--no-skip-active-storage] # Skip Lively Garage recordsdata -P, [--skip-puma], [--no-skip-puma] # Skip Puma similar recordsdata -C, [--skip-action-cable], [--no-skip-action-cable] # Skip Motion Cable recordsdata -S, [--skip-sprockets], [--no-skip-sprockets] # Skip Sprockets recordsdata [--skip-spring], [--no-skip-spring] # Do not set up Spring application preloader [--skip-listen], [--no-skip-listen] # Do not generate configuration that is dependent on the concentrate gem -J, [--skip-javascript], [--no-skip-javascript] # Skip JavaScript recordsdata [--skip-turbolinks], [--no-skip-turbolinks] # Skip turbolinks gem -T, [--skip-test], [--no-skip-test] # Skip verify recordsdata [--skip-system-test], [--no-skip-system-test] # Skip gadget verify recordsdata [--skip-bootsnap], [--no-skip-bootsnap] # Skip bootsnap gem [--dev], [--no-dev] # Setup the application with Gemfile pointing in your Rails checkout [--edge], [--no-edge] # Setup the application with Gemfile pointing to Rails repository [--rc=RC] # Trail to record containing further configuration choices for rails command [--no-rc], [--no-no-rc] # Skip loading of additional configuration choices from .railsrc record [--api], [--no-api] # Preconfigure smaller stack for API handiest apps -B, [--skip-bundle], [--no-skip-bundle] # Do not run package set up --webpacker, [--webpack=WEBPACK] # Preconfigure Webpack with a specific framework (choices: react, vue, angular, elm, stimulus) [--skip-webpack-install], [--no-skip-webpack-install] # Do not run Webpack set up Runtime choices: -f, [--force] # Overwrite recordsdata that exist already -p, [--pretend], [--no-pretend] # Run however are not making any adjustments -q, [--quiet], [--no-quiet] # Suppress standing output -s, [--skip], [--no-skip] # Skip recordsdata that exist already Rails choices: -h, [--help], [--no-help] # Display this lend a hand message and give up -v, [--version], [--no-version] # Display Rails model quantity and give up Description: The 'rails new' command creates a brand new Rails application with a default listing construction and configuration on the trail you specify. You'll be able to specify further command-line arguments for use each and every time 'rails new' runs within the .railsrc configuration record in your house listing. Observe that the arguments specified within the .railsrc record do not have an effect on the defaults values proven above on this lend a hand message. Instance: rails new ~/Code/Ruby/blog This generates a skeletal Rails set up in ~/Code/Ruby/blog.

And there is a handful to keep in mind, as you’ll be able to see.

In the event you already constructed some Ruby on Rails apps, if in case you have a listing of choices you do not like, or you are within the PostgreSQL or MySQL camp.

If that is the case, you’ll be able to outline the magic ~/.railsrc record in your house listing, with initialization flags that you simply at all times use.

contact ~/.railsrc
vi ~/.railsrc

For instance, my seems like this:

Right here, I inform each and every

rails new

command on my gadget:

  1. pass Postgres!
  2. nope Spring
  3. nope MiniTest, I would like RSpec
  4. pass React!

Say what?

Ruby on Rails – Application Template

You’ll be able to upload stuff to the new Ruby on Rails undertaking – initializers, gemstones, rake duties – you identify it.

rails new undertaking --template /trail/to/template.rb
rails new undertaking --template https://instance.com/template.rb

Easy, but so robust.

The template code runs after the preliminary Rails configuration, and prior to the package installs.

That signifies that you’ll be able to:

  1. Override each and every record generated
  2. Upload any gemstones you need
  3. Create new recordsdata
  4. Create database tables

On best of that, you’ll be able to run any shell command.

For instance: create a faraway git repository by means of Github or Gitlab API.

Afterwards upload the git faraway to the undertaking.

So cool.

Template DSL

Let’s now take a snappy glance what we will do the usage of Rails App Template explicit language:

  1. Set up and crew gemstones through environments with


  2. Claim gem resources with


  3. Write to declared


  4. Write







    and any


    with HEREDOC

  5. Acquire consumer enter with





    strategies and run different stuff conditionally

  6. Release rails

    for scaffold, migration, fashion, controller, no matter

  7. Execute


  8. Explicitly run decided on code


  9. Execute shell instructions with the


That is numerous helpful stuff proper right here, so let’s examine it in motion.

Rails Application Template with static code research

Let’s give one of the most application template strategies a spin through putting in a truly easy Rails Application Template, that:

  1. Installs and makes use of RuboCop through default
  2. Installs and calls for RuboCop Efficiency extension
  3. Installs and calls for RuboCop Rails extension

So here is how to try this.

First, let’s create our template.rb record and open it with a textual content editor.

vi ~/ws/articles/hackernoon/template.rb
Subsequent, let’s set up all of the RuboCop gemstones within the





 # frozen_string_literal: true gem_group :building, :verify do gem 'rubocop' gem 'rubocop-performance' gem 'rubocop-rails' finish

Now that we have got the ones in position, let’s inform Rails so as to add the .rubocop.yml record, so we will unfastened up those Ruby nazi a bit.

Upload the next code.

record '.rubocop.yml', <<-CODE
require: - rubocop-rails - rubocop-performance AllCops: Exclude: - node_modules/**/* - db/** - db/migrate/** - bin/** - dealer/**/* Format/LineLength: Max: 120 Metrics/BlockLength: Exclude: - config/**/* Taste/Documentation: Enabled: false

That is going to jot down our RuboCop configuration record within the root of the brand new Ruby on Rails application initialized with the template.

The very last thing is to scrub up the default Rails code with RuboCop after the undertaking initializes, and when all RuboCop gemstones are to be had.

Trace: after package installs.

Upload the next code:

 after_bundle do run 'package exec rubocop --auto-correct' finish

On the level of writing this newsletter, with Rails, it fastened 75 of 78 offenses robotically, so I had to perform a little paintings manually.

$ package exec rubocop
Analyzing 31 recordsdata
.............C...............CC Offenses: config/environments/building.rb:19:6: C: Rails/FilePath: Please use Rails.root.sign up for('trail/to') as a substitute. if Rails.root.sign up for('tmp', 'caching-dev.txt').exist? ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
verify/channels/application_cable/connection_test.rb:5:7: C: Taste/ClassAndModuleChildren: Use nested module/magnificence definitions as a substitute of compact taste.
magnificence ApplicationCable::ConnectionTest < ActionCable::Connection::TestCase ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
verify/test_helper.rb:7:7: C: Taste/ClassAndModuleChildren: Use nested module/magnificence definitions as a substitute of compact taste.
magnificence ActiveSupport::TestCase ^^^^^^^^^^^^^^^^^^^^^^^ 31 recordsdata inspected, 3 offenses detected

Now not such a lot despite the fact that.

Much less hardcore possibility is the usage of the


, despite the fact that it not too long ago stopped to mend the lacking


and would require you to manually upload it to each and every record – and it is value it.

For the ones chickens who do not like code that writes code, there is a light-weight choice that’ll level to all of the violated traces within the particular .rubocop_todo.yml record.

 after_bundle do run 'package exec rubocop --auto-gen-config' finish

It does two issues:

  1. Launches a dry run of RuboCop, that writes .rubocop_todo.yml with violated recordsdata excluded from the following RuboCop runs
  2. Edits .rubocop.yml record, telling it to thoughts the exclusions

This manner you’ll be able to make certain that RuboCop’s auto-correction does now not beak the rest, through solving all of it one through one, through hand. No YOLO despite the fact that.

Now all that is left to be achieved is launching the Rails installer pointing to our new template record:

rails new project_name --template=/trail/to/native/template.rb

Sit down again and loosen up – all of it occurs robotically. Cool, huh?

When to make use of Rails Application Template

Everytime, truly, reason why the hell now not.

However for some this can be a goldmine:

Tool Properties: shall we say one undertaking per week, doing all these things – I will be able to handiest believe that there is this one go-to man, most definitely mid to senior stage, that units the stuff up, for one to three running days.

I consider my co-worker telling me a tale about any person, who labored at a Tool Area and as soon as consistent with two weeks he used to be actually putting in the similar gemstones in a subsequent new undertaking all over the place once more.

There is even a corporate that I do know for a undeniable fact that makes use of their very own application template, reason they open-sourced it.

Subsequent, Freelancers: for those who occur to take on some paintings and code in Ruby on Rails, it is an effective way to hurry up the method.

Closing, Companies, or any micro products and services freaks, even smaller firms.

I would say that for those who:

  1. Deal with round five Ruby on Rails tasks already,
  2. Stay sure coding requirements for your building group,

it is time to extract the repeatedly used stuff to the template prior to beginning the following undertaking.

In the event you love to compartmentalize stuff into small Ruby on Rails items, Application Templates are methods to pass.

What is the downside with Rails Application Template?

Upkeep. You will not use it that regularly.

Rails itself, similar as numerous gemstones that you simply thankfully use, evolve.

You’ll be able to lock the gemstones variations, however it isn’t the most efficient thought – it is higher to make use of the newest strong variations to be had, for a safety causes if now not for the rest.

Simply get to the purpose and put into effect trade common sense after finishing a brief wizard within the CLI. Painlessly, easy as that.


So, how do you prefer the concept that of automating Ruby on Rails tasks initialization?

Do you be mindful the rest that might be so cool to enter the Rails Application Template?

Possibly you already use templates your self, and I have ignored one thing?

I would love your comments – please, inform me in feedback!

(Visited 1 times, 1 visits today)

Leave a Reply