djechlin
djechlin

Reputation: 60768

What does "rake routes" actually do?

In a fresh(ish) rails project, the Rakefile looks like this:

# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.

require File.expand_path('../config/application', __FILE__)

Blog::Application.load_tasks

Yet rake routes produces the following output:

cpe-74-72-73-47:rails-blog-example djechlin$ rake routes
home_index GET /home/index(.:format) home#index
      root     /                     home#index

I don't understand how rake works so that it can get to the routes file or the routes task. Per the command line usage documentation, rake is invoked as

rake [options ...]  [VAR=VALUE ...]  [targets ...]

But the page has no explanation of what the targets are. I assume rake is called directly on the routes.rb file from this and that Rakefile is not related, but I can't confirm this at all.

Upvotes: 3

Views: 6456

Answers (2)

AnkitG
AnkitG

Reputation: 6568

A Rakefile contains executable Ruby code. Anything legal in a ruby script is allowed in a Rakefile.

When you trigger rake routes you call this piece of Ruby code .

Upvotes: 7

user229044
user229044

Reputation: 239311

Actually, the Rakefile is very related, and rake isn't called directly on routes.rb at all. Rake needs a rakefile. The magic happens inside load_tasks, which load the numerious Rails-specific Rake tasks that come with the framework.

When you invoke Rake, it looks for a Rakefile. The Rakefile is just Ruby. In your default Rakefile, first it includes ../config/application, where your application class (Blog::Application) is defined; then it invokes load_tasks, which is provided by Rails::Application, from which your Blog::Application inherits.

From there there are a million ways for each part of Rails to make Rake tasks available. Typically the core libraries provide Rail ties which expose tasks.

Upvotes: 3

Related Questions