From 751251c0501010ada176cbb2251a7d3ab57c8643 Mon Sep 17 00:00:00 2001 From: Gregory Brown Date: Fri, 23 Aug 2013 18:03:35 -0400 Subject: [PATCH 1/2] Add token to broadcast mailer --- app/mailers/broadcast_mailer.rb | 8 ++++---- app/models/article_link.rb | 2 +- app/models/broadcaster.rb | 8 +++++--- test/unit/mailers/broadcast_mailer_test.rb | 8 ++++---- 4 files changed, 14 insertions(+), 12 deletions(-) diff --git a/app/mailers/broadcast_mailer.rb b/app/mailers/broadcast_mailer.rb index fd98630..e5a95b3 100644 --- a/app/mailers/broadcast_mailer.rb +++ b/app/mailers/broadcast_mailer.rb @@ -1,14 +1,14 @@ class BroadcastMailer < ActionMailer::Base def self.recipients - User.where(:notify_updates => true).to_notify.map(&:contact_email) + User.where(:notify_updates => true).to_notify end - def broadcast(message, email) - article_finder = ->(e) { article_url(Article[e]) } + def broadcast(message, subscriber) + article_finder = ->(e) { ArticleLink.new(Article[e]).url(subscriber.share_token) } @body = Mustache.render(message[:body], :article => article_finder) - mail(:to => email, + mail(:to => subscriber.contact_email, :subject => message[:subject]) end end diff --git a/app/models/article_link.rb b/app/models/article_link.rb index 18122c1..b683d2a 100644 --- a/app/models/article_link.rb +++ b/app/models/article_link.rb @@ -1,7 +1,7 @@ class ArticleLink include Rails.application.routes.url_helpers - def initialize(article, params) + def initialize(article, params={}) self.article = article self.params = params end diff --git a/app/models/broadcaster.rb b/app/models/broadcaster.rb index 129d163..b1f41e7 100644 --- a/app/models/broadcaster.rb +++ b/app/models/broadcaster.rb @@ -1,11 +1,13 @@ class Broadcaster def self.notify_subscribers(params) - BroadcastMailer.recipients.each do |email| - BroadcastMailer.broadcast(params, email).deliver + BroadcastMailer.recipients.each do |subscriber| + BroadcastMailer.broadcast(params, subscriber).deliver end end def self.notify_testers(params) - BroadcastMailer.broadcast(params, params[:to]).deliver + subscriber = Struct.new(:contact_email) + + BroadcastMailer.broadcast(params, subscriber.new(params[:to])).deliver end end diff --git a/test/unit/mailers/broadcast_mailer_test.rb b/test/unit/mailers/broadcast_mailer_test.rb index 3277095..c4ae531 100644 --- a/test/unit/mailers/broadcast_mailer_test.rb +++ b/test/unit/mailers/broadcast_mailer_test.rb @@ -16,14 +16,14 @@ class BroadcastMailerTest < ActionMailer::TestCase assert_equal 5, messages.count do_not_mail.each do |user| - refute messages.any? { |m| m.to.include?(user.contact_email) }, - "User with status '#{user.status}' was sent a broadcast" + refute messages.any? { |m| m.to.include?(user.contact_email) }, + "User with status '#{user.status}' was sent a broadcast" end end test "article links can be generated from template" do - FactoryGirl.create(:user) + user = FactoryGirl.create(:user) article = FactoryGirl.create(:article, :slug => "a-fancy-article") @@ -33,7 +33,7 @@ class BroadcastMailerTest < ActionMailer::TestCase message = ActionMailer::Base.deliveries.first - url = Rails.application.routes.url_helpers.article_url(article) + url = ArticleLink.new(article).url(user.share_token) expected_body = "Here's an amazing article\n#{url}" From f673b9f7e70d6ce12c9da182cbb475750e479172 Mon Sep 17 00:00:00 2001 From: Gregory Brown Date: Sat, 24 Aug 2013 12:31:00 -0400 Subject: [PATCH 2/2] Need to include a token for testers --- app/models/broadcaster.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/models/broadcaster.rb b/app/models/broadcaster.rb index b1f41e7..0b4dab0 100644 --- a/app/models/broadcaster.rb +++ b/app/models/broadcaster.rb @@ -6,8 +6,9 @@ def self.notify_subscribers(params) end def self.notify_testers(params) - subscriber = Struct.new(:contact_email) + subscriber = Struct.new(:contact_email, :share_token) + .new(params[:to], "testtoken") - BroadcastMailer.broadcast(params, subscriber.new(params[:to])).deliver + BroadcastMailer.broadcast(params, subscriber).deliver end end