csv ファイルを生成してメーラーに送信する以下の resque ジョブがあります。csv ファイルにデータがあることを確認したいので、空のファイルをメールで送信しません。なぜかPerformメソッドの外にメソッドを書くとうまくいきません。たとえば、csv ファイルの最初の行にデータがあることがわかっている場合、以下のコードは無効と出力されます。以下の行のコメントを外すと、正常に機能することを確認できますが、このファイルのチェックを別のメソッドに抽出したいと考えています。これは正しいです?
class ReportJob
@queue = :report_job
def self.perform(application_id, current_user_id)
user = User.find(current_user_id)
client_application = Application.find(client_application_id)
transactions = application.transactions
file = Tempfile.open(["#{Rails.root}/tmp/", ".csv"]) do |csv|
begin
csv_file = CSV.new(csv)
csv_file << ["Application", "Price", "Tax"]
transactions.each do |transaction|
csv_file << [application.name, transaction.price, transaction.tax]
end
ensure
ReportJob.email_report(user.email, csv_file)
#ReportMailer.send_report(user.email, csv_file).deliver
csv_file.close(unlink=true)
end
end
end
def self.email_report(email, csv)
array = csv.to_a
if array[1].blank?
puts "invalid"
else
ReportMailer.send_report(email, csv).deliver
end
end
end