demarches-normaliennes/app/services/commentaire_service.rb

55 lines
1.3 KiB
Ruby
Raw Permalink Normal View History

class CommentaireService
2021-11-30 18:56:12 +01:00
def self.create(sender, dossier, params)
save(dossier, prepare_params(sender, params))
end
def self.create!(sender, dossier, params)
save!(dossier, prepare_params(sender, params))
end
def self.build(sender, dossier, params)
dossier.commentaires.build(prepare_params(sender, params))
end
def self.prepare_params(sender, params)
case sender
when String
params[:email] = sender
when Instructeur
params[:instructeur] = sender
params[:email] = sender.email
when Expert
params[:expert] = sender
params[:email] = sender.email
else
params[:email] = sender.email
end
# For some reason ActiveStorage trows an error in tests if we passe an empty string here.
# I suspect it could be resolved in rails 6 by using explicit `attach()`
if params[:piece_jointe].blank?
params.delete(:piece_jointe)
end
2021-11-30 18:56:12 +01:00
params
end
def self.save(dossier, params)
build_and_save(dossier, params)
end
def self.save!(dossier, params)
build_and_save(dossier, params, raise_exception: true)
end
def self.build_and_save(dossier, params, raise_exception: false)
message = dossier.commentaires.build(params)
if raise_exception
message.save!
else
message.save
message
end
end
end