rails参数的任务

时间:2015-07-22 10:02:18

标签: ruby-on-rails task

我是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函数

由于

1 个答案:

答案 0 :(得分:0)

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]