demarches-normaliennes/app/controllers/invites_controller.rb
2018-08-01 17:41:12 +02:00

37 lines
888 B
Ruby

class InvitesController < ApplicationController
before_action :ensure_user_signed_in
def create
email = params[:invite_email].downcase
dossier = current_user.dossiers.find(params[:dossier_id])
invite = InviteUser.create(
dossier: dossier,
user: User.find_by(email: email),
email: email,
email_sender: current_user.email
)
if invite.valid?
if invite.user.present?
InviteMailer.invite_user(invite).deliver_later
else
InviteMailer.invite_guest(invite).deliver_later
end
flash.notice = "Une invitation a été envoyée à #{invite.email}."
else
flash.alert = invite.errors.full_messages
end
redirect_back(fallback_location: helpers.url_for_dossier(dossier))
end
private
def ensure_user_signed_in
if !user_signed_in?
return redirect_to root_path
end
end
end