Skip to content

Commit

Permalink
email author when responding to their comment - closes ryanb#30
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanb committed Jul 25, 2011
1 parent 40312b6 commit 5349b33
Show file tree
Hide file tree
Showing 14 changed files with 118 additions and 4 deletions.
1 change: 1 addition & 0 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def create
if @comment.errors.present?
render :new
else
@comment.notify_other_commenters
redirect_to(episode_path(@comment.episode, :view => "comments"))
end
end
Expand Down
5 changes: 5 additions & 0 deletions app/mailers/mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,9 @@ def feedback(message)
@message = message
mail :to => "feedback@railscasts.com", :from => @message.email, :subject => "RailsCasts Feedback from #{@message.name}"
end

def comment_response(comment, user)
@comment = comment
mail :to => user.email, :from => "noreply@railscasts.com", :subject => "Comment Response on RailsCasts"
end
end
10 changes: 10 additions & 0 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,14 @@ def request=(request)
self.user_agent = request.env['HTTP_USER_AGENT']
self.referrer = request.env['HTTP_REFERER']
end

def notify_other_commenters
users_to_notify.each do |user|
Mailer.comment_response(self, user).deliver
end
end

def users_to_notify
ancestors.map(&:user).compact.select { |u| u.email.present? && u != user }
end
end
9 changes: 9 additions & 0 deletions app/views/mailer/comment_response.text.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Someone has responded to your comment on RailsCasts. Here is the response.

---
From: <%= raw @comment.user.name %>
<%= raw @comment.content %>
---

To view the full comment: <%= episode_url(@comment.episode, :view => "comments") %>
2 changes: 2 additions & 0 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,7 @@

# Only use best-standards-support built into browsers
config.action_dispatch.best_standards_support = :builtin

config.action_mailer.default_url_options = { :host => "railscasts.dev" }
end

2 changes: 2 additions & 0 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,6 @@

config.action_mailer.delivery_method = :sendmail
config.middleware.use ExceptionNotifier, :email_prefix => "[ERROR] ", :sender_address => 'noreply@railscasts.com', :exception_recipients => "ryan@railscasts.com"

config.action_mailer.default_url_options = { :host => "railscasts.com" }
end
2 changes: 2 additions & 0 deletions config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@

# Print deprecation notices to the stderr
config.active_support.deprecation = :stderr

config.action_mailer.default_url_options = { :host => "www.example.com" }
end
11 changes: 8 additions & 3 deletions spec/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,20 @@
end

Factory.define :comment do |f|
f.name 'Foo'
f.email 'foo@example.com'
f.site_url 'example.com'
f.content 'Hello world.'
f.episode { |c| c.association(:episode) }
f.user { |c| c.association(:user) }
end

Factory.define :user do |f|
f.name "Foo Bar"
f.sequence(:github_username) { |n| "foo#{n}" }
f.sequence(:github_uid) { |n| n }
f.sequence(:email) { |n| "foo#{n}@example.com" }
end

Factory.define :feedback_message do |f|
f.name "Foo Bar"
f.content "Hello World"
f.sequence(:email) { |n| "foo#{n}@example.com" }
end
29 changes: 29 additions & 0 deletions spec/mailers/mailer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require "spec_helper"

describe Mailer do
describe "feedback" do
let(:message) { Factory(:feedback_message) }
let(:mail) { Mailer.feedback(message) }

it "includes message with name and email" do
mail.subject.should eq("RailsCasts Feedback from #{message.name}")
mail.to.should eq(["feedback@railscasts.com"])
mail.from.should eq([message.email])
mail.body.encoded.should match(message.content)
end
end

describe "comment_response" do
let(:user) { Factory(:user) }
let(:comment) { Factory(:comment) }
let(:mail) { Mailer.comment_response(comment, user) }

it "includes comment content and link to comment page" do
mail.subject.should eq("Comment Response on RailsCasts")
mail.to.should eq([user.email])
mail.from.should eq(["noreply@railscasts.com"])
mail.body.encoded.should include(comment.content)
mail.body.encoded.should include(episode_url(comment.episode, :view => "comments"))
end
end
end
17 changes: 17 additions & 0 deletions spec/models/comment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,21 @@
c2 = Factory(:comment, :created_at => Time.now)
Comment.recent.should == [c2, c1]
end

it "should notify owners of all previous commenters except self" do
c1 = Factory(:comment)
c2a = Factory(:comment, :parent => c1)
c2b = Factory(:comment, :parent => c1)
c3 = Factory(:comment, :parent => c2a, :user => c2a.user)
c3.notify_other_commenters
email_count.should eq(1)
last_email.to.should include(c1.user.email)
end

it "should not notify users which don't have an email or comments which don't have user" do
c1 = Factory(:comment, :user => nil)
c2 = Factory(:comment, :parent => c1, :user => Factory(:user, :email => ""))
c3 = Factory(:comment, :parent => c2)
c3.users_to_notify.should eq([])
end
end
11 changes: 11 additions & 0 deletions spec/requests/comments_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@
page.should have_content("Hello back.")
end

it "send email to original authors when replying to comment" do
comment = Factory(:comment)
login
visit episode_path(comment.episode, :view => "comments")
click_on "Reply"
fill_in "comment_content", :with => "Hello back."
click_on "Post Comment"
page.should have_content("Hello back.")
last_email.to.should include(comment.user.email)
end

it "creates when banned" do
login Factory(:user, :banned_at => Time.now)
visit episode_path(Factory(:episode), :view => "comments")
Expand Down
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@

config.before(:each) do
FakeWeb.clean_registry
reset_email
if example.metadata[:js]
DatabaseCleaner.strategy = :truncation
else
Expand All @@ -66,6 +67,7 @@
end

config.include AuthMacros
config.include MailerMacros
end


Expand Down
8 changes: 7 additions & 1 deletion spec/support/auth_macros.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
module AuthMacros
def login(user)
def login(user = nil)
user ||= Factory(:user)
OmniAuth.config.add_mock(:github, "uid" => user.github_uid)
visit login_path
@_current_user = user
end

def current_user
@_current_user
end
end
13 changes: 13 additions & 0 deletions spec/support/mailer_macros.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module MailerMacros
def last_email
ActionMailer::Base.deliveries.last
end

def reset_email
ActionMailer::Base.deliveries = []
end

def email_count
ActionMailer::Base.deliveries.size
end
end

0 comments on commit 5349b33

Please sign in to comment.