From 3a7f462b139a5aa41b6f5f06e412034e6b677488 Mon Sep 17 00:00:00 2001 From: Marcin Kulik Date: Thu, 12 Apr 2012 15:49:20 +0200 Subject: [PATCH] Send email to asciicast author when someone comments --- app/controllers/api/comments_controller.rb | 10 +++- app/mailers/user_mailer.rb | 13 +++++ .../user_mailer/new_comment_email.text.erb | 8 ++++ config/app.yml | 3 ++ config/application.rb | 2 + config/environments/production.rb | 2 + .../api/comments_controller_spec.rb | 48 ++++++++++++++++++- spec/mailers/user_mailer_spec.rb | 5 ++ 8 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 app/mailers/user_mailer.rb create mode 100644 app/views/user_mailer/new_comment_email.text.erb create mode 100644 spec/mailers/user_mailer_spec.rb diff --git a/app/controllers/api/comments_controller.rb b/app/controllers/api/comments_controller.rb index 15cf163..ab6ec89 100644 --- a/app/controllers/api/comments_controller.rb +++ b/app/controllers/api/comments_controller.rb @@ -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 diff --git a/app/mailers/user_mailer.rb b/app/mailers/user_mailer.rb new file mode 100644 index 0000000..fbdd0cb --- /dev/null +++ b/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 diff --git a/app/views/user_mailer/new_comment_email.text.erb b/app/views/user_mailer/new_comment_email.text.erb new file mode 100644 index 0000000..559cb66 --- /dev/null +++ b/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/ diff --git a/config/app.yml b/config/app.yml index b18855a..2a78409 100644 --- a/config/app.yml +++ b/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 diff --git a/config/application.rb b/config/application.rb index b11b261..58e30a9 100644 --- a/config/application.rb +++ b/config/application.rb @@ -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 diff --git a/config/environments/production.rb b/config/environments/production.rb index 09d6227..7198dd0 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -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 diff --git a/spec/controllers/api/comments_controller_spec.rb b/spec/controllers/api/comments_controller_spec.rb index 06dd221..c2fecd4 100644 --- a/spec/controllers/api/comments_controller_spec.rb +++ b/spec/controllers/api/comments_controller_spec.rb @@ -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 diff --git a/spec/mailers/user_mailer_spec.rb b/spec/mailers/user_mailer_spec.rb new file mode 100644 index 0000000..8d004b3 --- /dev/null +++ b/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