Skip to content

Commit 99ebf29

Browse files
authored
Merge pull request #161 from joyofrails/feat/email-admin-new-user
Thanks page and email admin new user
2 parents 6032cb8 + 5a3eaa0 commit 99ebf29

File tree

20 files changed

+310
-64
lines changed

20 files changed

+310
-64
lines changed

app/controllers/concerns/erroring.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module Erroring
22
def not_found!
3-
raise ActionController::RoutingError, "Not Found"
3+
raise ActionController::RoutingError, "Route not found #{request.path}"
44
end
55
end

app/controllers/users/newsletter_subscriptions_controller.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class Users::NewsletterSubscriptionsController < ApplicationController
2-
invisible_captcha only: [:create]
2+
# invisible_captcha only: [:create]
33

44
before_action :authenticate_user_or_not_found!, only: [:index, :subscribe]
55
before_action :authenticate_user!, only: [:subscribe]
@@ -26,10 +26,8 @@ def new
2626

2727
def create
2828
create_user_params = params.require(:user).permit(:email)
29-
@user = User.find_or_initialize_by(email: create_user_params[:email]) do |u|
30-
u.subscribing = true
31-
end
32-
29+
@user = User.find_or_initialize_by(email: create_user_params[:email])
30+
@user.subscribing = true
3331
@newsletter_subscription = @user.newsletter_subscription || @user.build_newsletter_subscription
3432

3533
@user.save
@@ -38,10 +36,12 @@ def create
3836
return render Users::NewsletterSubscriptions::NewView.new(newsletter_subscription: @newsletter_subscription), status: :unprocessable_entity
3937
end
4038

39+
if @user.previously_new_record?
40+
NewUserNotifier.deliver_to(AdminUser.all, user: @user)
41+
end
42+
4143
if @user.needs_confirmation?
4244
EmailConfirmationNotifier.deliver_to(@user)
43-
else
44-
# TODO: Send already subscribed email
4545
end
4646

4747
redirect_to users_newsletter_subscription_path(@newsletter_subscription)

app/controllers/users/registrations_controller.rb

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,44 +8,47 @@ class Users::RegistrationsController < ApplicationController
88
before_action :authenticate_user!, only: [:edit, :update, :destroy]
99

1010
def new
11-
@user = User.new
12-
render Users::Registrations::NewView.new(user: @user)
11+
user = User.new
12+
render Users::Registrations::NewView.new(user: user)
1313
end
1414

1515
def create
1616
create_user_params = params.require(:user).permit(:email, :password, :password_confirmation)
17-
@user = User.new(create_user_params)
18-
if @user.save
19-
EmailConfirmationNotifier.deliver_to(@user)
20-
redirect_to root_path, notice: "Welcome to Joy of Rails! Please check your email for confirmation instructions"
17+
user = User.new(create_user_params)
18+
if user.save
19+
NewUserNotifier.deliver_to(AdminUser.all, user: user)
20+
EmailConfirmationNotifier.deliver_to(user)
21+
22+
redirect_to users_thank_you_path, notice: "Welcome to Joy of Rails! Please check your email for confirmation instructions"
2123
else
22-
render Users::Registrations::NewView.new(user: @user), status: :unprocessable_entity
24+
render Users::Registrations::NewView.new(user: user), status: :unprocessable_entity
2325
end
2426
end
2527

2628
def edit
27-
@user = current_user
28-
@user.email_exchanges.build
29+
user = current_user
30+
user.email_exchanges.build
2931

30-
render Users::Registrations::EditView.new(user: @user)
32+
render Users::Registrations::EditView.new(user: user)
3133
end
3234

3335
def update
3436
update_user_params = params.require(:user).permit(:password_challenge, :password, :password_confirmation, email_exchanges_attributes: [:email])
3537

36-
@user = current_user
38+
user = current_user
3739

38-
if !@user.authenticate(params[:user][:password_challenge])
40+
if user.password_digest_was.present? && !user.authenticate(params[:user][:password_challenge])
3941
flash.now[:error] = "Incorrect password"
40-
return render Users::Registrations::EditView.new(user: @user), status: :unprocessable_entity
42+
return render Users::Registrations::EditView.new(user: user), status: :unprocessable_entity
4143
end
4244

43-
if !@user.update(update_user_params)
44-
return render Users::Registrations::EditView.new(user: @user), status: :unprocessable_entity
45+
if !user.update(update_user_params)
46+
flash.now[:error] = user.errors.full_messages.to_sentence
47+
return render Users::Registrations::EditView.new(user: user), status: :unprocessable_entity
4548
end
4649

4750
if update_user_params[:email_exchanges_attributes].present?
48-
EmailConfirmationNotifier.deliver_to(@user)
51+
EmailConfirmationNotifier.deliver_to(user)
4952
redirect_to users_dashboard_path, notice: "Check your email for confirmation instructions"
5053
else
5154
redirect_to users_dashboard_path, notice: "Account updated"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# frozen_string_literal: true
2+
3+
class Users::ThankYousController < ApplicationController
4+
def show
5+
render Users::ThankYous::ShowView.new
6+
end
7+
end

app/javascript/css/utilities/tailwind.css

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -937,3 +937,15 @@ focus\:ring-0:focus {
937937
.max-w-sm {
938938
max-width: 24rem;
939939
}
940+
941+
.list-disc {
942+
list-style-type: disc;
943+
}
944+
945+
.list-inside {
946+
list-style-position: inside;
947+
}
948+
949+
.list-outside {
950+
list-style-position: outside;
951+
}
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!

app/views/users/dashboard/index_view.rb

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,50 @@ class Users::Dashboard::IndexView < ApplicationView
44
include Phlex::Rails::Helpers::LinkTo
55

66
def view_template
7-
div(class: "container lg:mt-12 md:mt-8") do
8-
h1 { "Welcome to Joy of Rails!" }
9-
p { "Thank you for signing up." }
10-
p { "Here's what you can do next:" }
11-
ul(class: "list-disc list-inside") do
12-
li do
13-
plain "Check out"
14-
whitespace
15-
strong { link_to("articles", "/articles") }
16-
whitespace
17-
plain "I’ve written on Ruby, Rails, and Hotwire."
18-
end
19-
li do
20-
plain "If you’re interested in learning how this app is built or even contributing to it, check out the"
21-
whitespace
22-
link_to("source code", "https://github.com/joyofrails/joyofrails")
23-
whitespace
24-
plain "on GitHub."
25-
end
26-
li do
27-
plain "Connect with me on"
28-
whitespace
29-
link_to("Twitter", "https://twitter.com/rossta", target: "_blank")
30-
plain ","
31-
whitespace
32-
link_to("Mastodon", "https://ruby.social/rossta", target: "_blank")
33-
whitespace
34-
plain ", and"
35-
whitespace
36-
link_to("LinkedIn", "https://www.linkedin.com/in/rosskaffenberger", target: "_blank")
37-
whitespace
7+
article do
8+
render Pages::Header.new(
9+
title: "Welcome to Joy of Rails!",
10+
description: "Thank you for signing up 😂"
11+
)
12+
13+
div(class: "container article-content") do
14+
p { "Here's what you can do next:" }
15+
ul(class: "list-disc list-outside px-4") do
16+
li do
17+
plain "Check your your email for a confirmation link to activate your account."
18+
whitespace
19+
plain "If you don’t see it, check your spam folder, or you can"
20+
whitespace
21+
link_to("resend the confirmation email", new_users_confirmation_path)
22+
plain "."
23+
end
24+
li do
25+
plain "Check out"
26+
whitespace
27+
strong { link_to("articles", "/articles") }
28+
whitespace
29+
plain "I’ve written on Ruby, Rails, and Hotwire."
30+
end
31+
li do
32+
plain "If you’re interested in learning how this app is built or even contributing to it, check out the"
33+
whitespace
34+
link_to("source code", "https://github.com/joyofrails/joyofrails")
35+
whitespace
36+
plain "on GitHub."
37+
end
38+
li do
39+
plain "Connect with me on"
40+
whitespace
41+
link_to("Twitter", "https://twitter.com/rossta", target: "_blank")
42+
plain ","
43+
whitespace
44+
link_to("Mastodon", "https://ruby.social/rossta", target: "_blank")
45+
whitespace
46+
plain ", and"
47+
whitespace
48+
link_to("LinkedIn", "https://www.linkedin.com/in/rosskaffenberger", target: "_blank")
49+
whitespace
50+
end
3851
end
3952
end
4053
end

0 commit comments

Comments
 (0)