From c7934b2f970a1b025959a8f7f5105ae37a13f95e Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Mon, 30 Jan 2017 15:17:45 +0100 Subject: [PATCH 1/3] Send a mail to usager if somebody else wrote a new commentaire on his dossier --- .gitignore | 1 + app/controllers/commentaires_controller.rb | 12 +++- app/views/dossiers/_dossier_show.html.haml | 13 ++--- .../{ => commentaires}/_commentaire.html.haml | 0 .../dossiers/commentaires/_form.html.haml | 5 ++ config/environments/development.rb | 8 ++- config/initializers/inflections.rb | 4 ++ .../dossiers/commentaires_controller_spec.rb | 58 +++++++++++++++++-- spec/factories/dossier.rb | 4 ++ 9 files changed, 87 insertions(+), 18 deletions(-) rename app/views/dossiers/{ => commentaires}/_commentaire.html.haml (100%) create mode 100644 app/views/dossiers/commentaires/_form.html.haml diff --git a/.gitignore b/.gitignore index 298f5f1bc..d7587c6c6 100644 --- a/.gitignore +++ b/.gitignore @@ -34,3 +34,4 @@ config/fog_credentials.yml uploads/* coverage/**/* .DS_Store +.byebug_history diff --git a/app/controllers/commentaires_controller.rb b/app/controllers/commentaires_controller.rb index 9506aa7f8..7eede7ab7 100644 --- a/app/controllers/commentaires_controller.rb +++ b/app/controllers/commentaires_controller.rb @@ -42,20 +42,20 @@ class CommentairesController < ApplicationController flash.alert = "Veuillez rédiger un message ou ajouter une pièce jointe." end + notify_user_with_mail(@commentaire) + if is_gestionnaire? unless current_gestionnaire.follow? @commentaire.dossier current_gestionnaire.toggle_follow_dossier @commentaire.dossier end - NotificationMailer.new_answer(@commentaire.dossier).deliver_now! if saved - redirect_to url_for(controller: 'backoffice/dossiers', action: :show, id: params['dossier_id']) else if current_user.email != @commentaire.dossier.user.email invite = Invite.where(dossier: @commentaire.dossier, email: current_user.email).first redirect_to url_for(controller: 'users/dossiers/invites', action: :show, id: invite.id) else - redirect_to url_for(controller: :recapitulatif, action: :show, dossier_id: params['dossier_id']) + redirect_to users_dossier_recapitulatif_path(params['dossier_id']) end end end @@ -63,4 +63,10 @@ class CommentairesController < ApplicationController def is_gestionnaire? false end + + private + + def notify_user_with_mail(commentaire) + NotificationMailer.new_answer(commentaire.dossier).deliver_now! unless current_user.email == commentaire.dossier.user.email + end end diff --git a/app/views/dossiers/_dossier_show.html.haml b/app/views/dossiers/_dossier_show.html.haml index e8681b45d..b67e69458 100644 --- a/app/views/dossiers/_dossier_show.html.haml +++ b/app/views/dossiers/_dossier_show.html.haml @@ -10,23 +10,19 @@ - elsif gestionnaire_signed_in? Usager %div.col-lg-2.col-md-2.col-sm-2.col-xs-2.count - - message_count = @facade.commentaires.count - = (message_count == 1) ? "1 message" : "#{message_count} messages" + = pluralize(@facade.commentaires.count, "message") %div.body - unless @facade.commentaires.empty? %div.commentaires - @facade.commentaires.object.sort.each do |commentaire| - = render partial: 'dossiers/commentaire', locals: {commentaire: commentaire} + = render partial: 'dossiers/commentaires/commentaire', locals: {commentaire: commentaire} .row .col-lg-12.col-md-12.col-sm-12.col-xs-12 %div.split-hr .row %div.col-lg-12.col-md-12.col-sm-12.col-xs-12#new-commentaire - = form_tag(url_for({ controller: 'commentaires', action: :create, dossier_id: @facade.dossier.id, champ_id: @facade.champ_id }), class: 'form-inline', method: 'POST', multipart: true) do - %textarea.form-control{id: 'texte_commentaire', class: 'wysihtml5', name: 'texte_commentaire', style: 'width: 100%; margin-bottom:2%', rows: '5', placeholder:"Commentaire"} - %h4.text-primary{style: 'margin-top: 0px'} Ajouter un fichier - = file_field_tag "piece_justificative[content]", accept: PieceJustificative.accept_format, style: 'float: left; margin-left: 20px' - %input#save-message.form-control.btn.btn-send{ type: 'submit', value: 'ENVOYER' } + = render partial: 'dossiers/commentaires/form', locals: { facade: @facade } + - if last_comment = @facade.commentaires.first %div.last-commentaire .row @@ -118,4 +114,3 @@ = (private_fields_count == 1) ? "1 champ" : "#{private_fields_count} champs" %div.body = render partial: '/dossiers/infos_private_fields' - diff --git a/app/views/dossiers/_commentaire.html.haml b/app/views/dossiers/commentaires/_commentaire.html.haml similarity index 100% rename from app/views/dossiers/_commentaire.html.haml rename to app/views/dossiers/commentaires/_commentaire.html.haml diff --git a/app/views/dossiers/commentaires/_form.html.haml b/app/views/dossiers/commentaires/_form.html.haml new file mode 100644 index 000000000..589fa587c --- /dev/null +++ b/app/views/dossiers/commentaires/_form.html.haml @@ -0,0 +1,5 @@ += form_tag(url_for({ controller: 'commentaires', action: :create, dossier_id: facade.dossier.id, champ_id: facade.champ_id }), class: 'form-inline', method: 'POST', multipart: true) do + %textarea.form-control{id: 'texte_commentaire', class: 'wysihtml5', name: 'texte_commentaire', style: 'width: 100%; margin-bottom:2%', rows: '5', placeholder:"Commentaire"} + %h4.text-primary{style: 'margin-top: 0px'} Ajouter un fichier + = file_field_tag "piece_justificative[content]", accept: PieceJustificative.accept_format, style: 'float: left; margin-left: 20px' + %input#save-message.form-control.btn.btn-send{ type: 'submit', value: 'ENVOYER' } diff --git a/config/environments/development.rb b/config/environments/development.rb index 4b96a6c33..c181590a8 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -39,9 +39,15 @@ Rails.application.configure do # Raises helpful error messages. config.assets.raise_runtime_errors = true - config.action_mailer.delivery_method = :test + config.action_mailer.delivery_method = :smtp config.action_mailer.default_url_options = { host: 'localhost', port: 3000 } + config.action_mailer.smtp_settings = { + :address => "localhost", + :port => 1025, + :locale => 'fr' + } + # Raises error for missing translations # config.action_view.raise_on_missing_translations = true diff --git a/config/initializers/inflections.rb b/config/initializers/inflections.rb index 940e78aad..a3bd49fb8 100644 --- a/config/initializers/inflections.rb +++ b/config/initializers/inflections.rb @@ -21,3 +21,7 @@ end # ActiveSupport::Inflector.inflections(:en) do |inflect| # inflect.acronym 'RESTful' # end +ActiveSupport::Inflector.inflections(:fr) do |inflect| + inflect.plural(/$/, 's') + inflect.plural(/(hib|ch|bij|caill|p|gen|jouj)ou$/i, '\1oux') +end diff --git a/spec/controllers/users/dossiers/commentaires_controller_spec.rb b/spec/controllers/users/dossiers/commentaires_controller_spec.rb index 871da0722..d56a18d9b 100644 --- a/spec/controllers/users/dossiers/commentaires_controller_spec.rb +++ b/spec/controllers/users/dossiers/commentaires_controller_spec.rb @@ -5,19 +5,67 @@ describe Users::Dossiers::CommentairesController, type: :controller do let(:texte_commentaire) { 'Commentaire de test' } describe '#POST create' do + + subject { + post :create, params:{dossier_id: dossier.id, texte_commentaire: texte_commentaire} + dossier.reload + } + context 'when invite is connected' do let!(:invite) { create(:invite, :with_user, dossier: dossier) } before do sign_in invite.user dossier.replied! - - post :create, params:{dossier_id: dossier.id, texte_commentaire: texte_commentaire} - dossier.reload end - it { is_expected.to redirect_to users_dossiers_invite_path(invite.id) } - it { expect(dossier.state).to eq 'replied' } + it do + subject + is_expected.to redirect_to users_dossiers_invite_path(invite.id) + expect(dossier.state).to eq 'replied' + end + + it 'should notify user' do + expect(NotificationMailer).to receive(:new_answer).and_return(NotificationMailer) + expect(NotificationMailer).to receive(:deliver_now!) + + subject + end + end + + context 'when user is connected' do + before do + sign_in dossier.user + end + + it 'do not send a mail to notify user' do + expect(NotificationMailer).to_not receive(:new_answer) + subject + end + + end + end + + describe '#notify_user_with_mail' do + let(:commentaire){create(:commentaire)} + + context 'when usager is writing a commentaire on dossier' do + before { sign_in commentaire.dossier.user } + + it { + expect(NotificationMailer).to_not receive(:new_answer) + subject.send(:notify_user_with_mail, commentaire) + } + end + + context 'when anybody else but usager is writing a commentaire' do + before { sign_in create(:user, email: 'administrateur@test.fr') } + + it { + expect(NotificationMailer).to receive(:new_answer).and_return(NotificationMailer) + expect(NotificationMailer).to receive(:deliver_now!) + subject.send(:notify_user_with_mail, commentaire) + } end end end diff --git a/spec/factories/dossier.rb b/spec/factories/dossier.rb index aa619d795..05e70525c 100644 --- a/spec/factories/dossier.rb +++ b/spec/factories/dossier.rb @@ -46,5 +46,9 @@ FactoryGirl.define do dossier.cerfa << create(:cerfa) end end + + trait :replied do + state 'replied' + end end end From 83945b3366ddef5e0a4879c6a2f5d1f60c2a9c69 Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Mon, 30 Jan 2017 15:21:47 +0100 Subject: [PATCH 2/3] add mailcatcher comment --- config/environments/development.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/config/environments/development.rb b/config/environments/development.rb index c181590a8..054e58352 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -42,6 +42,7 @@ Rails.application.configure do config.action_mailer.delivery_method = :smtp config.action_mailer.default_url_options = { host: 'localhost', port: 3000 } + # mailcatcher settings https://mailcatcher.me/ config.action_mailer.smtp_settings = { :address => "localhost", :port => 1025, From 91bd058106312bc6966738cf7a03e4f100ea4f42 Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Wed, 1 Feb 2017 15:58:56 +0100 Subject: [PATCH 3/3] Some fixes after review --- app/views/dossiers/_dossier_show.html.haml | 3 +-- spec/controllers/users/commentaires_controller_spec.rb | 8 ++++---- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/app/views/dossiers/_dossier_show.html.haml b/app/views/dossiers/_dossier_show.html.haml index b67e69458..913fc8024 100644 --- a/app/views/dossiers/_dossier_show.html.haml +++ b/app/views/dossiers/_dossier_show.html.haml @@ -14,8 +14,7 @@ %div.body - unless @facade.commentaires.empty? %div.commentaires - - @facade.commentaires.object.sort.each do |commentaire| - = render partial: 'dossiers/commentaires/commentaire', locals: {commentaire: commentaire} + = render partial: 'dossiers/commentaires/commentaire', collection: @facade.commentaires.object.sort .row .col-lg-12.col-md-12.col-sm-12.col-xs-12 %div.split-hr diff --git a/spec/controllers/users/commentaires_controller_spec.rb b/spec/controllers/users/commentaires_controller_spec.rb index b03cc9225..b6d2f0f60 100644 --- a/spec/controllers/users/commentaires_controller_spec.rb +++ b/spec/controllers/users/commentaires_controller_spec.rb @@ -14,7 +14,7 @@ describe Users::CommentairesController, type: :controller do context 'création correct d\'un commentaire' do subject do sign_in dossier.user - post :create, params: {dossier_id: dossier_id, texte_commentaire: texte_commentaire} + post :create, params: { dossier_id: dossier_id, texte_commentaire: texte_commentaire } end it 'depuis la page récapitulatif' do @@ -34,12 +34,12 @@ describe Users::CommentairesController, type: :controller do end end - context 'when document is upload whith a commentaire', vcr: {cassette_name: 'controllers_sers_commentaires_controller_upload_doc'} do + context 'when document is upload whith a commentaire', vcr: { cassette_name: 'controllers_sers_commentaires_controller_upload_doc' } do let(:document_upload) { Rack::Test::UploadedFile.new("./spec/support/files/piece_justificative_0.pdf", 'application/pdf') } subject do sign_in dossier.user - post :create, params: {dossier_id: dossier_id, texte_commentaire: texte_commentaire, piece_justificative: {content: document_upload}} + post :create, params: { dossier_id: dossier_id, texte_commentaire: texte_commentaire, piece_justificative: { content: document_upload } } end it 'create a new piece justificative' do @@ -88,7 +88,7 @@ describe Users::CommentairesController, type: :controller do sign_in dossier.user dossier.replied! - post :create, params: {dossier_id: dossier_id, texte_commentaire: texte_commentaire} + post :create, params: { dossier_id: dossier_id, texte_commentaire: texte_commentaire } dossier.reload end