Call Application Controller Functions from a Rake Task

I want to call a function in my ApplicationController from a rake task. I added the tag =>: environment, but it just doesn't want to work.

Here is my stripped down code -

lib\taks\autoscrape.rake: desc "This task will scrape all the movies without info" task(:autoscrape => :environment) do require 'application' #probably extraneous require File.dirname(__FILE__) + '/../../config/environment' #probably extraneous unless ApplicationController.is_admin? logger.error = "Sorry, you're not allowed to do that" return end app\controller\application_controller.rb: class ApplicationController < ActionController::Base helper :all # include all helpers, all the time def is_admin? session[:is_admin] && session[:is_admin] > 0 end end result: rake scrape:autoscrape --trace ** Invoke scrape:autoscrape (first_time) ** Invoke environment (first_time) ** Execute environment ** Execute scrape:autoscrape rake aborted! undefined method `is_admin?' for ApplicationController:Class E:/Dropbox/My Dropbox/Ruby/moviecat/lib/tasks/autoscrape.rake:11 

My other controllers always call this code, no problem. How can my Rake task call this code? This is greatly simplified, this is part of a more serious problem, I would like to reuse more code.

Thanks!!

+6
ruby-on-rails rake
source share
4 answers

Firstly, the error you get is related to the fact that you are calling ApplicationController.is_admin? which is not defined because your method is defined in instances of ApplicationController and not in the ApplicationController class.

Secondly, the concept of session (at least for me) does not really make much sense in the rake problem. There are no real sessions on the command line other than a user session. This is not what you get.

Honestly, I don’t know how best to get to calling the Controller action / method from anywhere outside the classes that inherit from ApplicationController or ActionController::Base , or why you want to. These actions / methods are specifically designed for use during the request, and not for any actions that you invoke each time. If you really need something and you do not want to redefine it, put it in the model / library and include it in it.

+6
source share

Create an instance for the controller and call method.method inside the controller, which should be public.

Example:

 objEmail = EmailController.new objEmail.message 
+6
source share

Perhaps the reason rajat doesn't make sense, but the question really is.

I need to do some maintenance, and I need to execute a function inside the class. A workaround is that every moment is fixed (for example, migration), and it is not necessary to do it properly.

This may be caused by calling url instead of calling rake (with the appropriate route and parameter settings).

This can be done by defining functions outside the class (making it publicly available).

Or it can be done, as suggested by William Richerd, who, incidentally, works.

0
source share

In your rake task you have:

 unless ApplicationController.is_admin? 

You should add .new to the controller name:

 unless ApplicationController.new.is_admin? 
-one
source share

All Articles