# 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 @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 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") 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