tabletop-companion/app/controllers/table_invites_controller.rb

61 lines
1.6 KiB
Ruby
Raw Normal View History

2024-05-29 14:24:18 +00:00
# frozen_string_literal: true
class TableInvitesController < ApplicationController
before_action :set_table, only: [ :new, :create ]
before_action :set_table_invite, only: [ :edit, :update ]
2024-05-29 15:47:31 +00:00
def index
@table_invites = TableInvite.where(email: Current.user.email)
end
2024-05-29 14:24:18 +00:00
def new
@table_invite = @table.table_invites.new
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
end
@table_invite = @table.table_invites.new(table_invite_params)
if @table_invite.save
2024-05-29 15:47:31 +00:00
TableInviteMailer.with(table_invite: @table_invite).invite_user.deliver_later
2024-05-29 14:24:18 +00:00
redirect_to @table, notice: t(".success", email: @table_invite.email)
else
render :new, status: :unprocessable_entity, alert: t(".error")
end
end
def edit
redirect_to @table_invite.table if @table_invite.responded?
end
def update
if params[:accept] == "true"
@table_invite.accept!
redirect_to @table_invite.table, notice: t(".accept_success")
elsif params[:decline] == "true"
@table_invite.decline!
redirect_to :root, notice: t(".decline_success")
end
end
private
def set_table
@table = Current.user.owned_tables.find(params[:table_id])
end
def set_table_invite
@table_invite = TableInvite.where(email: Current.user.email)
.find(params[:id])
end
def table_invite_params
params.require(:table_invite).permit(:email)
end
end