In this story, I want to show you how to . bring your Ruby on Rails development to the next level Like often in programming, all it takes is . automating repetitive stuff One in the Ruby on Rails development is starting new projects. task that begs for automation If you care about: from day one Code quality, Easy maintenance, Good performance, Code that's pleasant to work with, then - let's see why. you've come to the right place All this stuff we need... Starting new Ruby on Rails project is like: No, seriously - so much stuff to do: Install the latest stable Ruby version - check. Install the latest stable Ruby on Rails version - check. Install the latest stable database version: MySQL, PostgreSQL, you name it - check. Prepare : Rails credentials, Dotenv, Figaro - check. strict separation of config from code Create a new git remote repository: , , Bitbucket - check. Github Gitlab Install the latest stable Redis version - check. in development to - check Configure ActionCable to use Redis keep the gap between production and development small Async code execution, like Sidekiq - check. Create a new project on the monitoring cloud: , , Honeybadger, Bugsnag, Airbrake, Skylight - check. Rollbar Sentry - check. Prepare a cloud storage bucket: Microsoft Azure Blob Storage, Google Cloud Storage, Amazon S3 - check. Prepare SMTP or email sending API settings: Amazon SES, Gmail, Mailgun, Mandrill, SendGrid - check. Send emails in development: MailCatcher, LetterOpener , and try not forgetting any of the following: DatabaseCleaner, FactoryBot, TimeCop, Rspec Sidekiq Testing, VCR - check. Prepare RSpec configuration for testing Oh yeah, code coverage with SimpleCov and Coveralls - yeah, I forgot when talking about RSpec - check. , Fasterer, Brakeman, Dawnscanner, Bundle Audit, Rails Best Practices - check. Keep the bar up: RuboCop, RuboCop Performance, RuboCop RSpec, RuboCop Rails Set up this whole standardized-code in the Continuous Integration - Github Actions, , Bamboo, , Travis - check. GitlabCI CircleCI Damn, that's a handful. Now, try for example to not , and take a crap in the middle of your code base - happily. keep the bar up from the first commit someone will Not to mention sending emails in development to some random folks, and doing it synchronously, cause "ah, , yeah I heard". ActiveWork Or, let's skip logging errors and leave the default logger only. Then enroll the MVP blindfolded. And.. have you noticed? about: Not a word yet Deployment, not to mention a Continuous Delivery and , and environment. staging preprod whatnot Front-end setup, that nowadays, with rich JavaScript doubles some of those points: like linting, separate tests. Don't get me wrong, , it's still my favorite - so much stuff out of the box. I love Ruby on Rails Just all this configuration is what, like two, three workdays? Disclaimer: it's minutes with Hix on Rails Application Template . Default Ruby on Rails options Before we get to the template, one cool thing worth mentioning with Rails are the default options. new When using , we are able to tell Rails to use certain parts of the framework. rails new do / do not Usage: rails new APP_PATH [options] Options: [--skip-namespace], [--no-skip-namespace] # Skip namespace (affects only isolated applications) -r, [--ruby=PATH] # Path to the Ruby binary of your choice # Default: /usr/share/rvm/rubies/ruby-2.6.1/bin/ruby -m, [--template=TEMPLATE] # Path to some application template (can be a filesystem path or URL) -d, [--database=DATABASE] # Preconfigure for selected database (options: mysql/postgresql/sqlite3/oracle/frontbase/ibm_db/sqlserver/jdbcmysql/jdbcsqlite3/jdbcpostgresql/jdbc) # Default: sqlite3 [--skip-gemfile], [--no-skip-gemfile] # Don't create a Gemfile -G, [--skip-git], [--no-skip-git] # Skip .gitignore file [--skip-keeps], [--no-skip-keeps] # Skip source control .keep files -M, [--skip-action-mailer], [--no-skip-action-mailer] # Skip Action Mailer files [--skip-action-mailbox], [--no-skip-action-mailbox] # Skip Action Mailbox gem [--skip-action-text], [--no-skip-action-text] # Skip Action Text gem -O, [--skip-active-record], [--no-skip-active-record] # Skip Active Record files [--skip-active-storage], [--no-skip-active-storage] # Skip Active Storage files -P, [--skip-puma], [--no-skip-puma] # Skip Puma related files -C, [--skip-action-cable], [--no-skip-action-cable] # Skip Action Cable files -S, [--skip-sprockets], [--no-skip-sprockets] # Skip Sprockets files [--skip-spring], [--no-skip-spring] # Don't install Spring application preloader [--skip-listen], [--no-skip-listen] # Don't generate configuration that depends on the listen gem -J, [--skip-javascript], [--no-skip-javascript] # Skip JavaScript files [--skip-turbolinks], [--no-skip-turbolinks] # Skip turbolinks gem -T, [--skip-test], [--no-skip-test] # Skip test files [--skip-system-test], [--no-skip-system-test] # Skip system test files [--skip-bootsnap], [--no-skip-bootsnap] # Skip bootsnap gem [--dev], [--no-dev] # Setup the application with Gemfile pointing to your Rails checkout [--edge], [--no-edge] # Setup the application with Gemfile pointing to Rails repository [--rc=RC] # Path to file containing extra configuration options for rails command [--no-rc], [--no-no-rc] # Skip loading of extra configuration options from .railsrc file [--api], [--no-api] # Preconfigure smaller stack for API only apps -B, [--skip-bundle], [--no-skip-bundle] # Don't run bundle install --webpacker, [--webpack=WEBPACK] # Preconfigure Webpack with a particular framework (options: react, vue, angular, elm, stimulus) [--skip-webpack-install], [--no-skip-webpack-install] # Don't run Webpack install Runtime options: -f, [--force] # Overwrite files that already exist -p, [--pretend], [--no-pretend] # Run but do not make any changes -q, [--quiet], [--no-quiet] # Suppress status output -s, [--skip], [--no-skip] # Skip files that already exist Rails options: -h, [--help], [--no-help] # Show this help message and quit -v, [--version], [--no-version] # Show Rails version number and quit Description: The 'rails new' command creates a new Rails application with a default directory structure and configuration at the path you specify. You can specify extra command-line arguments to be used every time 'rails new' runs in the .railsrc configuration file in your home directory. Note that the arguments specified in the .railsrc file don't affect the defaults values shown above in this help message. Example: rails new ~/Code/Ruby/weblog This generates a skeletal Rails installation in ~/Code/Ruby/weblog. $ rails new -- help And there's a handful to remember, as you can see. If you already built some Ruby on Rails apps, , or you're in the PostgreSQL or MySQL camp. you probably have a list of options you don't like If that's the case, you can define the magic file in your home directory, with initialization flags that you always use. ~/.railsrc touch ~/.railsrc vi ~/.railsrc For example, my looks like this: --database=postgresql --skip-spring --skip-test --webpack=react --template=https://hixonrails.com/template.rb Here, I tell every command on my system: rails new go Postgres! nope Spring nope MiniTest, I want RSpec go React! And the last thing: use . the Rails Application Template Say what? Ruby on Rails - Application Template are simple files with a Domain Specific Language. Ruby on Rails Application Templates You can add stuff to the brand new Ruby on Rails project - initializers, gems, rake tasks - you name it. rails new project --template /path/to/template.rb rails new project --template https://example.com/template.rb Simple, yet so powerful. The template code runs after the initial Rails configuration, and before the bundle installs. That means that you can: Override every file generated Add any gems you want Create new files Create database tables On top of that, you can . run any shell command For example: create a remote git repository via Github or Gitlab API. Afterwards add the git remote to the project. So cool. Template DSL Let's now take a quick look what we can do using Rails App Template specific language: Install and group gems by environments with and gem gem_group Declare gem sources with method add_source Write to declared files environment Write , , , and any with HEREDOC lib initializer vendor rakefile file Gather user input with , and methods and run other stuff conditionally ask yes? no? Launch rails for scaffold, migration, model, controller, whatever generator Execute commands git Explicitly run selected code installs. after_bundle Execute shell commands with the method. run That's a lot of useful stuff right here, so let's see it in action. Rails Application Template with static code analysis Let's give some of the methods a spin by setting up a really simple Rails Application Template, that: Installs and uses RuboCop by default Installs and requires RuboCop Performance extension Installs and requires RuboCop Rails extension So here's how to do that. First, let's create our file and open it with a text editor. template.rb vi ~/ws/articles/hackernoon/template.rb Next, let's install all the RuboCop gems in the and environments: development test gem_group , gem gem gem # frozen_string_literal: true :development :test do 'rubocop' 'rubocop-performance' 'rubocop-rails' end Now that we have those in place, let's tell Rails to add the file, so we can loose up those restrictions a little. .rubocop.yml Add the following code. file , '.rubocop.yml' <<-CODE require: - rubocop-rails - rubocop-performance AllCops: Exclude: - node_modules/**/* - db/** - db/migrate/** - bin/** - vendor/**/* Layout/LineLength: Max: 120 Metrics/BlockLength: Exclude: - config/**/* Style/Documentation: Enabled: false CODE That's going to write our RuboCop configuration file in the root of the new Ruby on Rails application initialized with the template. The last thing is to clean up the default Rails code with RuboCop after the project initializes, and when all RuboCop gems are available. Hint: after bundle installs. Add the following code: after_bundle run do 'bundle exec rubocop --auto-correct' end At the point of writing this article, with Rails 6.0.2.1, it fixed 75 of 78 offenses automatically, so I needed to do some work manually. Inspecting 31 files .............C...............CC Offenses: config/environments/development.rb:19:6: C: Rails/FilePath: Please use Rails.root.join('path/to') instead. if Rails.root.join('tmp', 'caching-dev.txt').exist? ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ test/channels/application_cable/connection_test.rb:5:7: C: Style/ClassAndModuleChildren: Use nested module/class definitions instead of compact style. class ApplicationCable::ConnectionTest < ActionCable::Connection::TestCase ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ test/test_helper.rb:7:7: C: Style/ClassAndModuleChildren: Use nested module/class definitions instead of compact style. class ActiveSupport::TestCase ^^^^^^^^^^^^^^^^^^^^^^^ 31 files inspected, 3 offenses detected $ bundle rubocop exec Not so much though. Less hardcore option is using the , though it recently stopped to fix the missing and will require you to manually add it to each file - and it's worth it. --safe-auto-correct FrozenStringLiteral For those chickens who don't like code that writes code, there's a lightweight alternative that'll point to all the violated lines in the special file. .rubocop_todo.yml after_bundle run do 'bundle exec rubocop --auto-gen-config' end It does two things: Launches a dry run of RuboCop, that writes with violated files excluded from the next RuboCop runs .rubocop_todo.yml Edits file, telling it to mind the exclusions .rubocop.yml This way you can make sure that RuboCop's auto-correction does not beak anything, by fixing it all one by one, by hand. No YOLO though. Now all that's left to be done is launching the Rails installer pointing to our new template file: rails new project_name --template=/path/to/local/template.rb Sit back and relax - it all happens automatically. Cool, huh? When to use Rails Application Template Everytime, really, cause why the hell not. But for some it is a goldmine: : let's say one project a week, doing all this stuff - I can only imagine that there's this one go-to guy, probably mid to senior level, that sets the stuff up, for one to three working days. Software Houses I remember my co-worker telling me a story about someone, who worked at a Software House and once per two weeks he was literally installing the same gems in a next new project all over again. There's even a that I know for a fact that uses their own application template, cause they open-sourced it. company Next, : if you happen to take on some work and code in Ruby on Rails, it's a great way to speed up the process. Freelancers Last, , or any , even . Corporations micro services freaks smaller companies I'd say that if you: Maintain around five Ruby on Rails projects already, Keep certain coding standards in your development team, before starting the next project. it's time to extract the commonly used stuff to the template If you like to compartmentalize stuff into small Ruby on Rails pieces, Application Templates are the way to go. What's the problem with Rails Application Template? Maintenance. You won't use it that often. Rails itself, same as a lot of gems that you happily use, evolve. You can lock the gems versions, but it's not the best idea - it's better to use the latest stable versions available, for a security reasons if not for anything else. Worry not though, cause those are exact reasons why , so you can . Hix on Rails decided to take care of all the up-to-date stuff focus on what really matters for you Just after completing a short wizard in the CLI. Painlessly, simple as that. get to the point and implement business logic Conclusion So, how do you like the concept of automating Ruby on Rails projects initialization? Do you have in mind anything that would be so cool to go into the Rails Application Template? Maybe you already use templates yourself, and I've missed something? I'd love your feedback - please, tell me in comments!