What is the purpose of RakeFile in the application root directory - ruby-on-rails

I am using Ruby on Rails and I see a 'Rakefile' in my application's root directory. What is its purpose and when will it get executed?

The Rakefile is a ruby-written file which contains the definition of Rake tasks.
Here you can find a small introduction to Rake.
The Rakefile can include other Ruby files. This is the case of Rails projects.
In fact, in a Rails project you shouldn't change the Rakefile directly. Instead, you can add more rake tasks by creating .rake files in the lib/tasks folder of your Rails project.

It contains several pre-defined actions (named tasks) that you could perform on the Rails project. If you run rake -T, it will show you a list of all available tasks. That will provide you with more info.

Related

Is it ok if I just delete a rails app directory? Will it leave any traces behind on the system?

I just created some sample apps by running
rails new
command and then deleted the directory in which I created them after my work was done. Do I have to run some other command(s) to delete all the traces which rails could have left behind?
Besides the local files, you may have created a database and installed some gems.
To remove unused gems, run gem cleanup.
To purge the database, use rake db:drop in the project folder.
Files generated using rails new are local, and will be fully removed when you delete the folder. Running the command shows you which files were created.
$ rails new commandsapp
create
create README.md
create Rakefile
create config.ru
create .gitignore
create Gemfile
create app
...
create tmp/cache
...
run bundle install

Where is the Rakefile in a Railsapp?

As the question states, where is the Rakefile in a Railsapp?
In my app it is no where to be found. I'm working on an app with another developer and he's just pushed code to the Rakefile as such:
7  Rakefile
## -0,0 +1,7 ##
+# 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__)
+require 'rake'
+
+Newapp::Application.load_tasks
Although, I can't find this code anywhere in the actual app. Please help clear this up for me!
Thanks!
Conventionally, the Rakefile usually goes at the Rails root of your project (this is the location first generated when you ran rails new ...).
rake can also be run by using a Rakefile anywhere, by using rake -f path/to/my/rakefile.rb, so if you need to diverge from the convention for some reason, that's possible too.
However, note that many third-party services presume that it's possible to run rake from the root of your project and get the desired result (e.g. heroku rake).

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.
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 .
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.

How to run a rake task by command line in rails

I have defined a rake task as follows in a file called file_locker_task.rake
namespace :myspace do
task :process => :environment do
FileLocker.lock_files
end
end
How do I execute this rake task from the command line?
I tried:
rake myspace:process and rake process but both are throwing an error like this:
rake aborted!
Don't know how to build task 'process'
Run rake -T -A from your Rails home directory to see all the tasks that rake knows about. Yours must be in that list for rake to run it.
By default, in a Rails app, rake looks in the lib/tasks directory and its subdirectories for your .rake files. Check that. (I suspect this is the problem.)
According to docs
Any ruby file (including other rakefiles) can be included with a standard Ruby require command.
-
Additional rake files (with the file extension “.rake”) may be placed in rakelib directory located at the top level of a project (i.e. the same directory that contains the main Rakefile). Also, rails projects may include additional rake files in the lib/tasks directory.

Seeding database from engine root fails to locate db/seeds.rb

After creating a new Rails 3.1 gem plugin via:
rails plugin new core --full
I am trying to seed the database with:
rake db:seed
I am running into the following error:
rake aborted!
can't convert nil into String
Moving the seeds.rb file into the test/dummy/db directory seems to resolve the problem. Is there any way to let the db:seed task know what the current working directory is, or where to find the seeds.rb file? The ideal solution would be to keep seeds.rb within my engines db directory
I got this error when I made a mistake naming my seed file "seed.rb", not "seeds.rb". Hope, this helps.
Try creating your engine as
rails plugin new core --full --mountable
You should now be able to do rake db:migrate from your engine root. If it's not a mountable engine, since the generators would typically deploy to your app (in this case test/dummy or spec/dummy) it makes sense that the rake task would be run from the host application.
I solved this by creating a seeds.rb in my dummy/db directory. In the seeds.rb file I created, I just added:
require File.expand_path('../../../../db/seeds.rb', __FILE__)
This basically extends and runs the seeds.rb file in the engines db directory.
Please Note
I am using both --full and --mountable and rake db:seed did not work (plus various other things)
I am using rspec for my tests, so my dummy is in the spec/dummy directory of the engine
I modified my rake and rails so that I could get rspec working
useful posts I have found
rails 3.1 engines with rspec
Hope this helps

Resources