Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions lib/sendgrid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ module SendGrid
:subscriptiontrack,
:footer,
:spamcheck,
:bypass_list_management
:bypass_list_management,
:templates
]

VALID_GANALYTICS_OPTIONS = [
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
1 change: 0 additions & 1 deletion sendgrid.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ Gem::Specification.new do |s|
s.add_development_dependency(%q<shoulda>, [">= 0"])
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
s.add_development_dependency(%q<jeweler>, ["~> 1.5.1"])
s.add_runtime_dependency(%q<json>, [">= 0"])
else
s.add_dependency(%q<json>, [">= 0"])
s.add_dependency(%q<shoulda>, [">= 0"])
Expand Down
24 changes: 24 additions & 0 deletions test/sendgrid_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
26 changes: 26 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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" })
Expand Down