Send email to asciicast author when someone comments

This commit is contained in:
Marcin Kulik 2012-04-12 15:49:20 +02:00
parent 0bb3e75fb6
commit 3a7f462b13
8 changed files with 89 additions and 2 deletions

View File

@ -13,7 +13,9 @@ class Api::CommentsController < ApplicationController
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 @@ class Api::CommentsController < ApplicationController
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 File

@ -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 File

@ -0,0 +1,8 @@
Howdy!
~<%= @author.nickname %> (<%= profile_url(@author) %>) commented on your asciicast "<%= @asciicast.smart_title %>":
<%= @comment.body %>
---
http://ascii.io/

View File

@ -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 File

@ -52,6 +52,8 @@ module AsciiIo
# 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 File

@ -45,6 +45,8 @@ AsciiIo::Application.configure do
# 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 File

@ -3,7 +3,7 @@ require 'spec_helper'
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 @@ describe Api::CommentsController do
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 @@ describe Api::CommentsController do
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 File

@ -0,0 +1,5 @@
require "spec_helper"
describe UserMailer do
pending "add some examples to (or delete) #{__FILE__}"
end