Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Send email to asciicast author when someone comments

  • Loading branch information...
commit 3a7f462b139a5aa41b6f5f06e412034e6b677488 1 parent 0bb3e75
@sickill sickill authored
View
10 app/controllers/api/comments_controller.rb
@@ -13,7 +13,9 @@ def create
comment.asciicast = @asciicast
comment.user = current_user
- comment.save
+ if comment.save
+ notify_via_email(@asciicast.user, comment)
+ end
decorated_comment = CommentDecorator.new(comment)
respond_with decorated_comment, :location => api_comment_url(comment)
@@ -34,4 +36,10 @@ def destroy
def load_asciicast
@asciicast = Asciicast.find(params[:asciicast_id])
end
+
+ def notify_via_email(user, comment)
+ if user.email.present? && user != comment.user
+ UserMailer.new_comment_email(user, comment).deliver
+ end
+ end
end
View
13 app/mailers/user_mailer.rb
@@ -0,0 +1,13 @@
+class UserMailer < ActionMailer::Base
+ default from: "hello@ascii.io"
+
+ def new_comment_email(user, comment)
+ @comment = comment
+ @author = comment.user
+ @asciicast = AsciicastDecorator.new(@comment.asciicast)
+
+ to = "~#{user.nickname} <#{user.email}>"
+ subject = %(New comment for #{@asciicast.smart_title})
+ mail :to => to, :subject => subject
+ end
+end
View
8 app/views/user_mailer/new_comment_email.text.erb
@@ -0,0 +1,8 @@
+Howdy!
+
+~<%= @author.nickname %> (<%= profile_url(@author) %>) commented on your asciicast "<%= @asciicast.smart_title %>":
+
+ <%= @comment.body %>
+
+---
+http://ascii.io/
View
3  config/app.yml
@@ -13,6 +13,7 @@ defaults: &defaults
development:
<<: *defaults
+ email_host: "localhost:3000"
carrierwave_storage: :file
bugfix:
@@ -21,6 +22,7 @@ bugfix:
production:
<<: *defaults
+ email_host: "ascii.io"
carrierwave_storage: :fog
carrierwave_fog:
aws_access_key_id:
@@ -30,4 +32,5 @@ production:
test:
<<: *defaults
+ email_host: "example.org"
carrierwave_storage: :file
View
2  config/application.rb
@@ -52,6 +52,8 @@ class Application < Rails::Application
# Version of your assets, change this if you want to expire all your assets
config.assets.version = '1.0'
+
+ config.action_mailer.default_url_options = { :host => CFG.email_host }
end
end
View
2  config/environments/production.rb
@@ -45,6 +45,8 @@
# Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
# config.assets.precompile += %w( search.js )
+ config.action_mailer.delivery_method = :sendmail
+
# Disable delivery errors, bad email addresses will be ignored
# config.action_mailer.raise_delivery_errors = false
View
48 spec/controllers/api/comments_controller_spec.rb
@@ -3,7 +3,7 @@
describe Api::CommentsController do
let(:user) { Factory(:user) }
- let(:asciicast) { mock_model(Asciicast) }
+ let(:asciicast) { Factory(:asciicast) }
before do
login_as(user)
@@ -30,6 +30,11 @@ def dispatch
dispatch
response.status.should == 201
end
+
+ it "notifies asciicast author via email" do
+ @controller.should_receive(:notify_via_email)
+ dispatch
+ end
end
context "given not valid data" do
@@ -96,4 +101,45 @@ def dispatch
end
end
+
+ describe '#notify_via_email' do
+ let(:user) { stub_model(User) }
+ let(:comment) { stub_model(Comment) }
+
+ context 'when asciicast author has email' do
+ before do
+ user.email = "jolka@pamietasz.pl"
+ end
+
+ context 'and he is not comment author' do
+ before do
+ comment.user = stub_model(User)
+ end
+
+ it "sends email" do
+ mail = double('mail', :deliver => true)
+ UserMailer.should_receive(:new_comment_email).and_return(mail)
+ @controller.send(:notify_via_email, user, comment)
+ end
+ end
+
+ context 'and he is comment author' do
+ before do
+ comment.user = user
+ end
+
+ it "doesn't send email" do
+ UserMailer.should_not_receive(:new_comment_email)
+ @controller.send(:notify_via_email, user, comment)
+ end
+ end
+ end
+
+ context 'when asciicast author has no email' do
+ it "doesn't send email" do
+ UserMailer.should_not_receive(:new_comment_email)
+ @controller.send(:notify_via_email, user, comment)
+ end
+ end
+ end
end
View
5 spec/mailers/user_mailer_spec.rb
@@ -0,0 +1,5 @@
+require "spec_helper"
+
+describe UserMailer do
+ pending "add some examples to (or delete) #{__FILE__}"
+end
Please sign in to comment.
Something went wrong with that request. Please try again.