
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
This command will list all the tasks available for rake.
After we add our task, it will also appear in this list.
If you want to create your own rake instruction, you have 3 options for doing this:
We use the third point.
He will create a skeleton for our new instruction:
lib / tasks / setup.rake
Let's go through the tokens of the newly created task
Namespace, it is the namespace - this is the environment under which tasks will be grouped.
An example from RoR can be given
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 command
Greeting the world through rake will be easy. Add to the body of the task
Rake allows you to call one task from another using a method
This makes it possible to create a series of instructions that, using the built-in Rails tasks, will prepare our database for work:
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.
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
an administrator, and I do not have to call the method for him manually through the Rails console. Implementing this is simple:
Add the following statement at the end of the file (outside the namespace: setup block):
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.
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:
- Write it yourself.
- Copy the code from another existing task and change its code.
- 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
end
end
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 db
also a namespacedesc "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 command
rake -T
task :hello_world => :environment
:hello_world
Is 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!
Invoke
Rake allows you to call one task from another using a method
invoke
, 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 database 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 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
User
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 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 ***"
User.first.become_admin!
end
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::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
Launch!
$ 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 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.