From cdb271bf98dccbb9f912346f98c8271282bf34f2 Mon Sep 17 00:00:00 2001 From: Jonathan Kolyer Date: Tue, 18 Mar 2014 22:50:06 +0000 Subject: [PATCH] VRFS-1483 added async support --- ruby/Gemfile | 1 + ruby/lib/jam_ruby.rb | 1 + ruby/lib/jam_ruby/app/mailers/batch_mailer.rb | 26 ++++++++++++++----- ruby/lib/jam_ruby/models/email_batch.rb | 2 +- ruby/spec/mailers/batch_mailer_spec.rb | 3 ++- 5 files changed, 25 insertions(+), 8 deletions(-) diff --git a/ruby/Gemfile b/ruby/Gemfile index 7ba18443c..52c04053e 100644 --- a/ruby/Gemfile +++ b/ruby/Gemfile @@ -40,6 +40,7 @@ gem 'resque' gem 'resque-retry' gem 'resque-failed-job-mailer' #, :path => "/Users/seth/workspace/resque_failed_job_mailer" gem 'resque-lonely_job', '~> 1.0.0' +gem 'resque_mailer' gem 'oj' gem 'builder' gem 'fog' diff --git a/ruby/lib/jam_ruby.rb b/ruby/lib/jam_ruby.rb index 927d1e40f..afd8c77c7 100755 --- a/ruby/lib/jam_ruby.rb +++ b/ruby/lib/jam_ruby.rb @@ -132,6 +132,7 @@ require "jam_ruby/models/country" require "jam_ruby/models/region" require "jam_ruby/models/city" require "jam_ruby/models/email_batch" +require "jam_ruby/app/mailers/async_mailer" require "jam_ruby/app/mailers/batch_mailer" include Jampb diff --git a/ruby/lib/jam_ruby/app/mailers/batch_mailer.rb b/ruby/lib/jam_ruby/app/mailers/batch_mailer.rb index 4c6873efc..8b16acd08 100644 --- a/ruby/lib/jam_ruby/app/mailers/batch_mailer.rb +++ b/ruby/lib/jam_ruby/app/mailers/batch_mailer.rb @@ -1,17 +1,31 @@ module JamRuby - class BatchMailer < ActionMailer::Base - include SendGrid - + class BatchMailer < JamRuby::AsyncMailer layout "batch_mailer" sendgrid_category :batch_email sendgrid_category :use_subject_lines sendgrid_unique_args :env => Environment.mode - def send_batch_email(batch, user) - @user = user + def send_batch_email(batch_id, user_id) + @user = User.where(:id => user_id).limit(1).first + batch = EmailBatch.where(:id => batch_id).limit(1).first @body = batch.merged_body(user) - mail(:to => user.email, :from => batch.from_email, :subject => batch.subject) do |format| + + mail(:to => user.email, + :from => batch.from_email, + :subject => batch.subject) do |format| + format.text + format.html + end + end + + def send_batch_email_test(batch_id, email_addy) + batch = EmailBatch.where(:id => batch_id).limit(1).first + @body = batch.body + + mail(:to => email_addy, + :from => batch.from_email, + :subject => batch.subject) do |format| format.text format.html end diff --git a/ruby/lib/jam_ruby/models/email_batch.rb b/ruby/lib/jam_ruby/models/email_batch.rb index 792aaf7bc..9df7d3d30 100644 --- a/ruby/lib/jam_ruby/models/email_batch.rb +++ b/ruby/lib/jam_ruby/models/email_batch.rb @@ -32,7 +32,7 @@ module JamRuby def send_test_batch self.test_users.each do |uu| - BatchMailer.send_batch_email(self, uu).deliver + BatchMailer.send_batch_email_test(self.id, uu.email).deliver end end diff --git a/ruby/spec/mailers/batch_mailer_spec.rb b/ruby/spec/mailers/batch_mailer_spec.rb index ed578da89..1febc734c 100644 --- a/ruby/spec/mailers/batch_mailer_spec.rb +++ b/ruby/spec/mailers/batch_mailer_spec.rb @@ -7,6 +7,7 @@ describe BatchMailer do batch = FactoryGirl.create(:email_batch) batch.send_test_batch + it { BatchMailer.deliveries.length.should == 4 } it { mail['from'].to_s.should == EmailBatch::DEFAULT_SENDER } @@ -14,7 +15,7 @@ describe BatchMailer do it { mail.subject.should == batch.subject } it { mail.multipart?.should == true } # because we send plain + html - it { mail.parts[1].decode_body.should match(/#{Regexp.escape(batch.merged_body(batch.test_users[0]))}/) } + it { mail.text_part.decode_body.should match(/#{Regexp.escape(batch.body)}/) } end end