Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are rake tasks stored in lib/tasks/?

My understanding of the lib/ directory in rails is that it stores non-domain specific code as a best practice.

However, my Rake scripts are very specific to my domain. They do things like create new models.

So is there a better place than lib/tasks/ to store domain-specific rake scripts, or am I missing something here?

like image 882
bgcode Avatar asked Apr 18 '17 22:04

bgcode


People also ask

Where are Rake tasks stored?

How to Write a Rake Task. You can put this code inside a file named Rakefile , or if you're using Rails, you can save this under lib/tasks/apple. rake .

What are Rake tasks?

Rake is a popular task runner for Ruby and Rails applications. For example, Rails provides the predefined Rake tasks for creating databases, running migrations, and performing tests. You can also create custom tasks to automate specific actions - run code analysis tools, backup databases, and so on.

How do I list a Rake task?

You can get a list of Rake tasks available to you, which will often depend on your current directory, by typing rake --tasks . Each task has a description, and should help you find the thing you need.

What is the difference between Rake and Ruby?

rake is a Make-like program implemented in Ruby. rails is a web framework, which also has some rake tasks. This means that you can have a ruby program with rake but without rails, but not the other way around. By itself, rake will be faster because you don't need to load the whole rails application.


1 Answers

I like this idea, and I agree - lib at one point was very much a junk drawer, and as a Rails community we've moved some of the junk away, but yes Rake tasks are usually very specific application logic.

In your Rakefile all you have to do is load your new Rakefiles (exercise for the reader: iterate the files in the folder instead of specifying it explicitly.

Example:

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

Rails.application.load_tasks

load('app/tasks/my_task.rake') # <--- my custom task!!!
like image 159
RyanWilcox Avatar answered Sep 20 '22 15:09

RyanWilcox