From 3f439ac07add450b99f4f5f856baf8de0be5631a Mon Sep 17 00:00:00 2001 From: Nicolas Bouilleaud Date: Mon, 1 Jul 2019 18:14:02 +0200 Subject: [PATCH] Add Commentaire.is_sent_by_system? and .is_sent_by(someone) And use it in CommentaireHelper and in the _message_icon and _message_issuer partials --- app/helpers/commentaire_helper.rb | 10 ++-------- app/models/commentaire.rb | 9 +++++++++ .../shared/dossiers/messages/_message_icon.html.haml | 9 ++++----- .../dossiers/messages/_message_issuer.html.haml | 8 +++----- spec/models/commentaire_spec.rb | 12 ++++++++++++ 5 files changed, 30 insertions(+), 18 deletions(-) diff --git a/app/helpers/commentaire_helper.rb b/app/helpers/commentaire_helper.rb index 684ff2356..a92b48b3c 100644 --- a/app/helpers/commentaire_helper.rb +++ b/app/helpers/commentaire_helper.rb @@ -1,12 +1,12 @@ module CommentaireHelper def commentaire_is_from_me_class(commentaire, connected_user) - if commentaire_is_from_me(commentaire, connected_user) + if commentaire.sent_by?(connected_user) "from-me" end end def commentaire_answer_action(commentaire, connected_user) - if commentaire_is_from_me(commentaire, connected_user) + if commentaire.sent_by?(connected_user) "Envoyer un message à l’instructeur" else "Répondre dans la messagerie" @@ -22,10 +22,4 @@ module CommentaireHelper template = is_current_year ? :message_date : :message_date_with_year I18n.l(commentaire.created_at, format: template) end - - private - - def commentaire_is_from_me(commentaire, connected_user) - commentaire.email == connected_user.email - end end diff --git a/app/models/commentaire.rb b/app/models/commentaire.rb index 72ed5f388..0c01921fb 100644 --- a/app/models/commentaire.rb +++ b/app/models/commentaire.rb @@ -36,6 +36,15 @@ class Commentaire < ApplicationRecord end end + def sent_by_system? + [CONTACT_EMAIL, OLD_CONTACT_EMAIL].include?(email) && + user.nil? && gestionnaire.nil? + end + + def sent_by?(someone) + email == someone.email + end + def file_url if Flipflop.remote_storage? RemoteDownloader.new(file.path).url diff --git a/app/views/shared/dossiers/messages/_message_icon.html.haml b/app/views/shared/dossiers/messages/_message_icon.html.haml index cca3f3568..0d8eb2db1 100644 --- a/app/views/shared/dossiers/messages/_message_icon.html.haml +++ b/app/views/shared/dossiers/messages/_message_icon.html.haml @@ -1,8 +1,7 @@ -- case commentaire.email -- when connected_user.email - = image_tag('icons/account-circle.svg', class: 'person-icon') -- when OLD_CONTACT_EMAIL -- when CONTACT_EMAIL +- if commentaire.sent_by_system? = image_tag('icons/mail.svg', class: 'person-icon') +- elsif commentaire.sent_by?(connected_user) + = image_tag('icons/account-circle.svg', class: 'person-icon') - else = image_tag('icons/blue-person.svg', class: 'person-icon') + diff --git a/app/views/shared/dossiers/messages/_message_issuer.html.haml b/app/views/shared/dossiers/messages/_message_issuer.html.haml index 9cd754130..899a76683 100644 --- a/app/views/shared/dossiers/messages/_message_issuer.html.haml +++ b/app/views/shared/dossiers/messages/_message_issuer.html.haml @@ -1,8 +1,6 @@ -- case commentaire.email -- when connected_user.email - Vous -- when OLD_CONTACT_EMAIL -- when CONTACT_EMAIL +- if commentaire.sent_by_system? Email automatique +- elsif commentaire.sent_by?(connected_user) + Vous - else = commentaire.redacted_email diff --git a/spec/models/commentaire_spec.rb b/spec/models/commentaire_spec.rb index 00807d503..16ed77c83 100644 --- a/spec/models/commentaire_spec.rb +++ b/spec/models/commentaire_spec.rb @@ -7,6 +7,18 @@ describe Commentaire do it { is_expected.to have_db_column(:updated_at) } it { is_expected.to belong_to(:dossier) } + describe "#is_sent_by_system?" do + subject { commentaire.is_sent_by_system? } + + let(:commentaire) { build :commentaire, email: email } + + context 'with a commentaire created by the DS system' do + let(:email) { CONTACT_EMAIL } + + it { is_expected.to be_truthy } + end + end + describe "#redacted_email" do subject { commentaire.redacted_email }