rails 带参数的任务
rails task with parameters
我是 rails 任务的新手,我想在任务中执行并传递参数,然后循环判断是否执行其他任务(不取决于 ENV,而是取决于我的 'on moment'需要)。
这是代码:
我的任务:
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
我要调用的其他函数:
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
我的佣金任务:
rake invoices:create_and_send_pdf
所以这是交易,如果我使用 send_invoice: false 参数
执行我的任务,我不想调用 send_to_customer 函数
谢谢
task :create_and_send_pdf, [:send_invoice] => :environment do |task, args|
Invoice.all.each do |invoice|
invoice.create_and_send_pdf({send_invoice: args.send_invoice})
end
end
rake invoices:create_and_send_pdf[true]
rake invoices:create_and_send_pdf[false]
我是 rails 任务的新手,我想在任务中执行并传递参数,然后循环判断是否执行其他任务(不取决于 ENV,而是取决于我的 'on moment'需要)。 这是代码:
我的任务:
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
我要调用的其他函数:
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
我的佣金任务:
rake invoices:create_and_send_pdf
所以这是交易,如果我使用 send_invoice: false 参数
执行我的任务,我不想调用 send_to_customer 函数谢谢
task :create_and_send_pdf, [:send_invoice] => :environment do |task, args|
Invoice.all.each do |invoice|
invoice.create_and_send_pdf({send_invoice: args.send_invoice})
end
end
rake invoices:create_and_send_pdf[true]
rake invoices:create_and_send_pdf[false]