Commit ebdd7a9

mo khan <mo@mokhan.ca>
2015-01-10 17:33:58
send a registration email when a user signs up.
1 parent 066c1e1
app/controllers/registrations_controller.rb
@@ -7,6 +7,7 @@ class RegistrationsController < ApplicationController
     @user = User.new(secure_params)
     if @user.save
       log_in(@user)
+      UserMailer.registration_email(@user).deliver_later
       redirect_to dashboard_path
     else
       flash.now[:error] = @user.errors.full_messages
app/mailers/.keep
app/mailers/application_mailer.rb
@@ -0,0 +1,4 @@
+class ApplicationMailer < ActionMailer::Base
+  default from: "from@example.com"
+  layout 'mailer'
+end
app/mailers/user_mailer.rb
@@ -0,0 +1,4 @@
+class UserMailer < ApplicationMailer
+  def registration_email(user)
+  end
+end
app/views/layouts/mailer.html.erb
@@ -0,0 +1,5 @@
+<html>
+  <body>
+    <%= yield %>
+  </body>
+</html>
app/views/layouts/mailer.text.erb
@@ -0,0 +1,1 @@
+<%= yield %>
spec/controllers/registrations_controller_spec.rb
@@ -14,7 +14,10 @@ describe RegistrationsController do
     let(:email) { 'email@example.com' }
 
     context "when valid params are provided" do
+      let(:mailer) { double(deliver_later: true) }
+
       before :each do
+        allow(UserMailer).to receive(:registration_email).and_return(mailer)
         post :create, user: { username: username, password: password, email: email, terms_and_conditions: true }
       end
 
@@ -36,6 +39,10 @@ describe RegistrationsController do
       it 'does not display any errors' do
         expect(flash[:error]).to be_nil
       end
+
+      it 'sends a user registration email' do
+        expect(mailer).to have_received(:deliver_later)
+      end
     end
 
     context "when the parameters provided are invalid" do
spec/mailers/user_mailer_spec.rb
@@ -0,0 +1,5 @@
+require "rails_helper"
+
+RSpec.describe UserMailer, :type => :mailer do
+  pending "add some examples to (or delete) #{__FILE__}"
+end