From 9cadb99f8ad36a25c7d73b9de2bad3c1f2b8744b Mon Sep 17 00:00:00 2001 From: Ilya Zhitomirskiy Date: Mon, 31 Oct 2011 16:34:03 -0700 Subject: [PATCH] sending csv as a file --- app/mailers/notifier.rb | 7 +++++++ lib/tasks/stats.rake | 2 +- spec/mailers/notifier_spec.rb | 5 +++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/app/mailers/notifier.rb b/app/mailers/notifier.rb index 30d58056b..b7a746cca 100644 --- a/app/mailers/notifier.rb +++ b/app/mailers/notifier.rb @@ -15,6 +15,12 @@ class Notifier < ActionMailer::Base def single_admin(string, recipient, opts={}) @receiver = recipient @string = string.html_safe + + if attach = opts.delete(:attachments) + attach.each{ |f| + attachments[f[:name]] = f[:file] + } + end default_opts = {:to => @receiver.email, :from => AppConfig[:smtp_sender_address], @@ -22,6 +28,7 @@ class Notifier < ActionMailer::Base default_opts.merge!(opts) + mail(default_opts) end diff --git a/lib/tasks/stats.rake b/lib/tasks/stats.rake index b551aaeca..b23770b66 100644 --- a/lib/tasks/stats.rake +++ b/lib/tasks/stats.rake @@ -17,7 +17,7 @@ namespace :stats do end end - emails = Notifier.admin(string, admins, {:subject => "retention numbers #{Time.now.to_s}"}) + emails = Notifier.admin( "here are some retention stats", admins, {:subject => "retention numbers #{Time.now.to_s}", :attachments => [:name => "retention_numbers_#{Time.now.to_s}.csv", :file => string]}) emails.each {|e| e.deliver!} end end diff --git a/spec/mailers/notifier_spec.rb b/spec/mailers/notifier_spec.rb index 1d0092564..4322ba1f8 100644 --- a/spec/mailers/notifier_spec.rb +++ b/spec/mailers/notifier_spec.rb @@ -45,6 +45,11 @@ describe Notifier do mail = Notifier.single_admin("Welcome to bureaucracy!", bob) mail.body.encoded.should match /change your notification settings/ end + + it 'has an optional attachment' do + mail = Notifier.single_admin("Welcome to bureaucracy!", bob, :attachments => [{:name => "retention stats", :file => "here is some file content"}]) + mail.attachments.length.should == 1 + end end describe ".started_sharing" do