deliver_later for controller actions.

This commit is contained in:
Mark Moser 2016-09-14 14:38:26 -05:00
parent d54b99135e
commit 9db007489f
10 changed files with 31 additions and 32 deletions

View File

@ -31,7 +31,7 @@ module Admin
redirect_to(admin_reset_request_path) and return if user.nil? redirect_to(admin_reset_request_path) and return if user.nil?
user.setup_reset user.setup_reset
UserMailer.password_reset(user).deliver_now UserMailer.password_reset(user).deliver_later
redirect_to admin_reset_request_path, redirect_to admin_reset_request_path,
success: "Reset request sent! Please check your email for instructions." success: "Reset request sent! Please check your email for instructions."
end end

View File

@ -14,7 +14,7 @@ module Admin
@user = User.create({ password: default_passwd }.merge(user_params.to_h)) @user = User.create({ password: default_passwd }.merge(user_params.to_h))
if @user.persisted? if @user.persisted?
UserMailer.welcome(@user, default_passwd).deliver_now UserMailer.welcome(@user, default_passwd).deliver_later
redirect_to admin_users_path, flash: { success: "Sucessfully created user #{@user.name}" } redirect_to admin_users_path, flash: { success: "Sucessfully created user #{@user.name}" }
else else
flash[:error] = "Failed to save user." flash[:error] = "Failed to save user."

View File

@ -26,14 +26,23 @@ class QuizController < ApplicationController
def submit_summary def submit_summary
not_completed_error = 'You must complete all questions to submit your test.' not_completed_error = 'You must complete all questions to submit your test.'
record_error = 'There was a problem with your submission. Please try again later.'
redirect_to :summary, flash: { error: not_completed_error } and return unless prep_status.can_submit redirect_to :summary, flash: { error: not_completed_error } and return unless prep_status.can_submit
redirect_to :thankyou and return if current_candidate.complete! redirect_to :thankyou and return if complete_and_email
redirect_to :summary, flash: { error: record_error } redirect_to :summary, flash: { error: 'Sorry, there was a problem. Please try again.' }
end end
private private
def complete_and_email
if current_candidate.update_attributes(completed: true)
CandidateMailer.submitted(current_candidate).deliver_later
RecruiterMailer.candidate_submitted(current_candidate).deliver_later
ReviewerMailer.candidate_submission(current_candidate).deliver_later
return true
end
false
end
def prep_question qid def prep_question qid
@question = current_candidate.fetch_question(qid) @question = current_candidate.fetch_question(qid)
end end

View File

@ -15,8 +15,8 @@ class RecruiterController < ApplicationController
@candidate = Candidate.create(candidate_params.merge(recruiter_id: current_recruiter.id)) @candidate = Candidate.create(candidate_params.merge(recruiter_id: current_recruiter.id))
if @candidate.persisted? if @candidate.persisted?
CandidateMailer.welcome(@candidate).deliver_now CandidateMailer.welcome(@candidate).deliver_later
RecruiterMailer.candidate_created(@candidate).deliver_now RecruiterMailer.candidate_created(@candidate).deliver_later
redirect_to recruiter_path, flash: { success: "Sucessfully created candidate #{@candidate.name}" } redirect_to recruiter_path, flash: { success: "Sucessfully created candidate #{@candidate.name}" }
else else
flash[:error] = "Failed to save candidate." flash[:error] = "Failed to save candidate."

View File

@ -40,19 +40,6 @@ class Candidate < ApplicationRecord
"#{my_status.progress}%" "#{my_status.progress}%"
end end
# FIXME: This feels wrong here. Mail deliveries should be in controller.
# Privatize in QuizController
# also, bang methods in ruby do an action and replace, or return nil
def complete!
if update_attributes(completed: true)
CandidateMailer.submitted(self).deliver_now
RecruiterMailer.candidate_submitted(self).deliver_now
ReviewerMailer.candidate_submission(self).deliver_now
return true
end
false
end
private private
def generate_test_hash def generate_test_hash

View File

@ -3,6 +3,8 @@ require 'test_helper'
module Admin module Admin
class AuthControllerTest < ActionDispatch::IntegrationTest class AuthControllerTest < ActionDispatch::IntegrationTest
include ActiveJob::TestHelper
test "should get login" do test "should get login" do
get admin_login_url get admin_login_url
assert_response :success assert_response :success
@ -45,7 +47,7 @@ module Admin
test "should process a reset request" do test "should process a reset request" do
user = users(:admin) user = users(:admin)
assert_difference("ActionMailer::Base.deliveries.size", 1) do assert_enqueued_jobs 1 do
post admin_send_reset_url, params: { auth: { email: user.email } } post admin_send_reset_url, params: { auth: { email: user.email } }
end end
refute_equal user.reset_token, User.find(user.id).reset_token refute_equal user.reset_token, User.find(user.id).reset_token

View File

@ -3,6 +3,8 @@ require 'test_helper'
module Admin module Admin
class UserControllerTest < ActionDispatch::IntegrationTest class UserControllerTest < ActionDispatch::IntegrationTest
include ActiveJob::TestHelper
def setup def setup
post admin_auth_url, params: { auth: post admin_auth_url, params: { auth:
{ email: 'alan.admin@mailinator.com', password: 'password' } } { email: 'alan.admin@mailinator.com', password: 'password' } }
@ -29,7 +31,7 @@ module Admin
end end
test "should post create" do test "should post create" do
assert_difference("ActionMailer::Base.deliveries.size", 1) do assert_enqueued_jobs 1 do
assert_difference("User.count", 1) do assert_difference("User.count", 1) do
post admin_create_user_url, params: { user: post admin_create_user_url, params: { user:
{ email: 'new.user@mailinator.com', name: 'New User', role: 'reviewer' } } { email: 'new.user@mailinator.com', name: 'New User', role: 'reviewer' } }

View File

@ -2,6 +2,8 @@
require 'test_helper' require 'test_helper'
class CandidateControllerTest < ActionDispatch::IntegrationTest class CandidateControllerTest < ActionDispatch::IntegrationTest
include ActiveJob::TestHelper
def setup_auth candidate def setup_auth candidate
post validate_candidate_url, params: { test_id: candidate.test_hash } post validate_candidate_url, params: { test_id: candidate.test_hash }
end end
@ -74,7 +76,7 @@ class CandidateControllerTest < ActionDispatch::IntegrationTest
test "should NOT send mailers on submission" do test "should NOT send mailers on submission" do
setup_auth candidates(:dawn) setup_auth candidates(:dawn)
assert_difference("ActionMailer::Base.deliveries.size", 0) do assert_enqueued_jobs 0 do
post post_summary_path post post_summary_path
end end
assert_redirected_to summary_path assert_redirected_to summary_path
@ -84,7 +86,7 @@ class CandidateControllerTest < ActionDispatch::IntegrationTest
test "should send mailers on submission" do test "should send mailers on submission" do
setup_auth candidates(:peggy) setup_auth candidates(:peggy)
assert_difference("ActionMailer::Base.deliveries.size", 3) do assert_enqueued_jobs 3 do
post post_summary_path post post_summary_path
end end
assert_redirected_to thankyou_path assert_redirected_to thankyou_path

View File

@ -17,11 +17,6 @@ class QuizControllerTest < ActionDispatch::IntegrationTest
assert_redirected_to login_path assert_redirected_to login_path
end end
# TODO: test models/candidate.complete! == false
# test 'should gracefully fail on summary submit' do
# # if the mailers fail, should go to thank you still
# end
test "should redirect to saved on save" do test "should redirect to saved on save" do
setup_auth candidates(:dawn) setup_auth candidates(:dawn)
qid = questions(:fed5).id qid = questions(:fed5).id

View File

@ -2,6 +2,8 @@
require 'test_helper' require 'test_helper'
class RecruiterControllerTest < ActionDispatch::IntegrationTest class RecruiterControllerTest < ActionDispatch::IntegrationTest
include ActiveJob::TestHelper
def setup_auth def setup_auth
post recruiter_auth_url, params: { auth: post recruiter_auth_url, params: { auth:
{ email: 'pdr.recruiter@mailinator.com', password: 'password' } } { email: 'pdr.recruiter@mailinator.com', password: 'password' } }
@ -68,7 +70,7 @@ class RecruiterControllerTest < ActionDispatch::IntegrationTest
test "should create new candidate" do test "should create new candidate" do
setup_auth setup_auth
assert_difference("ActionMailer::Base.deliveries.size", 2) do assert_enqueued_jobs 2 do
assert_difference("Candidate.count") do assert_difference("Candidate.count") do
post create_candidate_path, params: { candidate: post create_candidate_path, params: { candidate:
{ name: 'new name', email: 'test@mailinator.com', experience: '0-3', quiz_id: quizzes(:fed).id } } { name: 'new name', email: 'test@mailinator.com', experience: '0-3', quiz_id: quizzes(:fed).id } }
@ -81,7 +83,7 @@ class RecruiterControllerTest < ActionDispatch::IntegrationTest
test "should fail creation with improper email format" do test "should fail creation with improper email format" do
setup_auth setup_auth
assert_difference("ActionMailer::Base.deliveries.size", 0) do assert_enqueued_jobs 0 do
assert_difference("Candidate.count", 0) do assert_difference("Candidate.count", 0) do
post create_candidate_path, params: { candidate: post create_candidate_path, params: { candidate:
{ name: 'new name', email: 'test@mailinatorcom', experience: '0-3', quiz_id: quizzes(:fed).id } } { name: 'new name', email: 'test@mailinatorcom', experience: '0-3', quiz_id: quizzes(:fed).id } }
@ -95,7 +97,7 @@ class RecruiterControllerTest < ActionDispatch::IntegrationTest
test "should fail creation gracefully with empty email" do test "should fail creation gracefully with empty email" do
setup_auth setup_auth
assert_difference("ActionMailer::Base.deliveries.size", 0) do assert_enqueued_jobs 0 do
assert_difference("Candidate.count", 0) do assert_difference("Candidate.count", 0) do
post create_candidate_path, params: { candidate: post create_candidate_path, params: { candidate:
{ name: 'new name', email: "", experience: '0-3', quiz_id: quizzes(:fed).id } } { name: 'new name', email: "", experience: '0-3', quiz_id: quizzes(:fed).id } }