Skip to content

Commit 878570e

Browse files
committed
Notify admin users when new users register
1 parent 37d553a commit 878570e

File tree

10 files changed

+83
-3
lines changed

10 files changed

+83
-3
lines changed

app/controllers/users/registrations_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def create
1616
create_user_params = params.require(:user).permit(:email, :password, :password_confirmation)
1717
@user = User.new(create_user_params)
1818
if @user.save
19-
EmailConfirmationNotifier.deliver_to(@user)
19+
NewUserNotificationJob.perform_later(@user)
2020
redirect_to thanks_users_registration_path, notice: "Welcome to Joy of Rails! Please check your email for confirmation instructions"
2121
else
2222
render Users::Registrations::NewView.new(user: @user), status: :unprocessable_entity

app/jobs/new_user_notification_job.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class NewUserNotificationJob < ApplicationJob
2+
queue_as :default
3+
4+
def perform(user)
5+
# Do something later
6+
NewUserNotifier.deliver_to(AdminUser.all, user: user)
7+
EmailConfirmationNotifier.deliver_to(user)
8+
end
9+
end
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Emails::AdminUserMailer < ApplicationMailer
2+
def new_user(admin_user:, user:)
3+
@admin_user = admin_user
4+
@user = user
5+
6+
mail to: @admin_user.email, subject: "New Joy of Rails User"
7+
end
8+
end

app/notifiers/new_user_notifier.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class NewUserNotifier < NotificationEvent
2+
def self.deliver_to(admin_user, user:, **)
3+
new(params: {user_id: user.id}).deliver(admin_user, **)
4+
end
5+
6+
def deliver_notification(notification)
7+
admin_user = notification.recipient
8+
user = User.find(params[:user_id])
9+
10+
Emails::AdminUserMailer.new_user(admin_user: admin_user, user: user).deliver_later
11+
end
12+
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<h2>New User</h2>
2+
3+
<%= mail_to @user.confirmable_email %> just registered for Joy of Rails!
4+
5+
Woo hoo!
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
New User
2+
3+
<%= @user.confirmable_email %> just registered for Joy of Rails!
4+
5+
Woo hoo!
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
require "rails_helper"
2+
3+
RSpec.describe Emails::AdminUserMailer, type: :mailer do
4+
describe "new_user" do
5+
let(:admin_user) { instance_double(AdminUser, email: "[email protected]") }
6+
let(:user) { instance_double(User, confirmable_email: "[email protected]") }
7+
let(:mail) { Emails::AdminUserMailer.new_user(admin_user:, user:) }
8+
9+
it "renders the headers" do
10+
expect(mail.subject).to eq("New Joy of Rails User")
11+
expect(mail.to).to eq(["[email protected]"])
12+
expect(mail.from).to eq(["[email protected]"])
13+
end
14+
15+
it "renders the body" do
16+
expect(mail.body.encoded).to match("[email protected]")
17+
expect(mail.body.encoded).to match("just registered for Joy of Rails!")
18+
end
19+
end
20+
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Preview all emails at http://localhost:3000/rails/mailers/admin_user
2+
class Emails::AdminUserMailerPreview < ActionMailer::Preview
3+
def new_user
4+
Emails::AdminUserMailer.new_user(admin_user: FactoryBot.build(:admin_user), user: FactoryBot.build(:user))
5+
end
6+
end

spec/requests/users/registrations_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,20 @@
4343
expect(mail.subject).to eq "Confirm your email address"
4444
end
4545

46+
it "notifies admin" do
47+
email = FactoryBot.generate(:email)
48+
admin = FactoryBot.create(:admin_user)
49+
50+
post users_registration_path,
51+
params: {user: {email: email, password: "password", password_confirmation: "password"}}
52+
53+
perform_enqueued_jobs_and_subsequently_enqueued_jobs
54+
55+
mail = find_mail_to(admin.email)
56+
57+
expect(mail.subject).to eq "New Joy of Rails User"
58+
end
59+
4660
it "disallows a user to subscribe with existing email" do
4761
user = FactoryBot.create(:user)
4862

spec/support/mail_helpers.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ def email_link(email, string)
1717
end
1818

1919
def perform_enqueued_jobs_and_subsequently_enqueued_jobs
20-
perform_enqueued_jobs # Process event job
21-
perform_enqueued_jobs # Process mailer job
20+
3.times do
21+
perform_enqueued_jobs
22+
end
2223
end
2324

2425
private

0 commit comments

Comments
 (0)