tabletop-companion/app/controllers/table_invites_controller.rb

58 lines
1.6 KiB
Ruby

# frozen_string_literal: true
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).not_responded
end
def new
@table_invite = @table.table_invites.new
end
def create
@table_invite = @table.table_invites.new(table_invite_params)
unless @table_invite.save
render :new, status: :unprocessable_entity, alert: t(".error") and return
end
if User.exists?(email: table_invite_params[:email])
TableInviteMailer.with(table_invite: @table_invite).invite_user.deliver_later
else
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
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