Maxsy
Maxsy

Reputation: 225

How to include a module to a rake namespace in separate files?

I have a module and a child class. Where i have all the functionality inside the module. and inside the child class I just call the methods from the module. I want this module to be linked with a rake task under a namespace, and these two files are in the same directory. RAILS_ROOT/lib. How do I do this? I am running Rails 3.0.3.

Upvotes: 8

Views: 5178

Answers (2)

cman77
cman77

Reputation: 1783

You need to both require and then include:

require 'your_module'
namespace :your_task do
   include YourModule
   ...

Upvotes: 3

Andrea Pavoni
Andrea Pavoni

Reputation: 5311

create a file under lib/tasks/your_namespace.rake , and write the task:

namespace :your_namespace do
  desc "An optional description of your task"
  task :your_task_name => [:environment] do
    # your code stuff
  end
end

you should be able to use code from modules. In case, just add this line in rake task:

require 'yourfile'

Upvotes: 3

Related Questions