rake setup: Configuring the environment for a Ruby On Rails project

  • Tutorial
Good day!

While re-reading the Signals Vs Noise blog, I came across an interesting article

. It recommended creating a rake task that would fully prepare your application for development after cloning from the repository.

> `rake setup`

> All our apps has a rake setup task that'll run bundler,
> create the databases, import seeds, and install any auxiliary
> software (little these days) or do any other setup. So when you git
> clone a new app, you know that “rake setup” will take care of you.

I’ll talk about how to do a similar task in a Ruby On Rails application.

Let's go to the application folder and run
rake -T

This command will list all the tasks available for rake.

rake about                            # List versions of all Rails frameworks and the environment
rake assets:clean                     # Remove old compiled assets
rake assets:clobber                   # Remove compiled assets
rake assets:environment               # Load asset compile environment
rake assets:precompile                # Compile all the assets named in config.assets.precompile
rake db:create                        # Create the database from DATABASE_URL or config/database.yml for the current Rails.env (use db:create:all to create all dbs in the config)
rake db:drop                          # Drops the database using DATABASE_URL or the current Rails.env (use db:drop:all to drop all databases)

After we add our task, it will also appear in this list.

Create a new Rake task

If you want to create your own rake instruction, you have 3 options for doing this:
  1. Write it yourself.
  2. Copy the code from another existing task and change its code.
  3. Use the Rails generator.

We use the third point.
$ rails g task setup hello_world

He will create a skeleton for our new instruction:
lib / tasks / setup.rake
namespace :setup do
  desc "TODO"
  task :hello_world => :environment do

Hello world

Let's go through the tokens of the newly created task

namespace :setup

Namespace, it is the namespace - this is the environment under which tasks will be grouped.
An example from RoR can be given rake db:migrate, where is dbalso a namespace

desc "TODO"

Description of our task. It is an optional component, but if you omit it, the task will not be displayed in the general list when output by the commandrake -T

task :hello_world => :environment

:hello_worldIs the name of the task.

=> :environment- addictions. Before starting the main task, Rake starts all dependent tasks. In this case, the rake environment instruction will be launched, which is included in the RoR assembly and allows you to work with environment-dependent operations, for example, using a database.

Greeting the world through rake will be easy. Add to the body of the task puts 'Hello from rake!' and run it
$ rake setup:hello_world
Hello from rake!


Rake allows you to call one task from another using a method invoke, for example:

This makes it possible to create a series of instructions that, using the built-in Rails tasks, will prepare our database for work:
task :drop_database do
  puts "*** Drop old database ***"
task :create_database do
  puts "*** Create new database ***"
task :migrate_database do
  puts "*** Do migrations ***"
task :seed_database do
  puts "*** Seeding database ***"
task :create_test_database do
  puts "*** Setup test database ***"

We delete the old database, create a new one, perform all the migrations, add the initial data, and create the base for the tests. These are standard actions that every developer does when installing the application.

Work with models

Inside the task, we can work with models in the same way as anywhere in the Rails application.

In my application, there is a model Userin which there is a method for adding an admin role. The file seeds.rbalso contains an entry for creating a user in the database. I need to make this user created immediately
an administrator, and I do not have to call the method for him manually through the Rails console. Implementing this is simple:
task :set_admin_user do
  puts "*** Add admin role to first user ***"

Putting it all together

Add the following statement at the end of the file (outside the namespace: setup block):
desc 'Configure the application for development.'
task :setup => :environment do


$ rake setup
*** Drop old database ***
*** Create new database ***
*** Do migrations ***
*** Seeding database ***
*** Add admin role to first user ***
*** Setup test database ***

Everything went as planned! Congratulations!


After creating this rake task, you have one more responsibility - to keep this file up to date, do not forget about it.

And remember - you are not the only developer. If some detail seems obvious to you, then another may lose a lot of time before you understand how to work with it. Try to simplify the development not only for yourself, but also for your colleagues.

Also popular now: