Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tokenized emails #165

Merged
merged 3 commits into from
Aug 29, 2013
Merged
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
8 changes: 4 additions & 4 deletions app/mailers/broadcast_mailer.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion app/models/article_link.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
9 changes: 6 additions & 3 deletions app/models/broadcaster.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
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, :share_token)
.new(params[:to], "testtoken")

BroadcastMailer.broadcast(params, subscriber).deliver
end
end
8 changes: 4 additions & 4 deletions test/unit/mailers/broadcast_mailer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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}"

Expand Down