From 2a2aef34db9d8cf9dd3fdea7e361ef5b099719ff Mon Sep 17 00:00:00 2001 From: Guillaume Lazzara Date: Fri, 5 Feb 2016 17:40:58 +0100 Subject: [PATCH 1/2] Add gestionnaire administration panel. --- .../admin/gestionnaires_controller.rb | 32 ++++++++++++ .../gestionnaires/_informations.html.haml | 5 ++ app/views/admin/gestionnaires/_list.html.haml | 15 ++++++ app/views/admin/gestionnaires/index.html.haml | 19 +++++++ app/views/admin/gestionnaires/index.js.erb | 1 + .../administrateurs/_login_banner.html.haml | 6 ++- config/locales/fr.yml | 8 +++ config/routes.rb | 1 + .../admin/gestionnaires_controller_spec.rb | 50 +++++++++++++++++++ .../gestionnaires/index.html.haml_spec.rb | 34 +++++++++++++ 10 files changed, 170 insertions(+), 1 deletion(-) create mode 100644 app/controllers/admin/gestionnaires_controller.rb create mode 100644 app/views/admin/gestionnaires/_informations.html.haml create mode 100644 app/views/admin/gestionnaires/_list.html.haml create mode 100644 app/views/admin/gestionnaires/index.html.haml create mode 100644 app/views/admin/gestionnaires/index.js.erb create mode 100644 spec/controllers/admin/gestionnaires_controller_spec.rb create mode 100644 spec/views/admin/gestionnaires/index.html.haml_spec.rb diff --git a/app/controllers/admin/gestionnaires_controller.rb b/app/controllers/admin/gestionnaires_controller.rb new file mode 100644 index 000000000..7d331452d --- /dev/null +++ b/app/controllers/admin/gestionnaires_controller.rb @@ -0,0 +1,32 @@ +class Admin::GestionnairesController < AdminController + include SmartListing::Helper::ControllerExtensions + helper SmartListing::Helper + + def index + @gestionnaires = smart_listing_create :gestionnaires, + current_administrateur.gestionnaires, + partial: "admin/gestionnaires/list", + array: true + @gestionnaire ||= Gestionnaire.new + end + + + def create + @gestionnaire = Gestionnaire.create(create_procedure_params) + + if (@gestionnaire.errors.empty?) + flash.notice = 'Gestionnaire ajouté' + else + flash.alert = @gestionnaire.errors.full_messages.join('
').html_safe + end + + redirect_to admin_gestionnaires_path + end + + def create_procedure_params + params.require(:gestionnaire).permit(:email) + .merge(administrateur_id: current_administrateur.id) + .merge(password: SecureRandom.hex(5)) + end + +end \ No newline at end of file diff --git a/app/views/admin/gestionnaires/_informations.html.haml b/app/views/admin/gestionnaires/_informations.html.haml new file mode 100644 index 000000000..6ee5146e4 --- /dev/null +++ b/app/views/admin/gestionnaires/_informations.html.haml @@ -0,0 +1,5 @@ +-{email: 'Email*'}.each do |key, value| + .form-group{class: ('has-error' if @gestionnaire.errors.messages[key])} + %h4 + =value + =f.text_field key, class: 'form-control', placeholder: value diff --git a/app/views/admin/gestionnaires/_list.html.haml b/app/views/admin/gestionnaires/_list.html.haml new file mode 100644 index 000000000..ced240082 --- /dev/null +++ b/app/views/admin/gestionnaires/_list.html.haml @@ -0,0 +1,15 @@ +- unless smart_listing.empty? + %table.table + %thead + %th#libelle= smart_listing.sortable 'Email', 'email' + + - @gestionnaires.each do |gestionnaire| + %tr + %td= gestionnaire.email + + = smart_listing.paginate + = smart_listing.pagination_per_page_links + +- else + %h4.center + Aucun gestionnaire diff --git a/app/views/admin/gestionnaires/index.html.haml b/app/views/admin/gestionnaires/index.html.haml new file mode 100644 index 000000000..094e5e546 --- /dev/null +++ b/app/views/admin/gestionnaires/index.html.haml @@ -0,0 +1,19 @@ +%h1 Gestion des gestionnaires +%br + +.row + .col-md-4.col-lg-4 + = smart_listing_render :gestionnaires + .col-md-1.col-lg-1 +   + .col-md-6.col-lg-6 + %h3 Ajouter un gestionnaire + #procedure_new.section.section-label + = form_for @gestionnaire, url: {controller: 'admin/gestionnaires', action: :create} do |f| + .row + .col-md-5.col-lg-5 + =render partial: 'informations', locals: {f: f} + .col-md-2.col-lg-2 + %br + %br + =f.submit 'Valider', class: 'btn btn-info', style: 'float:left' diff --git a/app/views/admin/gestionnaires/index.js.erb b/app/views/admin/gestionnaires/index.js.erb new file mode 100644 index 000000000..bd6cb85e9 --- /dev/null +++ b/app/views/admin/gestionnaires/index.js.erb @@ -0,0 +1 @@ +<%= smart_listing_update :gestionnaires %> diff --git a/app/views/administrateurs/_login_banner.html.haml b/app/views/administrateurs/_login_banner.html.haml index 0115dccd2..6b38f4867 100644 --- a/app/views/administrateurs/_login_banner.html.haml +++ b/app/views/administrateurs/_login_banner.html.haml @@ -10,11 +10,15 @@ = link_to(admin_procedures_path, id: :menu_item_procedure) do %i.fa.fa-list{ style: "background-size: 10px;"}  Procédures + %li + = link_to(admin_gestionnaires_path) do + %i.fa.fa-user +  Gestionnaires + %li.divider{ role: :separator} %li = link_to(admin_profile_path, id: :profile) do %i.fa.fa-user  Profile - %li.divider{ role: :separator} %li = link_to('/administrateurs/sign_out',id: :admin_sign_out, method: :delete) do %i.fa.fa-power-off diff --git a/config/locales/fr.yml b/config/locales/fr.yml index bbc60da49..20699080f 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -57,6 +57,14 @@ fr: email: blank: est vide taken: ': Invitation déjà envoyée' + gestionnaire: + attributes: + email: + invalid: invalide + taken: déjà utilisé + blank: est vide + password: + blank: ': Le mot de passe est vide' devise: confirmations: diff --git a/config/routes.rb b/config/routes.rb index fb8f963e3..ff8a910dd 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -74,6 +74,7 @@ Rails.application.routes.draw do resource :pieces_justificatives, only: [:show, :update] resources :pieces_justificatives, only: :destroy end + resources :gestionnaires, only: [:index, :create] end get 'backoffice' => 'backoffice#index' diff --git a/spec/controllers/admin/gestionnaires_controller_spec.rb b/spec/controllers/admin/gestionnaires_controller_spec.rb new file mode 100644 index 000000000..7d1f6cede --- /dev/null +++ b/spec/controllers/admin/gestionnaires_controller_spec.rb @@ -0,0 +1,50 @@ +require 'spec_helper' + +describe Admin::GestionnairesController, type: :controller do + let(:admin) { create(:administrateur) } + before do + sign_in admin + end + + describe 'GET #index' do + subject { get :index } + it { expect(subject.status).to eq(200) } + end + + describe 'POST #create' do + let(:email) { 'test@plop.com' } + before do + post :create, gestionnaire: { email: email } + end + it { expect(response.status).to eq(302) } + it { expect(response).to redirect_to admin_gestionnaires_path } + + describe 'Gestionnaire attributs in database' do + let(:gestionnaire) { Gestionnaire.last } + it { expect(gestionnaire.email).to eq(email) } + it { expect(gestionnaire.administrateur_id).to eq(admin.id) } + end + + context 'when email is not valid' do + let(:email) { 'piou' } + it { expect(response.status).to eq(302) } + it { expect{ response }.not_to change(Gestionnaire, :count) } + end + + context 'when email is empty' do + let(:email) { '' } + it { expect(response.status).to eq(302) } + it { expect{ response }.not_to change(Gestionnaire, :count) } + end + + context ' when email already exists' do + let(:email) { 'test@plop.com' } + before do + post :create, gestionnaire: { email: email } + end + it { expect(response.status).to eq(302) } + it { expect{ response }.not_to change(Gestionnaire, :count) } + end + + end +end \ No newline at end of file diff --git a/spec/views/admin/gestionnaires/index.html.haml_spec.rb b/spec/views/admin/gestionnaires/index.html.haml_spec.rb new file mode 100644 index 000000000..13fb1c9df --- /dev/null +++ b/spec/views/admin/gestionnaires/index.html.haml_spec.rb @@ -0,0 +1,34 @@ +require 'spec_helper' + +describe 'admin/gestionnaires/index.html.haml', type: :view do + let(:token) { 'super_token' } + let(:admin) { create(:administrateur, api_token: token) } + + before do + assign(:gestionnaires, (smart_listing_create :gestionnaires, + admin.gestionnaires, + partial: "admin/gestionnaires/list", + array: true)) + assign(:gestionnaire, Gestionnaire.new()) + end + + context 'Aucun gestionnaire' do + before do + render + end + it { expect(rendered).to have_content('Aucun gestionnaire') } + end + + context 'Ajout d\'un gestionnaire' do + before do + create(:gestionnaire, administrateur: admin) + admin.reload + assign(:gestionnaires, (smart_listing_create :gestionnaires, + admin.gestionnaires, + partial: "admin/gestionnaires/list", + array: true)) + render + end + it { expect(rendered).to match(/plop\d+@plop.com/) } + end +end \ No newline at end of file From b58b0de8b09b60662fb530d5ca7ab9086107f464 Mon Sep 17 00:00:00 2001 From: Guillaume Lazzara Date: Tue, 9 Feb 2016 11:00:13 +0100 Subject: [PATCH 2/2] Add gestionnaire management --- .../admin/gestionnaires_controller.rb | 10 ++-- app/mailers/gestionnaire_mailer.rb | 20 +++++++ .../new_gestionnaire.text.erb | 12 ++++ .../admin/gestionnaires_controller_spec.rb | 58 ++++++++++++++++--- 4 files changed, 87 insertions(+), 13 deletions(-) create mode 100644 app/mailers/gestionnaire_mailer.rb create mode 100644 app/views/gestionnaire_mailer/new_gestionnaire.text.erb diff --git a/app/controllers/admin/gestionnaires_controller.rb b/app/controllers/admin/gestionnaires_controller.rb index 7d331452d..3be0ac818 100644 --- a/app/controllers/admin/gestionnaires_controller.rb +++ b/app/controllers/admin/gestionnaires_controller.rb @@ -12,10 +12,12 @@ class Admin::GestionnairesController < AdminController def create - @gestionnaire = Gestionnaire.create(create_procedure_params) + gestionnaire_params = create_gestionnaire_params + @gestionnaire = Gestionnaire.create(gestionnaire_params) - if (@gestionnaire.errors.empty?) - flash.notice = 'Gestionnaire ajouté' + if @gestionnaire.errors.messages.empty? + flash.notice = 'Gestionnaire ajouté' + GestionnaireMailer.new_gestionnaire(gestionnaire_params[:email], gestionnaire_params[:password]).deliver_now! else flash.alert = @gestionnaire.errors.full_messages.join('
').html_safe end @@ -23,7 +25,7 @@ class Admin::GestionnairesController < AdminController redirect_to admin_gestionnaires_path end - def create_procedure_params + def create_gestionnaire_params params.require(:gestionnaire).permit(:email) .merge(administrateur_id: current_administrateur.id) .merge(password: SecureRandom.hex(5)) diff --git a/app/mailers/gestionnaire_mailer.rb b/app/mailers/gestionnaire_mailer.rb new file mode 100644 index 000000000..86339a022 --- /dev/null +++ b/app/mailers/gestionnaire_mailer.rb @@ -0,0 +1,20 @@ +class GestionnaireMailer < ApplicationMailer + + def new_gestionnaire email, password + send_mail email, password, "Vous avez été nommé accompagnateur sur la plateforme TPS" + end + + private + + def vars_mailer email, password + @password = password + @email = email + end + + def send_mail email, password, subject + vars_mailer email, password + + mail(from: "tps@apientreprise.fr", to: email, + subject: subject) + end +end diff --git a/app/views/gestionnaire_mailer/new_gestionnaire.text.erb b/app/views/gestionnaire_mailer/new_gestionnaire.text.erb new file mode 100644 index 000000000..29ea6fd04 --- /dev/null +++ b/app/views/gestionnaire_mailer/new_gestionnaire.text.erb @@ -0,0 +1,12 @@ +Bienvenue sur la plateforme TPS + +Vous venez d'être nommé accompagnateur sur la plateforme TPS. Pour mémoire, voici quelques informations utiles : + + URL : https://tps.apientreprise.fr/gestionnaires/sign_in + Login : <%= @email %> + Mot de passe : <%= @password %> + +Bonne journée, + +--- +L'équipe TPS - tps@apientreprise.fr \ No newline at end of file diff --git a/spec/controllers/admin/gestionnaires_controller_spec.rb b/spec/controllers/admin/gestionnaires_controller_spec.rb index 7d1f6cede..030e8c66e 100644 --- a/spec/controllers/admin/gestionnaires_controller_spec.rb +++ b/spec/controllers/admin/gestionnaires_controller_spec.rb @@ -13,38 +13,78 @@ describe Admin::GestionnairesController, type: :controller do describe 'POST #create' do let(:email) { 'test@plop.com' } - before do - post :create, gestionnaire: { email: email } - end - it { expect(response.status).to eq(302) } - it { expect(response).to redirect_to admin_gestionnaires_path } + subject { post :create, gestionnaire: { email: email } } - describe 'Gestionnaire attributs in database' do - let(:gestionnaire) { Gestionnaire.last } - it { expect(gestionnaire.email).to eq(email) } - it { expect(gestionnaire.administrateur_id).to eq(admin.id) } + context 'When email is valid' do + before do + subject + end + it { expect(response.status).to eq(302) } + it { expect(response).to redirect_to admin_gestionnaires_path } + + describe 'Gestionnaire attributs in database' do + let(:gestionnaire) { Gestionnaire.last } + it { expect(gestionnaire.email).to eq(email) } + it { expect(gestionnaire.administrateur_id).to eq(admin.id) } + end end context 'when email is not valid' do + before do + subject + end let(:email) { 'piou' } it { expect(response.status).to eq(302) } it { expect{ response }.not_to change(Gestionnaire, :count) } end context 'when email is empty' do + before do + subject + end let(:email) { '' } it { expect(response.status).to eq(302) } it { expect{ response }.not_to change(Gestionnaire, :count) } + it 'Notification email is not send' do + expect(GestionnaireMailer).not_to receive(:new_gestionnaire) + expect(GestionnaireMailer).not_to receive(:deliver_now!) + end end context ' when email already exists' do let(:email) { 'test@plop.com' } before do + subject post :create, gestionnaire: { email: email } end it { expect(response.status).to eq(302) } it { expect{ response }.not_to change(Gestionnaire, :count) } end + context 'Email notification' do + + it 'Notification email is sent when email is valid' do + expect(GestionnaireMailer).to receive(:new_gestionnaire).and_return(GestionnaireMailer) + expect(GestionnaireMailer).to receive(:deliver_now!) + subject + end + + context 'is not sent when email is not valid' do + let(:email) { 'testplop.com' } + it { + expect(GestionnaireMailer).not_to receive(:new_gestionnaire) + expect(GestionnaireMailer).not_to receive(:deliver_now!) + subject + } + end + + it 'is not sent when email already exists' do + subject + expect(GestionnaireMailer).not_to receive(:new_gestionnaire) + expect(GestionnaireMailer).not_to receive(:deliver_now!) + subject + end + + end end end \ No newline at end of file