[Fix #835] Enable asking for multiple avis at the same time
This commit is contained in:
parent
31ca0552ab
commit
c7358a8f53
6 changed files with 33 additions and 12 deletions
|
@ -5,22 +5,39 @@ module CreateAvisConcern
|
|||
|
||||
def create_avis_from_params(dossier, confidentiel = false)
|
||||
confidentiel ||= create_avis_params[:confidentiel]
|
||||
avis = Avis.new(create_avis_params.merge(claimant: current_gestionnaire, dossier: dossier, confidentiel: confidentiel))
|
||||
emails = create_avis_params[:emails].split(',').map(&:strip)
|
||||
|
||||
if avis.save
|
||||
flash.notice = "Une demande d'avis a été envoyée à #{avis.email_to_display}"
|
||||
create_results = Avis.create(
|
||||
emails.map do |email|
|
||||
{
|
||||
email: email,
|
||||
introduction: create_avis_params[:introduction],
|
||||
claimant: current_gestionnaire,
|
||||
dossier: dossier,
|
||||
confidentiel: confidentiel
|
||||
}
|
||||
end
|
||||
)
|
||||
|
||||
if create_results.all?(&:persisted?)
|
||||
sent_emails_addresses = create_results.map(&:email_to_display).join(", ")
|
||||
flash.notice = "Une demande d'avis a été envoyée à #{sent_emails_addresses}"
|
||||
|
||||
nil
|
||||
else
|
||||
flash.now.alert = @avis.errors.full_messages
|
||||
flash.now.alert = create_results
|
||||
.map(&:errors)
|
||||
.reject(&:empty?)
|
||||
.map(&:full_messages)
|
||||
.flatten
|
||||
|
||||
# When an error occurs, return the avis back to the controller
|
||||
# to give the user a chance to correct and resubmit
|
||||
avis
|
||||
Avis.new(create_avis_params)
|
||||
end
|
||||
end
|
||||
|
||||
def create_avis_params
|
||||
params.require(:avis).permit(:email, :introduction, :confidentiel)
|
||||
params.require(:avis).permit(:emails, :introduction, :confidentiel)
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue