Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call controller from rake task

I would like to call controller action from rake task. My question is what method is the best to prepare http request? Thanks for all tips.

Edit: Have someone another tip? I tried this and not work:

controller_obj = Controller.new
controller.your_method

I got this exception:

rake aborted!
uninitialized constant Controller

Edit2: I tried:

sess = ActionController::Integration::Session.new
sess.post('/route', 'codes=3')

But I got (I have require 'action_controller/integration' in rake file) :

rake aborted!
cannot load such file -- action_controller/integration
like image 975
user2239655 Avatar asked Apr 08 '14 11:04

user2239655


People also ask

What is use of rake task?

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 you define a rake task?

Rake enables you to define a set of tasks and the dependencies between them in a file, and then have the right thing happen when you run any given task. The combination of convenience and flexibility that Rake provides has made it the standard method of job automation for Ruby projects.

What is environment rake task?

Including => :environment will tell Rake to load full the application environment, giving the relevant task access to things like classes, helpers, etc. Without the :environment , you won't have access to any of those extras.

What is rake extension?

Rake files ARE Ruby files, they just refer to extensions provided by the DSL. By using the same extension my editors are happy and Ruby is happy. Ruby's require will automatically provide the extension if you don't: If the filename has the extension “. rb”, it is loaded as a source file; if the extension is “.


3 Answers

task :use_controller_method_in_rake => :environment do
  session = ActionDispatch::Integration::Session.new(Rails.application)
  session.get "/foo"
  session.post "/bar", {my_post_params: 'foobar'}
end
like image 140
chris finne Avatar answered Oct 12 '22 23:10

chris finne


You should move that action's behavior to a model class, then use that action with the instance of that model class. This is the standard approach.

move controller/action to a model class (Recommended). In your rake task, do the following....

model_obj = Model.new
model_obj.your_instance_method

And still you want to call controller action then you should create an instance of your controller in your rake task and call that action with that instance(if that method is an instance method.)

controller_obj = Controller.new
controller.your_method
like image 42
Alok Anand Avatar answered Oct 13 '22 01:10

Alok Anand


Thanks @chris-finne, that was very helpful. Note that session.response.body is where you can access the results. Here's an method I'm using in a rake task:

def api_json_get(url, params={})
  @rails_session ||= ActionDispatch::Integration::Session.new(Rails.application)
  @rails_session.get(url, { :format => :json }.merge(params))
  JSON.parse(@rails_session.response.body)
end

Notice I'm memoizing the session object since it's quite slow to instantiate and I'm calling this method many times.

like image 31
Randomibis Avatar answered Oct 13 '22 01:10

Randomibis