From 1dcb29ec659a95b42fa6b021a859abbdf139e1a7 Mon Sep 17 00:00:00 2001 From: Xavier J Date: Mon, 20 Jun 2016 17:37:04 +0200 Subject: [PATCH] Add feature delete for draft procedure --- .../admin/procedures_controller.rb | 19 ++++++-- app/decorators/commentaire_decorator.rb | 2 +- app/decorators/dossier_decorator.rb | 2 +- app/decorators/procedure_decorator.rb | 4 ++ app/models/gestionnaire.rb | 2 +- app/models/procedure.rb | 4 +- .../admin/procedures/_draft_list.html.haml | 21 --------- app/views/admin/procedures/_list.html.haml | 14 ++++-- .../admin/procedures_controller_spec.rb | 43 ++++++++++++++++++- 9 files changed, 77 insertions(+), 34 deletions(-) delete mode 100644 app/views/admin/procedures/_draft_list.html.haml diff --git a/app/controllers/admin/procedures_controller.rb b/app/controllers/admin/procedures_controller.rb index 19e003a70..2698c8dca 100644 --- a/app/controllers/admin/procedures_controller.rb +++ b/app/controllers/admin/procedures_controller.rb @@ -7,7 +7,7 @@ class Admin::ProceduresController < AdminController def index @procedures = smart_listing_create :procedures, - current_administrateur.procedures.where(published: true, archived: false), + current_administrateur.procedures.where(published: true, archived: false).order(created_at: :desc), partial: "admin/procedures/list", array: true @@ -16,7 +16,7 @@ class Admin::ProceduresController < AdminController def archived @procedures = smart_listing_create :procedures, - current_administrateur.procedures.where(archived: true), + current_administrateur.procedures.where(archived: true).order(created_at: :desc), partial: "admin/procedures/list", array: true @@ -27,8 +27,8 @@ class Admin::ProceduresController < AdminController def draft @procedures = smart_listing_create :procedures, - current_administrateur.procedures.where(published: false, archived: false), - partial: "admin/procedures/draft_list", + current_administrateur.procedures.where(published: false, archived: false).order(created_at: :desc), + partial: "admin/procedures/list", array: true draft_class @@ -45,6 +45,17 @@ class Admin::ProceduresController < AdminController end + def destroy + procedure = Procedure.find(params[:id]) + + return render json: {}, status: 401 if procedure.published? || procedure.archived? + + procedure.destroy + + flash.notice = 'Procédure supprimée' + redirect_to admin_procedures_draft_path + end + def new @procedure ||= Procedure.new @procedure.module_api_carto ||= ModuleAPICarto.new diff --git a/app/decorators/commentaire_decorator.rb b/app/decorators/commentaire_decorator.rb index 9cf845b95..06b752c3b 100644 --- a/app/decorators/commentaire_decorator.rb +++ b/app/decorators/commentaire_decorator.rb @@ -2,7 +2,7 @@ class CommentaireDecorator < Draper::Decorator delegate_all def created_at_fr - created_at.to_datetime.strftime('%d/%m/%Y - %H:%M') + created_at.localtime.strftime('%d/%m/%Y - %H:%M') rescue 'dd/mm/YYYY - HH:MM' end diff --git a/app/decorators/dossier_decorator.rb b/app/decorators/dossier_decorator.rb index e39477d51..a1b76c80d 100644 --- a/app/decorators/dossier_decorator.rb +++ b/app/decorators/dossier_decorator.rb @@ -3,7 +3,7 @@ class DossierDecorator < Draper::Decorator delegate_all def display_date - date_previsionnelle.to_date.strftime('%d/%m/%Y') + date_previsionnelle.localtime.strftime('%d/%m/%Y') rescue 'dd/mm/YYYY' end diff --git a/app/decorators/procedure_decorator.rb b/app/decorators/procedure_decorator.rb index 445753b64..b0b38bea2 100644 --- a/app/decorators/procedure_decorator.rb +++ b/app/decorators/procedure_decorator.rb @@ -5,6 +5,10 @@ class ProcedureDecorator < Draper::Decorator h.new_users_dossiers_url(procedure_id: id) end + def created_at_fr + created_at.localtime.strftime('%d/%m/%Y %H:%M') + end + def logo_img return 'logo-tps.png' if logo.blank? logo diff --git a/app/models/gestionnaire.rb b/app/models/gestionnaire.rb index 94ee10e21..e01fe47a2 100644 --- a/app/models/gestionnaire.rb +++ b/app/models/gestionnaire.rb @@ -4,7 +4,7 @@ class Gestionnaire < ActiveRecord::Base has_and_belongs_to_many :administrateurs - has_many :assign_to + has_many :assign_to, dependent: :destroy has_many :procedures, through: :assign_to has_many :dossiers, through: :procedures diff --git a/app/models/procedure.rb b/app/models/procedure.rb index 5f4de31f6..de66b000e 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -1,13 +1,13 @@ class Procedure < ActiveRecord::Base has_many :types_de_piece_justificative, dependent: :destroy has_many :types_de_champ, dependent: :destroy - has_many :dossiers, dependent: :destroy + has_many :dossiers has_one :module_api_carto, dependent: :destroy belongs_to :administrateur - has_many :assign_to + has_many :assign_to, dependent: :destroy has_many :gestionnaires, through: :assign_to delegate :use_api_carto, to: :module_api_carto diff --git a/app/views/admin/procedures/_draft_list.html.haml b/app/views/admin/procedures/_draft_list.html.haml deleted file mode 100644 index b169f2fc5..000000000 --- a/app/views/admin/procedures/_draft_list.html.haml +++ /dev/null @@ -1,21 +0,0 @@ -- unless smart_listing.empty? - %table.table - %thead - %th#ID= smart_listing.sortable 'ID', 'id' - %th#libelle= smart_listing.sortable 'Libellé', 'libelle' - %th#lien Actions - - - @procedures.each do |procedure| - - procedure = procedure.decorate - %tr - %td.col-md-1.col-lg-1= procedure.id - %td.col-md-6.col-lg-6 - = link_to(procedure.libelle, "/admin/procedures/#{procedure.id}") - %td= link_to('cloner', admin_procedure_clone_path(procedure.id), 'data-method' => :put, class: 'btn-xs btn-primary') - - = smart_listing.paginate - = smart_listing.pagination_per_page_links - -- else - %h4.center - Aucune procédure diff --git a/app/views/admin/procedures/_list.html.haml b/app/views/admin/procedures/_list.html.haml index 4735d527f..ed729fc6d 100644 --- a/app/views/admin/procedures/_list.html.haml +++ b/app/views/admin/procedures/_list.html.haml @@ -3,7 +3,9 @@ %thead %th#ID= smart_listing.sortable 'ID', 'id' %th#libelle= smart_listing.sortable 'Libellé', 'libelle' - %th#lien Lien + - unless @draft_class + %th#lien Lien + %th#created_at= smart_listing.sortable 'Date création', 'created_at' %th#lien Actions - @procedures.each do |procedure| @@ -12,8 +14,14 @@ %td.col-md-1.col-lg-1= procedure.id %td.col-md-6.col-lg-6 = link_to(procedure.libelle, "/admin/procedures/#{procedure.id}") - %td= link_to procedure.lien, procedure.lien - %td= link_to('cloner', admin_procedure_clone_path(procedure.id), 'data-method' => :put, class: 'btn-xs btn-primary') + - unless @draft_class + %td= link_to procedure.lien, procedure.lien + %td + = procedure.created_at_fr + %td + = link_to('Cloner', admin_procedure_clone_path(procedure.id), 'data-method' => :put, class: 'btn-sm btn-primary') + - unless procedure.published? || procedure.archived? + = link_to('X', url_for(controller: 'admin/procedures', action: :destroy, id: procedure.id), 'data-method' => :delete, class: 'btn-sm btn-danger') = smart_listing.paginate = smart_listing.pagination_per_page_links diff --git a/spec/controllers/admin/procedures_controller_spec.rb b/spec/controllers/admin/procedures_controller_spec.rb index 8d8779dc3..6bd380b79 100644 --- a/spec/controllers/admin/procedures_controller_spec.rb +++ b/spec/controllers/admin/procedures_controller_spec.rb @@ -52,6 +52,47 @@ describe Admin::ProceduresController, type: :controller do it { expect(response.status).to eq(200) } end + describe 'DELETE #destroy' do + + let(:procedure_draft) { create :procedure, published: false, archived: false } + let(:procedure_published) { create :procedure, published: true, archived: false } + let(:procedure_archived) { create :procedure, published: false, archived: true } + + subject { delete :destroy, id: procedure.id } + + context 'when procedure is draft' do + let!(:procedure) { procedure_draft } + + describe 'tech params' do + before do + subject + end + + it { expect(subject.status).to eq 302 } + it { expect(flash[:notice]).to be_present } + end + + it 'destroy procedure is call' do + expect_any_instance_of(Procedure).to receive(:destroy) + subject + end + + it { expect { subject }.to change{Procedure.count}.by(-1) } + end + + context 'when procedure is published' do + let(:procedure) { procedure_published } + + it { expect(subject.status).to eq 401 } + end + + context 'when procedure is archived' do + let(:procedure) { procedure_published } + + it { expect(subject.status).to eq 401 } + end + end + describe 'GET #edit' do let(:published) { false } let(:procedure) { create(:procedure, administrateur: admin, published: published) } @@ -270,7 +311,7 @@ describe Admin::ProceduresController, type: :controller do let!(:procedure) { create(:procedure, administrateur: admin) } subject { put :clone, procedure_id: procedure.id } - it { expect{ subject }.to change(Procedure, :count).by(1) } + it { expect { subject }.to change(Procedure, :count).by(1) } context 'when admin is the owner of the procedure' do before do