I'm new in rails task and I want to execute and pass parameter in a task then in a loop to say if yes or no execute other task (not depending on the ENV but on my 'on moment' needs). Here is the code :
My task :
namespace :invoices do
desc "CREATE PDF AND SEND MAIL TASK"
task create_and_send_pdf: :environment do
Invoice.all.each do |invoice|
invoice.create_and_send_pdf({send_invoice: true})
end
end
end
My other function to call :
def create_and_send_pdf(options = {})
begin
to_print = self
...
if options[:send_invoice].blank? || options[:send_invoice]
send_to_customer(to_print)
end
...
end
return true
end
private
# Call mailer and send mail to customer
def send_to_customer(invoice)
unless invoice.send_at
key = SecureRandom.hex(10)
# Update invoice attibutes
invoice.update_attributes({track_number: key, send_at: DateTime.now})
InvoiceMailer.sample_email(invoice, key).deliver!
end
end
And my rake task :
rake invoices:create_and_send_pdf
So here is the deal, I don't want to call send_to_customer function if I execure my task with a send_invoice: false parameter
Thanks