Can invite new members to a table

This commit is contained in:
Trevor Vallender 2024-05-30 08:16:06 +01:00
parent 3e7909c02b
commit d00bde60b1
6 changed files with 30 additions and 13 deletions

View File

@ -13,20 +13,17 @@ class TableInvitesController < ApplicationController
end end
def create def create
@user = User.find_by(email: table_invite_params[:email]) @table_invite = @table.table_invites.new(table_invite_params)
if @user.blank? unless @table_invite.save
# TODO: Allow inviting non-users, we can send an email invite render :new, status: :unprocessable_entity, alert: t(".error") and return
flash[:alert] = t(".user_not_found")
render :new, status: :unprocessable_entity and return
end end
@table_invite = @table.table_invites.new(table_invite_params) if User.exists?(email: table_invite_params[:email])
if @table_invite.save
TableInviteMailer.with(table_invite: @table_invite).invite_user.deliver_later TableInviteMailer.with(table_invite: @table_invite).invite_user.deliver_later
redirect_to @table, notice: t(".success", email: @table_invite.email)
else else
render :new, status: :unprocessable_entity, alert: t(".error") TableInviteMailer.with(table_invite: @table_invite).invite_new_user.deliver_later
end end
redirect_to @table, notice: t(".success", email: @table_invite.email)
end end
def edit def edit

View File

@ -8,4 +8,10 @@ class TableInviteMailer < ApplicationMailer
mail(to: @table_invite.email, subject: "[Tabletop Companion] Invite to join a table") mail(to: @table_invite.email, subject: "[Tabletop Companion] Invite to join a table")
end end
def invite_new_user
@table_invite = params[:table_invite]
mail(to: @table_invite.email, subject: "[Tabletop Companion] Youve been invited to a game!")
end
end end

View File

@ -0,0 +1,3 @@
<%= htmlify_email(t(".content", table_name: @table_invite.table.name)) %>
<%= link_to t(".sign_up"), new_user_url %>

View File

@ -0,0 +1,3 @@
<%= t(".content", table_name: @table_invite.table.name) %>
<%= new_user_url %>

View File

@ -62,6 +62,11 @@ en:
log_out: Log out log_out: Log out
success: "You have signed out." success: "You have signed out."
table_invite_mailer: table_invite_mailer:
invite_new_user:
content: |-
Youve been invited to join a game on Tabletop Companion. To start playing, sign up at the
link below and accept your invitation!
sign_up: Sign up now
invite_user: invite_user:
content: |- content: |-
You have been invited to join the table “%{table_name}” on Tabletop Companion. To You have been invited to join the table “%{table_name}” on Tabletop Companion. To

View File

@ -35,13 +35,16 @@ class TableInviteInvitesControllerTest < ActionDispatch::IntegrationTest
assert_redirected_to table_path(tables(:table)) assert_redirected_to table_path(tables(:table))
end end
test "cant invite a user who doesnt exist" do test "can invite a non-user to join" do
sign_in users(:trevor) sign_in users(:trevor)
assert_no_changes("TableInvite.count") do assert_changes("TableInvite.count", +1) do
post(table_table_invites_url(tables(:table)), params: { table_invite: { email: "not_real@example.com" } }) assert_emails(+1) do
post(table_table_invites_url(tables(:table)), params: { table_invite: { email: "not_a_member@example.com" } })
end end
assert_response :unprocessable_entity
end end
assert_redirected_to table_path(tables(:table))
end
test "should email user when an invite is created" do test "should email user when an invite is created" do
sign_in users(:trevor) sign_in users(:trevor)
assert_emails(+1) do assert_emails(+1) do