From e0d8e4d26d6482f8db0ff5dda05d9db0b24e5970 Mon Sep 17 00:00:00 2001 From: Fire-Dragon-DoL Date: Fri, 7 Jun 2013 00:19:09 +0200 Subject: [PATCH 1/9] Working on a fix for mail override method --- lib/sendgrid/version.rb | 2 +- test/sendgrid_test.rb | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/sendgrid/version.rb b/lib/sendgrid/version.rb index 720d817..de41fcb 100644 --- a/lib/sendgrid/version.rb +++ b/lib/sendgrid/version.rb @@ -1,3 +1,3 @@ module SendGrid #:nodoc: - VERSION = "2.0.0" + VERSION = "2.0.1" end diff --git a/test/sendgrid_test.rb b/test/sendgrid_test.rb index 6c3ff89..b07c245 100644 --- a/test/sendgrid_test.rb +++ b/test/sendgrid_test.rb @@ -1,4 +1,5 @@ require 'test_helper' +require 'pry' class SendgridTest < Test::Unit::TestCase def setup @@ -33,6 +34,7 @@ def setup should "pass unique args from both the mailer class and the mailer method through custom headers" do @options.delete(:substitutions) SendgridUniqueArgsMailer.unique_args_test_email(@options).deliver + # binding.pry mail = ActionMailer::Base.deliveries.last # assert({ :unique_args => {:mailer_method_unique_arg => "some value", :test_arg => "test value"} }.to_json == mail.header['X-SMTPAPI'].value) expected = { 'unique_args' => {'mailer_method_unique_arg' => "some value", 'test_arg' => "test value"} } From adab7ed92b371c3a908c1a58eaddb5f04c33f8dc Mon Sep 17 00:00:00 2001 From: Fire-Dragon-DoL Date: Fri, 7 Jun 2013 01:50:07 +0200 Subject: [PATCH 2/9] Removed protected and moved super to bottom --- lib/sendgrid.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/sendgrid.rb b/lib/sendgrid.rb index cf230ef..e2f5dc2 100644 --- a/lib/sendgrid.rb +++ b/lib/sendgrid.rb @@ -160,11 +160,12 @@ def sendgrid_ganalytics_options(options) options.each { |option| @ganalytics_options << option if VALID_GANALYTICS_OPTIONS.include?(option[0].to_sym) } end - protected + # protected # Sets the custom X-SMTPAPI header after creating the email but before delivery - def mail(headers={}, &block) - m = super + # def mail(headers={}, &block) + def mail(*args, &block) + # m = super if @sg_substitutions && !@sg_substitutions.empty? @sg_substitutions.each do |find, replace| raise ArgumentError.new("Array for #{find} is not the same size as the recipient array") if replace.size != @sg_recipients.size @@ -174,7 +175,8 @@ def mail(headers={}, &block) Rails.logger.debug "SendGrid X-SMTPAPI: #{sendgrid_json_headers(message)}" if DEBUG self.headers['X-SMTPAPI'] = sendgrid_json_headers(message) - m + # m + super if defined? super end private From 7e608954f814507c4ad69f09c7cdc9eda51d0ac0 Mon Sep 17 00:00:00 2001 From: Fire-Dragon-DoL Date: Fri, 7 Jun 2013 01:50:44 +0200 Subject: [PATCH 3/9] Updated version --- lib/sendgrid/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/sendgrid/version.rb b/lib/sendgrid/version.rb index de41fcb..a67470b 100644 --- a/lib/sendgrid/version.rb +++ b/lib/sendgrid/version.rb @@ -1,3 +1,3 @@ module SendGrid #:nodoc: - VERSION = "2.0.1" + VERSION = "2.0.2" end From f716ee5d7ecdd233cc218b33b31e2fd249b47125 Mon Sep 17 00:00:00 2001 From: Fire-Dragon-DoL Date: Fri, 7 Jun 2013 02:21:55 +0200 Subject: [PATCH 4/9] Updated version --- lib/sendgrid.rb | 2 +- lib/sendgrid/version.rb | 2 +- test/sendgrid_test.rb | 2 -- 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/sendgrid.rb b/lib/sendgrid.rb index e2f5dc2..c0f19a4 100644 --- a/lib/sendgrid.rb +++ b/lib/sendgrid.rb @@ -160,7 +160,7 @@ def sendgrid_ganalytics_options(options) options.each { |option| @ganalytics_options << option if VALID_GANALYTICS_OPTIONS.include?(option[0].to_sym) } end - # protected + protected # Sets the custom X-SMTPAPI header after creating the email but before delivery # def mail(headers={}, &block) diff --git a/lib/sendgrid/version.rb b/lib/sendgrid/version.rb index a67470b..ecc2aea 100644 --- a/lib/sendgrid/version.rb +++ b/lib/sendgrid/version.rb @@ -1,3 +1,3 @@ module SendGrid #:nodoc: - VERSION = "2.0.2" + VERSION = "2.0.3" end diff --git a/test/sendgrid_test.rb b/test/sendgrid_test.rb index b07c245..6c3ff89 100644 --- a/test/sendgrid_test.rb +++ b/test/sendgrid_test.rb @@ -1,5 +1,4 @@ require 'test_helper' -require 'pry' class SendgridTest < Test::Unit::TestCase def setup @@ -34,7 +33,6 @@ def setup should "pass unique args from both the mailer class and the mailer method through custom headers" do @options.delete(:substitutions) SendgridUniqueArgsMailer.unique_args_test_email(@options).deliver - # binding.pry mail = ActionMailer::Base.deliveries.last # assert({ :unique_args => {:mailer_method_unique_arg => "some value", :test_arg => "test value"} }.to_json == mail.header['X-SMTPAPI'].value) expected = { 'unique_args' => {'mailer_method_unique_arg' => "some value", 'test_arg' => "test value"} } From d2902a989787e9b4e311b0e4c5d375f65b00801b Mon Sep 17 00:00:00 2001 From: Fire-Dragon-DoL Date: Fri, 7 Jun 2013 02:38:35 +0200 Subject: [PATCH 5/9] Updated version --- lib/sendgrid.rb | 5 +---- lib/sendgrid/version.rb | 2 +- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/lib/sendgrid.rb b/lib/sendgrid.rb index c0f19a4..3ead494 100644 --- a/lib/sendgrid.rb +++ b/lib/sendgrid.rb @@ -163,9 +163,7 @@ def sendgrid_ganalytics_options(options) protected # Sets the custom X-SMTPAPI header after creating the email but before delivery - # def mail(headers={}, &block) - def mail(*args, &block) - # m = super + def mail(headers={}, &block) if @sg_substitutions && !@sg_substitutions.empty? @sg_substitutions.each do |find, replace| raise ArgumentError.new("Array for #{find} is not the same size as the recipient array") if replace.size != @sg_recipients.size @@ -175,7 +173,6 @@ def mail(*args, &block) Rails.logger.debug "SendGrid X-SMTPAPI: #{sendgrid_json_headers(message)}" if DEBUG self.headers['X-SMTPAPI'] = sendgrid_json_headers(message) - # m super if defined? super end diff --git a/lib/sendgrid/version.rb b/lib/sendgrid/version.rb index ecc2aea..f909097 100644 --- a/lib/sendgrid/version.rb +++ b/lib/sendgrid/version.rb @@ -1,3 +1,3 @@ module SendGrid #:nodoc: - VERSION = "2.0.3" + VERSION = "2.0.4" end From fcc81389d766ca5075e85d118711b2e3c9e36b5b Mon Sep 17 00:00:00 2001 From: Fire-Dragon-DoL Date: Fri, 7 Jun 2013 02:48:27 +0200 Subject: [PATCH 6/9] Fixed the issue with inherited mail method not behaving as expected, it didn't append sendgrid headers --- lib/sendgrid/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/sendgrid/version.rb b/lib/sendgrid/version.rb index f909097..de41fcb 100644 --- a/lib/sendgrid/version.rb +++ b/lib/sendgrid/version.rb @@ -1,3 +1,3 @@ module SendGrid #:nodoc: - VERSION = "2.0.4" + VERSION = "2.0.1" end From 3a5891088a17a4b2eecb7970a27970b29c002202 Mon Sep 17 00:00:00 2001 From: Fire-Dragon-DoL Date: Sat, 8 Jun 2013 04:32:12 +0200 Subject: [PATCH 7/9] Solved the issue with mail inclusion, where class ActionMailer::Base#mail method completely hides the module method --- lib/sendgrid.rb | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/sendgrid.rb b/lib/sendgrid.rb index 3ead494..1d5710a 100644 --- a/lib/sendgrid.rb +++ b/lib/sendgrid.rb @@ -26,6 +26,13 @@ module SendGrid def self.included(base) base.class_eval do + + # NOTE: This code is required to correctly override the ActionMailer::Base + # #mail method. Otherwise, mail method for the class will hide completely + # the one included from the module. + alias_method :send_grid_old_mail, :mail + remove_method :mail + class << self attr_accessor :default_sg_category, :default_sg_options, :default_subscriptiontrack_text, :default_footer_text, :default_spamcheck_score, :default_sg_unique_args @@ -42,6 +49,7 @@ class << self # end base.extend(ClassMethods) + end module ClassMethods @@ -163,6 +171,7 @@ def sendgrid_ganalytics_options(options) protected # Sets the custom X-SMTPAPI header after creating the email but before delivery + # def mail(headers={}, &block) def mail(headers={}, &block) if @sg_substitutions && !@sg_substitutions.empty? @sg_substitutions.each do |find, replace| @@ -173,7 +182,10 @@ def mail(headers={}, &block) Rails.logger.debug "SendGrid X-SMTPAPI: #{sendgrid_json_headers(message)}" if DEBUG self.headers['X-SMTPAPI'] = sendgrid_json_headers(message) - super if defined? super + + # NOTE: Added to ensure that ActionMailer::Base#mail method is called, + # after SendGrid headers have been injected. + send_grid_old_mail(headers, &block) end private From 1e6046594d25b3a3404cfb58dfddf2c6dfbd471a Mon Sep 17 00:00:00 2001 From: Fire-Dragon-DoL Date: Sat, 8 Jun 2013 05:50:38 +0200 Subject: [PATCH 8/9] Changed the code to make it more simple, tests now work correctly --- lib/sendgrid.rb | 54 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 37 insertions(+), 17 deletions(-) diff --git a/lib/sendgrid.rb b/lib/sendgrid.rb index 1d5710a..bafd224 100644 --- a/lib/sendgrid.rb +++ b/lib/sendgrid.rb @@ -30,8 +30,28 @@ def self.included(base) # NOTE: This code is required to correctly override the ActionMailer::Base # #mail method. Otherwise, mail method for the class will hide completely # the one included from the module. - alias_method :send_grid_old_mail, :mail - remove_method :mail + alias_method :send_grid_old_mail, :mail + protected + + # Sets the custom X-SMTPAPI header after creating the email but before delivery + # def mail(headers={}, &block) + def mail(headers={}, &block) + if @sg_substitutions && !@sg_substitutions.empty? + @sg_substitutions.each do |find, replace| + raise ArgumentError.new("Array for #{find} is not the same size as the recipient array") if replace.size != @sg_recipients.size + end + end + + Rails.logger.debug "SendGrid X-SMTPAPI: #{sendgrid_json_headers(message)}" if DEBUG + + self.headers['X-SMTPAPI'] = sendgrid_json_headers(message) + + # NOTE: Added to ensure that ActionMailer::Base#mail method is called, + # after SendGrid headers have been injected. + send_grid_old_mail(headers, &block) + end + + public class << self attr_accessor :default_sg_category, :default_sg_options, :default_subscriptiontrack_text, @@ -168,25 +188,25 @@ def sendgrid_ganalytics_options(options) options.each { |option| @ganalytics_options << option if VALID_GANALYTICS_OPTIONS.include?(option[0].to_sym) } end - protected + # protected - # Sets the custom X-SMTPAPI header after creating the email but before delivery - # def mail(headers={}, &block) - def mail(headers={}, &block) - if @sg_substitutions && !@sg_substitutions.empty? - @sg_substitutions.each do |find, replace| - raise ArgumentError.new("Array for #{find} is not the same size as the recipient array") if replace.size != @sg_recipients.size - end - end + # # Sets the custom X-SMTPAPI header after creating the email but before delivery + # # def mail(headers={}, &block) + # def mail(headers={}, &block) + # if @sg_substitutions && !@sg_substitutions.empty? + # @sg_substitutions.each do |find, replace| + # raise ArgumentError.new("Array for #{find} is not the same size as the recipient array") if replace.size != @sg_recipients.size + # end + # end - Rails.logger.debug "SendGrid X-SMTPAPI: #{sendgrid_json_headers(message)}" if DEBUG + # Rails.logger.debug "SendGrid X-SMTPAPI: #{sendgrid_json_headers(message)}" if DEBUG - self.headers['X-SMTPAPI'] = sendgrid_json_headers(message) + # self.headers['X-SMTPAPI'] = sendgrid_json_headers(message) - # NOTE: Added to ensure that ActionMailer::Base#mail method is called, - # after SendGrid headers have been injected. - send_grid_old_mail(headers, &block) - end + # # NOTE: Added to ensure that ActionMailer::Base#mail method is called, + # # after SendGrid headers have been injected. + # send_grid_old_mail(headers, &block) + # end private From c2d9a4191605dbf76a29c5649cf006e85e70bf6e Mon Sep 17 00:00:00 2001 From: Fire-Dragon-DoL Date: Sat, 8 Jun 2013 05:52:18 +0200 Subject: [PATCH 9/9] Forgot to remove commented #mail method from the module itself --- lib/sendgrid.rb | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/lib/sendgrid.rb b/lib/sendgrid.rb index bafd224..49fa0e6 100644 --- a/lib/sendgrid.rb +++ b/lib/sendgrid.rb @@ -188,26 +188,6 @@ def sendgrid_ganalytics_options(options) options.each { |option| @ganalytics_options << option if VALID_GANALYTICS_OPTIONS.include?(option[0].to_sym) } end - # protected - - # # Sets the custom X-SMTPAPI header after creating the email but before delivery - # # def mail(headers={}, &block) - # def mail(headers={}, &block) - # if @sg_substitutions && !@sg_substitutions.empty? - # @sg_substitutions.each do |find, replace| - # raise ArgumentError.new("Array for #{find} is not the same size as the recipient array") if replace.size != @sg_recipients.size - # end - # end - - # Rails.logger.debug "SendGrid X-SMTPAPI: #{sendgrid_json_headers(message)}" if DEBUG - - # self.headers['X-SMTPAPI'] = sendgrid_json_headers(message) - - # # NOTE: Added to ensure that ActionMailer::Base#mail method is called, - # # after SendGrid headers have been injected. - # send_grid_old_mail(headers, &block) - # end - private # Take all of the options and turn it into the json format that SendGrid expects