From c3bf15d49f77eac516c0a54b161f49e09aa186d1 Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Tue, 25 Apr 2017 17:02:54 +0200 Subject: [PATCH] Create and list Avis on Dossier detail page --- app/controllers/backoffice/avis_controller.rb | 21 ++++++++ .../backoffice/dossiers_controller.rb | 2 + app/models/avis.rb | 4 ++ app/views/dossiers/_avis.html.haml | 52 +++++++++++++++++++ app/views/dossiers/_dossier_show.html.haml | 3 +- config/routes.rb | 1 + spec/models/avis_spec.rb | 20 +++++++ 7 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 app/controllers/backoffice/avis_controller.rb create mode 100644 app/views/dossiers/_avis.html.haml create mode 100644 spec/models/avis_spec.rb diff --git a/app/controllers/backoffice/avis_controller.rb b/app/controllers/backoffice/avis_controller.rb new file mode 100644 index 000000000..3d1559f6e --- /dev/null +++ b/app/controllers/backoffice/avis_controller.rb @@ -0,0 +1,21 @@ +class Backoffice::AvisController < ApplicationController + + def create + avis = Avis.new(create_params) + avis.dossier = dossier + avis.save + + redirect_to backoffice_dossier_path(dossier) + end + + private + + def dossier + current_gestionnaire.dossiers.find(params[:dossier_id]) + end + + def create_params + params.require(:avis).permit(:email, :introduction) + end + +end diff --git a/app/controllers/backoffice/dossiers_controller.rb b/app/controllers/backoffice/dossiers_controller.rb index c2dc2b89c..11ff0d7a6 100644 --- a/app/controllers/backoffice/dossiers_controller.rb +++ b/app/controllers/backoffice/dossiers_controller.rb @@ -29,6 +29,8 @@ class Backoffice::DossiersController < Backoffice::DossiersListController end Notification.where(dossier_id: params[:id].to_i).update_all already_read: true + + @new_avis = Avis.new(introduction: "Bonjour, merci de me donner votre avis sur ce dossier.") end def filter diff --git a/app/models/avis.rb b/app/models/avis.rb index 2e6c622a2..5bbe6244f 100644 --- a/app/models/avis.rb +++ b/app/models/avis.rb @@ -1,4 +1,8 @@ class Avis < ApplicationRecord belongs_to :dossier belongs_to :gestionnaire + + def email_to_display + gestionnaire.try(:email) || email + end end diff --git a/app/views/dossiers/_avis.html.haml b/app/views/dossiers/_avis.html.haml new file mode 100644 index 000000000..feeedd37c --- /dev/null +++ b/app/views/dossiers/_avis.html.haml @@ -0,0 +1,52 @@ +- if @current_gestionnaire + + .default-data-block.default_visible + .row.show-block.infos + .header + .col-xs-12.title + .carret-right + .carret-down + AVIS EXTERNES + .body + .display-block-on-print + - dossier_facade.dossier.avis.order(updated_at: :desc).each do |avis| + - if avis.answer + .panel.panel-success + .panel-heading + %strong= avis.email_to_display + a donné son avis le + = avis.updated_at.localtime.strftime('%d/%m/%Y à %H:%M') + .panel-body + %strong Vous : + = avis.introduction + %hr + %strong= "#{avis.email_to_display} :" + = avis.answer + - else + .panel.panel-info + .panel-heading + Avis demandé à + %strong= avis.email_to_display + le + = avis.created_at.localtime.strftime('%d/%m/%Y à %H:%M') + .panel-body + %strong Vous : + = avis.introduction + %hr + .center + %em Avis en attente + .hidden-print + .panel.panel-default + .panel-heading + Demander un avis externe + .panel-body + .help-block + Invitez une personne externe à consulter le dossier et à vous donner un avis sur celui ci. + %br + Cette personne pourra également contribuer au fil de messagerie, mais ne pourra pas modifier le dossier. + = simple_form_for @new_avis, url: backoffice_dossier_avis_index_path(dossier_facade.dossier.object.id) do |f| + + = f.input 'email', label: "Email de la personne qui doit donner un avis" + = f.input 'introduction', label: "Message" + + = f.submit "Envoyer la demande d'avis", class: 'btn btn-default' diff --git a/app/views/dossiers/_dossier_show.html.haml b/app/views/dossiers/_dossier_show.html.haml index 632a32f20..f4accd099 100644 --- a/app/views/dossiers/_dossier_show.html.haml +++ b/app/views/dossiers/_dossier_show.html.haml @@ -51,7 +51,6 @@ = render partial: '/users/carte/map', locals: { dossier: @facade.dossier } = render partial: 'users/carte/init_carto', locals: { dossier: @facade.dossier } - - if @current_gestionnaire && gestionnaire_signed_in? && @champs_private.count > 0 .default-data-block.default_visible .row.show-block#private-fields @@ -65,3 +64,5 @@ = (private_fields_count == 1) ? "1 champ" : "#{private_fields_count} champs" .body = render partial: '/dossiers/infos_private_fields' + += render partial: 'dossiers/avis', locals: { dossier_facade: @facade } diff --git a/config/routes.rb b/config/routes.rb index 16b0b090a..c11e135e0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -179,6 +179,7 @@ Rails.application.routes.draw do post 'reopen' => 'dossiers#reopen' put 'follow' => 'dossiers#follow' resources :commentaires, only: [:index] + resources :avis, only: [:create] end namespace :dossiers do diff --git a/spec/models/avis_spec.rb b/spec/models/avis_spec.rb new file mode 100644 index 000000000..fbb731329 --- /dev/null +++ b/spec/models/avis_spec.rb @@ -0,0 +1,20 @@ +require 'rails_helper' + +RSpec.describe Avis, type: :model do + describe '.email_to_display' do + let(:invited_email) { 'invited@avis.com' } + let!(:avis) { Avis.create(email: invited_email, dossier: create(:dossier)) } + + subject { avis.email_to_display } + + context 'when gestionnaire is not known' do + it{ is_expected.to eq(invited_email) } + end + + context 'when gestionnaire is known' do + let!(:avis) { Avis.create(email: nil, gestionnaire: create(:gestionnaire), dossier: create(:dossier)) } + + it{ is_expected.to eq(avis.gestionnaire.email) } + end + end +end