diff --git a/app/controllers/table_invites_controller.rb b/app/controllers/table_invites_controller.rb index 49affb9..4b6c99c 100644 --- a/app/controllers/table_invites_controller.rb +++ b/app/controllers/table_invites_controller.rb @@ -13,20 +13,17 @@ class TableInvitesController < ApplicationController end def create - @user = User.find_by(email: table_invite_params[:email]) - if @user.blank? - # TODO: Allow inviting non-users, we can send an email invite - flash[:alert] = t(".user_not_found") - render :new, status: :unprocessable_entity and return + @table_invite = @table.table_invites.new(table_invite_params) + unless @table_invite.save + render :new, status: :unprocessable_entity, alert: t(".error") and return end - @table_invite = @table.table_invites.new(table_invite_params) - if @table_invite.save + if User.exists?(email: table_invite_params[:email]) TableInviteMailer.with(table_invite: @table_invite).invite_user.deliver_later - redirect_to @table, notice: t(".success", email: @table_invite.email) else - render :new, status: :unprocessable_entity, alert: t(".error") + TableInviteMailer.with(table_invite: @table_invite).invite_new_user.deliver_later end + redirect_to @table, notice: t(".success", email: @table_invite.email) end def edit diff --git a/app/mailers/table_invite_mailer.rb b/app/mailers/table_invite_mailer.rb index 1b689ce..d16116f 100644 --- a/app/mailers/table_invite_mailer.rb +++ b/app/mailers/table_invite_mailer.rb @@ -8,4 +8,10 @@ class TableInviteMailer < ApplicationMailer mail(to: @table_invite.email, subject: "[Tabletop Companion] Invite to join a table") end + + def invite_new_user + @table_invite = params[:table_invite] + + mail(to: @table_invite.email, subject: "[Tabletop Companion] You’ve been invited to a game!") + end end diff --git a/app/views/table_invite_mailer/invite_new_user.html.erb b/app/views/table_invite_mailer/invite_new_user.html.erb new file mode 100644 index 0000000..ed7972c --- /dev/null +++ b/app/views/table_invite_mailer/invite_new_user.html.erb @@ -0,0 +1,3 @@ +<%= htmlify_email(t(".content", table_name: @table_invite.table.name)) %> + +<%= link_to t(".sign_up"), new_user_url %> diff --git a/app/views/table_invite_mailer/invite_new_user.text.erb b/app/views/table_invite_mailer/invite_new_user.text.erb new file mode 100644 index 0000000..15a6ddb --- /dev/null +++ b/app/views/table_invite_mailer/invite_new_user.text.erb @@ -0,0 +1,3 @@ +<%= t(".content", table_name: @table_invite.table.name) %> + +<%= new_user_url %> diff --git a/config/locales/en.yml b/config/locales/en.yml index adab564..4d93e6d 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -62,6 +62,11 @@ en: log_out: Log out success: "You have signed out." table_invite_mailer: + invite_new_user: + content: |- + You’ve 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: content: |- You have been invited to join the table “%{table_name}” on Tabletop Companion. To diff --git a/test/controllers/table_invites_controller_test.rb b/test/controllers/table_invites_controller_test.rb index ca3da66..cf8eb4c 100644 --- a/test/controllers/table_invites_controller_test.rb +++ b/test/controllers/table_invites_controller_test.rb @@ -35,13 +35,16 @@ class TableInviteInvitesControllerTest < ActionDispatch::IntegrationTest assert_redirected_to table_path(tables(:table)) end - test "can’t invite a user who doesn’t exist" do + test "can invite a non-user to join" do sign_in users(:trevor) - assert_no_changes("TableInvite.count") do - post(table_table_invites_url(tables(:table)), params: { table_invite: { email: "not_real@example.com" } }) + assert_changes("TableInvite.count", +1) do + 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 + assert_redirected_to table_path(tables(:table)) end + test "should email user when an invite is created" do sign_in users(:trevor) assert_emails(+1) do