Email when invite is received
This commit is contained in:
parent
18c264b64b
commit
df0854ac39
|
@ -4,6 +4,10 @@ class TableInvitesController < ApplicationController
|
|||
before_action :set_table, only: [ :new, :create ]
|
||||
before_action :set_table_invite, only: [ :edit, :update ]
|
||||
|
||||
def index
|
||||
@table_invites = TableInvite.where(email: Current.user.email)
|
||||
end
|
||||
|
||||
def new
|
||||
@table_invite = @table.table_invites.new
|
||||
end
|
||||
|
@ -18,6 +22,7 @@ class TableInvitesController < ApplicationController
|
|||
|
||||
@table_invite = @table.table_invites.new(table_invite_params)
|
||||
if @table_invite.save
|
||||
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")
|
||||
|
|
|
@ -8,6 +8,7 @@ class TablesController < ApplicationController
|
|||
end
|
||||
|
||||
def show
|
||||
@table_invites = @table.table_invites.not_responded
|
||||
end
|
||||
|
||||
def new
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class TableInviteMailer < ApplicationMailer
|
||||
helper(:mailer)
|
||||
|
||||
def invite_user
|
||||
@table_invite = params[:table_invite]
|
||||
|
||||
mail(to: @table_invite.email, subject: "[Tabletop Companion] Invite to join a table")
|
||||
end
|
||||
end
|
|
@ -7,6 +7,8 @@ class TableInvite < ApplicationRecord
|
|||
presence: true,
|
||||
length: { maximum: 100 }
|
||||
|
||||
scope :not_responded, -> { where(accepted_at: nil, declined_at: nil) }
|
||||
|
||||
def accepted?
|
||||
accepted_at.present?
|
||||
end
|
||||
|
|
|
@ -8,7 +8,11 @@
|
|||
</head>
|
||||
|
||||
<body>
|
||||
<% if @user.present? %>
|
||||
<p><%= t(".greeting", name: @user.first_name) %></p>
|
||||
<% else %>
|
||||
<p><%= t(".greeting_without_name") %></p>
|
||||
<% end %>
|
||||
<%= yield %>
|
||||
<%= t(".sign_off_html") %>
|
||||
</body>
|
||||
|
|
|
@ -1,4 +1,8 @@
|
|||
<%= t(".greeting") %>
|
||||
<% if @user.present? %>
|
||||
<%= t(".greeting", name: @user.first_name) %>
|
||||
<% else %>
|
||||
<%= t(".greeting_without_name") %>
|
||||
<% end %>
|
||||
|
||||
<%= yield %>
|
||||
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
<%= htmlify_email(t(".content", table_name: @table_invite.table.name)) %>
|
||||
|
||||
<%= link_to t(".respond_to_invite"), table_invite_url(@table_invite) %>
|
|
@ -0,0 +1,3 @@
|
|||
<%= t(".content", table_name: @table_invite.table.name) %>
|
||||
|
||||
<%= table_invite_url(@table_invite) %>
|
|
@ -0,0 +1,11 @@
|
|||
<% content_for :title, t(".table_invites") %>
|
||||
|
||||
<h2><%= t(".table_invites") %></h2>
|
||||
|
||||
<% if @table_invites.any? %>
|
||||
<% @table_invites.each do |table_invite| %>
|
||||
<%= link_to table_invite.table, edit_table_invite_path(table_invite) %>
|
||||
<% end %>
|
||||
<% else %>
|
||||
<p><%= t(".no_table_invites") %></p>
|
||||
<% end %>
|
|
@ -3,7 +3,7 @@
|
|||
<h2><%= t(".new_table_invite", name: @table.name) %></h2>
|
||||
|
||||
<p><%= t(".new_invite_description") %></p>
|
||||
<%= form_with url: table_table_invites_path do |f| %>
|
||||
<%= form_with model: @table_invite, url: table_table_invites_path do |f| %>
|
||||
<%= f.label :email %>
|
||||
<%= f.email_field :email %>
|
||||
|
||||
|
|
|
@ -11,3 +11,12 @@
|
|||
<dt><%= t(".owner") %>:</dt>
|
||||
<dd><%= @table.owner.username %></dd>
|
||||
</dl>
|
||||
|
||||
<% if @table_invites.any? %>
|
||||
<h4>Pending invites</h4>
|
||||
<ul>
|
||||
<% @table_invites.each do |table_invite| %>
|
||||
<li><%= table_invite.email %></li>
|
||||
<% end %>
|
||||
</ul>
|
||||
<% end %>
|
||||
|
|
|
@ -16,7 +16,8 @@ en:
|
|||
application:
|
||||
tables: Tables
|
||||
mailer:
|
||||
greeting: "Hello, %{name}"
|
||||
greeting: "Hi %{name},"
|
||||
greeting_without_name: Hi,
|
||||
sign_off: |
|
||||
See you soon,
|
||||
The Tabletop Companion team
|
||||
|
@ -60,7 +61,16 @@ en:
|
|||
destroy:
|
||||
log_out: Log out
|
||||
success: "You have signed out."
|
||||
table_invite_mailer:
|
||||
invite_user:
|
||||
content: |-
|
||||
You have been invited to join the table “%{table_name}” on Tabletop Companion. To
|
||||
respond, visit the link below.
|
||||
respond_to_invite: Respond to invite
|
||||
table_invites:
|
||||
index:
|
||||
table_invites: Your invitations
|
||||
no_table_invites: You have no new invitations
|
||||
new:
|
||||
new_table_invite: Invite a player to %{name}
|
||||
new_invite_description: Enter an email address to invite a player to your table.
|
||||
|
@ -68,7 +78,7 @@ en:
|
|||
button_text: Send invite
|
||||
create:
|
||||
user_not_found: There is no registered user with that email address. Ask them to sign up!
|
||||
success: Send invite to %{email}
|
||||
success: Sent invite to %{email}
|
||||
error: Failed to send invite
|
||||
edit:
|
||||
respond_to_invite: Respond to your invitation
|
||||
|
|
|
@ -12,7 +12,7 @@ Rails.application.routes.draw do
|
|||
resources :account_verifications, only: [ :show ]
|
||||
resources :sessions, only: [ :new, :create, :destroy ]
|
||||
|
||||
resources :table_invites, only: [ :edit, :update ]
|
||||
resources :table_invites, only: [ :index, :edit, :update ]
|
||||
resources :tables do
|
||||
resources :table_invites, only: [ :new, :create ]
|
||||
end
|
||||
|
|
|
@ -3,6 +3,12 @@
|
|||
require "test_helper"
|
||||
|
||||
class TableInviteInvitesControllerTest < ActionDispatch::IntegrationTest
|
||||
test "should get index" do
|
||||
sign_in users(:trevor)
|
||||
get table_invites_url
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
test "only a table owner can invite players" do
|
||||
sign_in users(:trevor)
|
||||
get new_table_table_invite_url(tables(:gimlis_table))
|
||||
|
@ -29,6 +35,20 @@ class TableInviteInvitesControllerTest < ActionDispatch::IntegrationTest
|
|||
assert_redirected_to table_path(tables(:table))
|
||||
end
|
||||
|
||||
test "can’t invite a user who doesn’t exist" 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" } })
|
||||
end
|
||||
assert_response :unprocessable_entity
|
||||
end
|
||||
test "should email user when an invite is created" do
|
||||
sign_in users(:trevor)
|
||||
assert_emails(+1) do
|
||||
post(table_table_invites_url(tables(:table)), params: { table_invite: { email: "uninvited_user@example.com" } })
|
||||
end
|
||||
end
|
||||
|
||||
test "should accept table_invite" do
|
||||
sign_in users(:frodo)
|
||||
invite = table_invites(:unaccepted)
|
||||
|
|
Loading…
Reference in New Issue