tabletop-companion/app/controllers/table_invites_controller.rb

58 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
2024-05-29 16:17:06 +00:00
@table_invites = TableInvite.where(email: Current.user.email).not_responded
2024-05-29 15:47:31 +00:00
end
2024-05-29 14:24:18 +00:00
def new
@table_invite = @table.table_invites.new
end
def create
2024-05-30 07:16:06 +00:00
@table_invite = @table.table_invites.new(table_invite_params)
unless @table_invite.save
render :new, status: :unprocessable_entity, alert: t(".error") and return
2024-05-29 14:24:18 +00:00
end
2024-05-30 07:16:06 +00:00
if User.exists?(email: table_invite_params[:email])
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
else
2024-05-30 07:16:06 +00:00
TableInviteMailer.with(table_invite: @table_invite).invite_new_user.deliver_later
2024-05-29 14:24:18 +00:00
end
2024-05-30 07:16:06 +00:00
redirect_to @table, notice: t(".success", email: @table_invite.email)
2024-05-29 14:24:18 +00:00
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