diff --git a/lib/sendgrid.rb b/lib/sendgrid.rb index 7c68943..7a1765f 100644 --- a/lib/sendgrid.rb +++ b/lib/sendgrid.rb @@ -10,7 +10,8 @@ module SendGrid :subscriptiontrack, :footer, :spamcheck, - :bypass_list_management + :bypass_list_management, + :templates ] VALID_GANALYTICS_OPTIONS = [ @@ -67,7 +68,7 @@ def sendgrid_enable(*options) self.default_sg_options = Array.new unless self.default_sg_options options.each { |option| self.default_sg_options << option if VALID_OPTIONS.include?(option) } end - + # Sets the default text for subscription tracking (must be enabled). # There are two options: # 1. Add an unsubscribe link at the bottom of the email @@ -98,6 +99,11 @@ def sendgrid_unique_args(unique_args = {}) end end + # Call within mailer method to set the template_id. + def sendgrid_template_id(template_id) + @sg_template_id = template_id + end + # Call within mailer method to override the default value. def sendgrid_category(category) @sg_category = category @@ -299,6 +305,11 @@ def filters_hash_from_options(enabled_opts, disabled_opts) filters[:ganalytics]['settings'][key.to_s] = value end end + + when :templates + if @sg_template_id + filters[:templates]['settings']['template_id'] = @sg_template_id + end end end diff --git a/sendgrid.gemspec b/sendgrid.gemspec index ff125f0..54cd58a 100644 --- a/sendgrid.gemspec +++ b/sendgrid.gemspec @@ -45,7 +45,6 @@ Gem::Specification.new do |s| s.add_development_dependency(%q, [">= 0"]) s.add_development_dependency(%q, ["~> 1.0.0"]) s.add_development_dependency(%q, ["~> 1.5.1"]) - s.add_runtime_dependency(%q, [">= 0"]) else s.add_dependency(%q, [">= 0"]) s.add_dependency(%q, [">= 0"]) diff --git a/test/sendgrid_test.rb b/test/sendgrid_test.rb index 5d39844..d84bcef 100644 --- a/test/sendgrid_test.rb +++ b/test/sendgrid_test.rb @@ -39,4 +39,28 @@ def setup actual = JSON.parse(mail.header['X-SMTPAPI'].value) assert_equal(expected, actual) end + + should "something something sendgrid templates" do + + sendgrid_template_uuid = "sendgrid-template-uuid" + @options[:sendgrid_template_id] = sendgrid_template_uuid + @options.delete(:substitutions) + + test_email = SendgridTemplateMailer.create_test(@options).deliver + + assert_equal(test_email.header["sendgrid-template-id"].value, sendgrid_template_uuid) + expected_filters = { + "templates" => { + "settings" => { + "enable" => 1, + "template_id" => "sendgrid-template-uuid" + } + } + } + filters = JSON.parse(test_email.header["X-SMTPAPI"].value)["filters"] + assert_equal(expected_filters, filters) + + end + + end diff --git a/test/test_helper.rb b/test/test_helper.rb index 75e8d21..f4b1d29 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -46,6 +46,32 @@ def handle_sendgrid_options(options) end end +class SendgridTemplateMailer < ActionMailer::Base + include SendGrid + + def create_test(options) + handle_sendgrid_options(options) + mail(options) + end + + protected + def handle_sendgrid_options(options) + if options[:sendgrid_template_id] + sendgrid_enable :templates + sendgrid_template_id options[:sendgrid_template_id] + end + + if options[:substitutions] + options[:substitutions].each do |find, replace| + sendgrid_substitute(find, replace) + end + end + + sendgrid_recipients options[:to] + sendgrid_category(options[:category]) + end +end + class SendgridUniqueArgsMailer < ActionMailer::Base include SendGrid sendgrid_unique_args({ :test_arg => "test value" })