Rails: Rake Tasks

While building a recent rails 5 app, I decided to enhance my documentation process by creating a suite of tasks that can help the next developer manage the app. Here are a few tasks I've created over the years that I use regularly.

Basic Task

Tasks help you create a repeatable set of scripts. For example, suppose you want to add a file to github, add a commit message and push your commit to github, you can turn it into a task.

Before

git add -A
git commit -m "custom_message"
git push

After

rails github:commit["my message"]

Beneath the hood

Create a task with a namespace of github and task name of commit.

rails g task github commit

Wrap the shell commands into executable scripts like this.

namespace :github do
  desc "›› Commit with message"
  task :commit, [:message] => :environment do |task, args|
    sh %{ git add -A }
    sh %{ git commit -m "#{args.message}" }
    sh %{ git push }
  end
end

Run the task

rails github:commit["my message"]

How to create a task

If you want to create a task in Rails, all you need to do is type:

rails g task namespace_name task_name

That's it!

Note

One minor change in Rails 5 is the switch from rake to rails. So rake db:create or rake task are now rails db:create and rails task, etc. I welcome this change as it helps developers remember one less thing.


View a list of available tasks

If you want to see a listing of available tasks, simply type:

rails -T

I use ›› as a marker to quickly identify my custom tasks.


Advanced Tasks

Here's a task created to automatically start and stop Webrick.

Revisiting Rails after 4 years of ExpressJS has been interesting. I find myself missing npm's nodemon to autostart my dev server every time I make changes. When it comes to gems that watch your environment, most of the Rails blogs suggest guard and live-reload but they lack the simplicity of nodemon. Therefore, I prefer rerun, a gem commonly used for Sinatra web-apps.

Step 1

Create a rails 5 app

rails _5.0.0_ new my_app

If this doesn't work for you, I suggest reading my Getting Started with Rails 5 article.

Step 2

Install Rerun by adding it to your Gemfile.

gem 'rerun', :groups => [:development, :test]

Step 3

Install the gem file

bundle install

Step 4 - Test rerun

Run rerun from your command line. If you notice, there are two explicit directories within the command line, config and app. This is how I'm telling rerun to specifically monitor the changes I'm making on my models, controllers, serializers, etc.

rerun --dir config,app rails s

Step 5 - Create a task

Now let's take this to the next level. Although running a single command in Terminal isn't too much effort, I often find myself moving on to another project and forgetting how I configured this one.

Therefore, I prefer to package these commands into Rails tasks. This command will tell Rails to generate a task with a file name server.rake with a method name of start.

rails g task server start

From there, change my tasks/server.rake file to look like this.

namespace :server do
  desc %Q{ ›› Run Rails while monitoring /app, /config }
  task :start do
    sh %{ rerun --dir config,app rails s  }
  end
end

Step 6 - Run your task

We're done, all we need to do now is call the rake task

rails server:start

Ta Da!


More with Tasks

Remove all comments within a Gemfile

Create the task

rails g task gemfile clean

Paste this into lib/tasks/gemfile.rake.

namespace :gemfile do
  desc %Q{ ›› Remove all the comments in Gemfile and make a Gemfile.bak just in case }
  task :clean do
    sh %{ ruby -pi.bak -pe "gsub(/^#.*\n/, '')" Gemfile }
  end
end

Run the task

rails gemfile:clean

Clearing a DB

This command drops the database, purges the schema, creates a new database, and populates it with data.

rails g task db wipe

Paste this into lib/tasks/db.rake.

namespace :db do
	desc %Q{ ›› Drop the db schema, Create a new one, Migrate the data, Populate the data }
  task wipe: :environment do
		sh %{ rails db:purge db:create db:migrate db:seed --trace  }
  end
end

Run the task

rails db:wipe

Even More with Tasks

If you take note of app.rake. One thing I often do is document the rails commands I use into a single task. Yes, it's cumbersome to do but on the bright side, I rarely have to look at Schema.rb or db/migrate/ to try and make sense of my models. Again, this isn't efficient but it does help keep a good record of your work.