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?
user.setup_reset
UserMailer.password_reset(user).deliver_now
UserMailer.password_reset(user).deliver_later
redirect_to admin_reset_request_path,
success: "Reset request sent! Please check your email for instructions."
end

View File

@ -14,7 +14,7 @@ module Admin
@user = User.create({ password: default_passwd }.merge(user_params.to_h))
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}" }
else
flash[:error] = "Failed to save user."

View File

@ -26,14 +26,23 @@ class QuizController < ApplicationController
def submit_summary
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 :thankyou and return if current_candidate.complete!
redirect_to :summary, flash: { error: record_error }
redirect_to :thankyou and return if complete_and_email
redirect_to :summary, flash: { error: 'Sorry, there was a problem. Please try again.' }
end
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
@question = current_candidate.fetch_question(qid)
end

View File

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

View File

@ -40,19 +40,6 @@ class Candidate < ApplicationRecord
"#{my_status.progress}%"
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
def generate_test_hash

View File

@ -3,6 +3,8 @@ require 'test_helper'
module Admin
class AuthControllerTest < ActionDispatch::IntegrationTest
include ActiveJob::TestHelper
test "should get login" do
get admin_login_url
assert_response :success
@ -45,7 +47,7 @@ module Admin
test "should process a reset request" do
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 } }
end
refute_equal user.reset_token, User.find(user.id).reset_token

View File

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

View File

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

View File

@ -17,11 +17,6 @@ class QuizControllerTest < ActionDispatch::IntegrationTest
assert_redirected_to login_path
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
setup_auth candidates(:dawn)
qid = questions(:fed5).id

View File

@ -2,6 +2,8 @@
require 'test_helper'
class RecruiterControllerTest < ActionDispatch::IntegrationTest
include ActiveJob::TestHelper
def setup_auth
post recruiter_auth_url, params: { auth:
{ email: 'pdr.recruiter@mailinator.com', password: 'password' } }
@ -68,7 +70,7 @@ class RecruiterControllerTest < ActionDispatch::IntegrationTest
test "should create new candidate" do
setup_auth
assert_difference("ActionMailer::Base.deliveries.size", 2) do
assert_enqueued_jobs 2 do
assert_difference("Candidate.count") do
post create_candidate_path, params: { candidate:
{ 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
setup_auth
assert_difference("ActionMailer::Base.deliveries.size", 0) do
assert_enqueued_jobs 0 do
assert_difference("Candidate.count", 0) do
post create_candidate_path, params: { candidate:
{ 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
setup_auth
assert_difference("ActionMailer::Base.deliveries.size", 0) do
assert_enqueued_jobs 0 do
assert_difference("Candidate.count", 0) do
post create_candidate_path, params: { candidate:
{ name: 'new name', email: "", experience: '0-3', quiz_id: quizzes(:fed).id } }