demarches-normaliennes/app/controllers/invites_controller.rb

41 lines
980 B
Ruby
Raw Normal View History

class InvitesController < ApplicationController
2018-03-22 09:31:07 +01:00
before_action :ensure_user_signed_in
2016-09-13 12:17:56 +02:00
def create
email = params[:invite_email].downcase
dossier = current_user.dossiers.find(params[:dossier_id])
2016-12-13 14:22:54 +01:00
2018-03-22 09:31:07 +01:00
invite = InviteUser.create(
dossier: dossier,
2018-03-22 09:31:07 +01:00
user: User.find_by(email: email),
email: email,
email_sender: current_user.email
)
if invite.valid?
2018-01-11 19:04:39 +01:00
if invite.user.present?
InviteMailer.invite_user(invite).deliver_later
2018-01-11 19:04:39 +01:00
else
InviteMailer.invite_guest(invite).deliver_later
2018-01-11 19:04:39 +01:00
end
flash.notice = "Une invitation a été envoyée à #{invite.email}."
else
flash.alert = invite.errors.full_messages
end
respond_to do |format|
format.html { redirect_back(fallback_location: helpers.url_for_dossier(dossier)) }
format.js { @dossier = dossier }
end
end
2016-09-13 12:17:56 +02:00
private
2018-03-22 09:31:07 +01:00
def ensure_user_signed_in
if !user_signed_in?
2018-01-11 19:04:39 +01:00
return redirect_to root_path
end
2016-09-13 12:17:56 +02:00
end
end