Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Allow unique args to be set at the class level #7

Merged
merged 2 commits into from

2 participants

@DanOlson

I found this to be useful because the app I'm integrating with SendGrid has around seven mailers. To set it up as DRYly as possible, I created one main mailer that the others inherit from. Inside that mailer, I'm able to call sendgrid_category, sendgrid_enable, and now sendgrid_unique_args through a custom method_missing. This way, I can dynamically set my unique_args without having to put sendgrid_unique_args in each mailer method. The existing method-level implementation still plays nicely with the new class method.

I've added tests, and made a change or two to get the existing test suite running and passing. Cheers on the gem! Thanks a lot!

Dan Olson added some commits
@stephenb stephenb merged commit 1384298 into stephenb:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 13, 2012
Commits on Jul 14, 2012
  1. Allows unique args to be set in both the mailer class and mailer meth…

    Dan Olson authored
    …ods. Includes tests. Also gets existing tests running and passing.
This page is out of date. Refresh to see the latest.
View
6 Gemfile
@@ -9,8 +9,12 @@ gem "json"
# Add dependencies to develop your gem here.
# Include everything needed to run rake, tests, features, etc.
group :development do
- # gem "shoulda", ">= 0"
+ gem "shoulda", ">= 0"
gem "bundler", "~> 1.0.0"
gem "jeweler", "~> 1.5.1"
# gem "rcov", ">= 0"
end
+
+group :test do
+ gem "actionmailer", ">= 2.3.2"
+end
View
49 Gemfile.lock
@@ -1,18 +1,67 @@
GEM
remote: http://rubygems.org/
specs:
+ actionmailer (3.1.5)
+ actionpack (= 3.1.5)
+ mail (~> 2.3.3)
+ actionpack (3.1.5)
+ activemodel (= 3.1.5)
+ activesupport (= 3.1.5)
+ builder (~> 3.0.0)
+ erubis (~> 2.7.0)
+ i18n (~> 0.6)
+ rack (~> 1.3.6)
+ rack-cache (~> 1.2)
+ rack-mount (~> 0.8.2)
+ rack-test (~> 0.6.1)
+ sprockets (~> 2.0.4)
+ activemodel (3.1.5)
+ activesupport (= 3.1.5)
+ builder (~> 3.0.0)
+ i18n (~> 0.6)
+ activesupport (3.1.5)
+ multi_json (>= 1.0, < 1.3)
+ builder (3.0.0)
+ erubis (2.7.0)
git (1.2.5)
+ hike (1.2.1)
+ i18n (0.6.0)
jeweler (1.5.2)
bundler (~> 1.0.0)
git (>= 1.2.5)
rake
json (1.5.1)
+ mail (2.3.3)
+ i18n (>= 0.4.0)
+ mime-types (~> 1.16)
+ treetop (~> 1.4.8)
+ mime-types (1.18)
+ multi_json (1.2.0)
+ polyglot (0.3.3)
+ rack (1.3.6)
+ rack-cache (1.2)
+ rack (>= 0.4)
+ rack-mount (0.8.3)
+ rack (>= 1.0.0)
+ rack-test (0.6.1)
+ rack (>= 1.0)
rake (0.8.7)
+ shoulda (2.10.3)
+ sprockets (2.0.4)
+ hike (~> 1.2)
+ rack (~> 1.0)
+ tilt (~> 1.1, != 1.3.0)
+ tilt (1.3.3)
+ treetop (1.4.10)
+ polyglot
+ polyglot (>= 0.3.1)
PLATFORMS
ruby
DEPENDENCIES
+ actionmailer (>= 2.3.2)
bundler (~> 1.0.0)
jeweler (~> 1.5.1)
json
+ shoulda
View
2  Rakefile
@@ -39,7 +39,7 @@ rescue LoadError
end
end
-task :test => :check_dependencies
+task :test
task :default => :test
View
22 lib/sendgrid.rb
@@ -17,7 +17,7 @@ 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_footer_text, :default_spamcheck_score, :default_sg_unique_args
end
attr_accessor :sg_category, :sg_options, :sg_disabled_options, :sg_recipients, :sg_substitutions, :subscriptiontrack_text, :footer_text, :spamcheck_score
end
@@ -59,6 +59,13 @@ def sendgrid_enable(*options)
options.each { |option| self.default_sg_options << option if VALID_OPTIONS.include?(option) }
end
+ # Sets unique args at the class level. Should be a hash
+ # of name, value pairs.
+ # { :some_unique_arg => "some_value"}
+ def sendgrid_unique_args(args)
+ self.default_sg_unique_args = args
+ 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
@@ -129,10 +136,11 @@ def sendgrid_spamcheck_maxscore(score)
end
# Call within mailer method to set unique args for this email.
+ # Merged with class-level unique args, if any exist.
def sendgrid_unique_args(args)
@sg_unique_args = args
end
-
+
# only override the appropriate methods for the current ActionMailer version
if ActionMailer::Base.respond_to?(:mail)
@@ -211,8 +219,13 @@ def sendgrid_json_headers(mail)
end
# Set unique_args
+ if self.class.default_sg_unique_args && !self.class.default_sg_unique_args.empty?
+ header_opts[:unique_args] = self.class.default_sg_unique_args
+ end
+
if @sg_unique_args && !@sg_unique_args.empty?
- header_opts[:unique_args] = @sg_unique_args
+ header_opts[:unique_args] ||= @sg_unique_args
+ header_opts[:unique_args].merge!(@sg_unique_args)
end
header_opts.to_json.gsub(/(["\]}])([,:])(["\[{])/, '\\1\\2 \\3')
@@ -264,5 +277,4 @@ def filters_hash_from_options(enabled_opts, disabled_opts)
return filters
end
-
-end
+end
View
18 test/sendgrid_test.rb
@@ -1,7 +1,11 @@
require 'test_helper'
class SendgridTest < Test::Unit::TestCase
- def setup
+ def setup
+ ActionMailer::Base.delivery_method = :test
+ ActionMailer::Base.perform_deliveries = true
+ ActionMailer::Base.deliveries = []
+
@options = {
:to => ['test@example.com'],
:from => 'test@example.com',
@@ -18,7 +22,17 @@ def setup
should "require the same number of items in a substitution array as is in the recipient array" do
assert_raise ArgumentError do
- test_email = SendgridCampaignTestMailer.create_test(@options)
+ test_email = SendgridCampaignTestMailer.create_test(@options).deliver
end
end
+
+ should "accept a hash of unique args at the class level" do
+ assert_equal ({ :test_arg => "test value" }), SendgridUniqueArgsMailer.default_sg_unique_args
+ end
+
+ should "pass unique args from both the mailer class and the mailer method through custom headers" do
+ SendgridUniqueArgsMailer.unique_args_test_email(@options).deliver
+ mail = ActionMailer::Base.deliveries.last
+ assert_match ({ :mailer_method_unique_arg => "some value", :test_arg => "test value" }.to_json.gsub(/(["\]}])([,:])(["\[{])/, '\\1\\2 \\3')), mail.header['X-SMTPAPI'].value
+ end
end
View
24 test/test_helper.rb
@@ -4,11 +4,8 @@
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
$LOAD_PATH.unshift(File.dirname(__FILE__))
-require 'sendgrid'
require 'action_mailer'
-
-class Test::Unit::TestCase
-end
+require 'sendgrid'
class SendgridCampaignTestMailer < ActionMailer::Base
include SendGrid
@@ -26,13 +23,9 @@ class SendgridCampaignTestMailer < ActionMailer::Base
# array must match the :to array
# :category the sendgrid category used for tracking
# :html_content, :text_content, :subject
- def test(options)
+ def create_test(options)
handle_sendgrid_options(options)
- recipients(options[:to])
- from(options[:from])
- reply_to(options[:reply_to])
- subject(options[:subject])
- body(options[:html_content])
+ mail(options)
end
protected
@@ -49,7 +42,16 @@ def handle_sendgrid_options(options)
end
end
-
sendgrid_category(options[:category])
end
end
+
+class SendgridUniqueArgsMailer < ActionMailer::Base
+ include SendGrid
+ sendgrid_unique_args({ :test_arg => "test value" })
+
+ def unique_args_test_email(options)
+ sendgrid_unique_args({ :mailer_method_unique_arg => "some value" })
+ mail(options)
+ end
+end
Something went wrong with that request. Please try again.