From 17759c491683a24ff033ebfcf9f5e8f3483341ca Mon Sep 17 00:00:00 2001 From: maxschulze Date: Wed, 20 Aug 2014 00:06:41 +0200 Subject: [PATCH] Update to allow to use Sendgrid Templates You can now use `sendgrid_template = 'ID'` on class level to set the default template --- lib/sendgrid.rb | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/sendgrid.rb b/lib/sendgrid.rb index 688493d..c2098f6 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 = [ @@ -25,7 +26,8 @@ def self.included(base) base.class_eval do class << self attr_accessor :default_sg_category, :default_sg_options, :default_subscriptiontrack_text, - :default_footer_text, :default_spamcheck_score, :default_sg_unique_args + :default_footer_text, :default_spamcheck_score, :default_sg_unique_args, + :default_sg_template end attr_accessor :sg_category, :sg_options, :sg_disabled_options, :sg_recipients, :sg_substitutions, :subscriptiontrack_text, :footer_text, :spamcheck_score, :sg_unique_args @@ -51,6 +53,12 @@ module ClassMethods def sendgrid_category(category) self.default_sg_category = category end + + # Set a default template for all mails + def sendgrid_template(template_id) + self.default_sg_template = template_id + self.sendgrid_enable(:templates) + end # Enables a default option for all emails. # See documentation for details. @@ -63,6 +71,7 @@ def sendgrid_category(category) # * :subscriptiontrack # * :footer # * :spamcheck + # * :templates 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) } @@ -254,6 +263,10 @@ def filters_hash_from_options(enabled_opts, disabled_opts) enabled_opts.each do |opt| filters[opt] = {'settings' => {'enable' => 1}} case opt.to_sym + when :templates + if self.class.default_sg_template + filters[:templates]['settings']['template_id'] = self.class.default_sg_template + end when :subscriptiontrack if @subscriptiontrack_text if @subscriptiontrack_text[:replace]