📜 ⬆️ ⬇️

rake setup: Environment configuration for running a project on Ruby on Rails

Good day!

Rereading the blog Signals Vs Noise, I stumbled upon an interesting article

It recommended creating a rake task that will 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 any other setup. So when you git
> clone a new app, you know that “rake setup” will take care of you.

I will talk about how to do a similar task in a Ruby on Rails application.

Go to the folder with the application and run
rake -T 

This command will list all 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 to do this:
  1. Write it yourself.
  2. Copy code from another existing task and change its code.
  3. Use the Rails generator.

We use the third item.
 $ 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 end end 

Hello world


Go through the lexemes of the newly created task.

 namespace :setup 

Namespace, the namespace itself, is the environment under which tasks will be grouped.
An example of RoR is rake db:migrate , where db also 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 with the rake -T command.

 task :hello_world => :environment 

:hello_world is the name of the task.

=> :environment - dependencies. 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.

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

Invoke


Rake allows you to call one task from another using the invoke method, for example:
 Rake::Task['db:drop'].invoke 

This makes it possible to create a series of instructions that, using the built-in Rails tasks, will prepare our base for work:
 task :drop_database do puts "*** Drop old database ***" Rake::Task['db:drop'].invoke end task :create_database do puts "*** Create new database ***" Rake::Task['db:create'].invoke end task :migrate_database do puts "*** Do migrations ***" Rake::Task['db:migrate'].invoke end task :seed_database do puts "*** Seeding database ***" Rake::Task['db:seed'].invoke end task :create_test_database do puts "*** Setup test database ***" Rake::Task['db:test:prepare'].invoke end 

We delete the old database, create a new one, perform all migrations, add initial data, and create a base for tests. These are standard actions that every developer takes when installing an 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 User model, in which there is a method for adding an admin role. The file seeds.rb also contains an entry for creating a user in the database. I need to make this created user immediately
I was an administrator, and I didn’t have to call a method for it manually through the Rails console. Implementing it is simple:
 task :set_admin_user do puts "*** Add admin role to first user ***" User.first.become_admin! end 

Putting it all together


We add the following instruction at the end of the file (outside the namespace: setup block):
 desc 'Configure the application for development.' task :setup => :environment do Rake::Task['setup:drop_database'].invoke Rake::Task['setup:create_database'].invoke Rake::Task['setup:migrate_database'].invoke Rake::Task['setup:seed_database'].invoke Rake::Task['setup:set_admin_user'].invoke Rake::Task['setup:create_test_database'].invoke end 

Run!


 $ 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!

Conclusion


After creating this rake task, you have another 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 the other can lose a lot of time before they understand how to work with it. Try to simplify the development of not only yourself, but also colleagues.

Source: https://habr.com/ru/post/180071/


All Articles