Create pdf with shrimp in the background using resque

I am trying to create a PDF document in the background using the job job Resque.

My code for creating a PDF file is a Rails helper method that I want to use in a Resque working document, for example:

class DocumentCreator @queue = :document_creator_queue require "prawn" def self.perform(id) @doc = Document.find(id) Prawn::Document.generate('test.pdf') do |pdf| include ActionView::Helpers::DocumentHelper create_pdf(pdf) end end end 

The create_pdf method relates to DocumentHelper , but I get this error:

 undefined method `create_pdf' 

Does anyone know how to do this?

+4
source share
1 answer

You are trying to call an instance method ( create_pdf ) from a class method ( self.perform ). Your code will only work if your DocumentHelper defined by create_pdf as a class method:

 def self.create_pdf 

If you don't need access to create_pdf in your views, you can move it to your Document class instead as an instance method, and then you can do @doc.create_pdf(pdf) .

However, if you need access to create_pdf in your views, you can put module_function :create_pdf inside your DocumentHelper file or you can dynamically add this to your working one:

 DocumentHelper.module_eval do module_function(:create_pdf) end DocumentHelper.create_pdf(pdf) 

Then you can correctly call DocumentHelper.create_pdf .

Also, in Rails 3, it seems to me that you only need to include DocumentHelper , and not include ActionView::Helpers::DocumentHelper .

+2
source

All Articles