diff --git a/app/controllers/admin/procedures_controller.rb b/app/controllers/admin/procedures_controller.rb index df49606fe..5f7731658 100644 --- a/app/controllers/admin/procedures_controller.rb +++ b/app/controllers/admin/procedures_controller.rb @@ -42,6 +42,14 @@ class Admin::ProceduresController < AdminController def edit end + def hide + procedure = Procedure.find(params[:id]) + procedure.hide! + + flash.notice = "Procédure supprimée, en cas d'erreur contactez nous : contact@tps.apientreprise.fr" + redirect_to admin_procedures_draft_path + end + def destroy procedure = Procedure.find(params[:id]) diff --git a/app/models/dossier.rb b/app/models/dossier.rb index fd5d23838..a430dbf23 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -42,6 +42,7 @@ class Dossier < ActiveRecord::Base belongs_to :procedure belongs_to :user + default_scope { where(hidden_at: nil) } scope :state_brouillon, -> { where(state: BROUILLON) } scope :state_not_brouillon, -> { where.not(state: BROUILLON) } scope :state_nouveaux, -> { where(state: NOUVEAUX) } diff --git a/app/models/gestionnaire.rb b/app/models/gestionnaire.rb index c2252d18a..30185d0a2 100644 --- a/app/models/gestionnaire.rb +++ b/app/models/gestionnaire.rb @@ -20,9 +20,13 @@ class Gestionnaire < ActiveRecord::Base include CredentialsSyncableConcern def procedure_filter - return nil unless assign_to.pluck(:procedure_id).include?(self[:procedure_filter]) - - self[:procedure_filter] + procedure_id = self[:procedure_filter] + if procedures.find_by(id: procedure_id).present? + procedure_id + else + self.update_column(:procedure_filter, nil) + nil + end end def can_view_dossier?(dossier_id) diff --git a/app/models/procedure.rb b/app/models/procedure.rb index 8b6481708..83e88b823 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -31,12 +31,19 @@ class Procedure < ActiveRecord::Base mount_uploader :logo, ProcedureLogoUploader + default_scope { where(hidden_at: nil) } scope :not_archived, -> { where(archived: false) } scope :by_libelle, -> { order(libelle: :asc) } validates :libelle, presence: true, allow_blank: false, allow_nil: false validates :description, presence: true, allow_blank: false, allow_nil: false + def hide! + now = DateTime.now + self.update_attributes(hidden_at: now) + self.dossiers.update_all(hidden_at: now) + end + def path procedure_path.path unless procedure_path.nil? end diff --git a/app/views/admin/procedures/show.html.haml b/app/views/admin/procedures/show.html.haml index 10d45504e..91fa3dc42 100644 --- a/app/views/admin/procedures/show.html.haml +++ b/app/views/admin/procedures/show.html.haml @@ -129,3 +129,20 @@ Aucune statistique pour le moment - else = pie_chart @facade.dossiers_for_pie_highchart + + - if @facade.procedure.published? || @facade.procedure.archived? + %h3 Supprimer la procédure + .alert.alert-danger + %p + Attention : la suppression d'une procédure est définitive. + - dossiers_count = @facade.procedure.dossiers.count + - if dossiers_count > 0 + %p + = pluralize(dossiers_count, "dossier est rattaché", "dossiers sont rattachés") + à cette procédure, la suppression de cette procédure entrainera également leur suppression. + %p.text-right + = link_to "J'ai compris, je supprime la procédure", + hide_admin_procedure_path(@facade.procedure), + method: :post, + class: "btn btn-danger", + data: { confirm: "Voulez-vous supprimer la procédure ?", disable_with: "Suppression..." } diff --git a/config/routes.rb b/config/routes.rb index a64710865..b93f66f2b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -114,6 +114,10 @@ Rails.application.routes.draw do patch 'change_dossier_state' => 'change_dossier_state#change' resources :procedures do + member do + post :hide + end + resources :types_de_champ, only: [:destroy] resource :types_de_champ, only: [:show, :update] do post '/:index/move_up' => 'types_de_champ#move_up', as: :move_up diff --git a/db/migrate/20170627120928_add_hidden_at_to_procedures.rb b/db/migrate/20170627120928_add_hidden_at_to_procedures.rb new file mode 100644 index 000000000..a0c109597 --- /dev/null +++ b/db/migrate/20170627120928_add_hidden_at_to_procedures.rb @@ -0,0 +1,6 @@ +class AddHiddenAtToProcedures < ActiveRecord::Migration[5.0] + def change + add_column :procedures, :hidden_at, :datetime + add_index :procedures, :hidden_at + end +end diff --git a/db/migrate/20170627144046_add_hidden_at_to_dossiers.rb b/db/migrate/20170627144046_add_hidden_at_to_dossiers.rb new file mode 100644 index 000000000..b70d02e69 --- /dev/null +++ b/db/migrate/20170627144046_add_hidden_at_to_dossiers.rb @@ -0,0 +1,6 @@ +class AddHiddenAtToDossiers < ActiveRecord::Migration[5.0] + def change + add_column :dossiers, :hidden_at, :datetime + add_index :dossiers, :hidden_at + end +end diff --git a/db/schema.rb b/db/schema.rb index ad99eb400..2b3bbab8c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20170627143701) do +ActiveRecord::Schema.define(version: 20170627144046) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -175,6 +175,8 @@ ActiveRecord::Schema.define(version: 20170627143701) do t.datetime "received_at" t.datetime "processed_at" t.text "motivation" + t.datetime "hidden_at" + t.index ["hidden_at"], name: "index_dossiers_on_hidden_at", using: :btree t.index ["procedure_id"], name: "index_dossiers_on_procedure_id", using: :btree t.index ["user_id"], name: "index_dossiers_on_user_id", using: :btree end @@ -381,7 +383,9 @@ ActiveRecord::Schema.define(version: 20170627143701) do t.string "lien_notice" t.boolean "for_individual", default: false t.boolean "individual_with_siret", default: false - t.datetime "auto_archive_on" + t.date "auto_archive_on" + t.datetime "hidden_at" + t.index ["hidden_at"], name: "index_procedures_on_hidden_at", using: :btree t.datetime "published_at" t.datetime "archived_at" end diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index 21d4c6c4c..39a6e6794 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -928,4 +928,21 @@ describe Dossier do end end end + + describe ".default_scope" do + let!(:dossier) { create(:dossier, hidden_at: hidden_at) } + + context "when dossier is not hidden" do + let(:hidden_at) { nil } + + it { expect(Dossier.count).to eq(1) } + it { expect(Dossier.all).to include(dossier) } + end + + context "when dossier is hidden" do + let(:hidden_at) { 1.day.ago } + + it { expect(Dossier.count).to eq(0) } + end + end end diff --git a/spec/models/gestionnaire_spec.rb b/spec/models/gestionnaire_spec.rb index b55a26684..ba09804f2 100644 --- a/spec/models/gestionnaire_spec.rb +++ b/spec/models/gestionnaire_spec.rb @@ -260,6 +260,15 @@ describe Gestionnaire, type: :model do it { expect(AssignTo.where(gestionnaire: gestionnaire, procedure: procedure_3).count).to eq 0 } it { is_expected.to be_nil } end + + context "when procedure is hidden clear procedure_filter" do + before do + gestionnaire.update_column :procedure_filter, procedure_3.id + procedure_3.hide! + end + + it { is_expected.to be_nil } + end end end diff --git a/spec/models/procedure_spec.rb b/spec/models/procedure_spec.rb index 9de535d08..6e2a6124f 100644 --- a/spec/models/procedure_spec.rb +++ b/spec/models/procedure_spec.rb @@ -302,4 +302,40 @@ describe Procedure do it { is_expected.to eq('a-long-libelle-with-accents-blabla-coucou-hello-un') } end + + describe ".default_scope" do + let!(:procedure) { create(:procedure, hidden_at: hidden_at) } + + context "when hidden_at is nil" do + let(:hidden_at) { nil } + + it { expect(Procedure.count).to eq(1) } + it { expect(Procedure.all).to include(procedure) } + end + + context "when hidden_at is not nil" do + let(:hidden_at) { 2.days.ago } + + it { expect(Procedure.count).to eq(0) } + it { expect { Procedure.find(procedure.id) }.to raise_error(ActiveRecord::RecordNotFound) } + end + end + + describe "#hide!" do + let(:procedure) { create(:procedure) } + let!(:dossier) { create(:dossier, procedure: procedure) } + let!(:dossier2) { create(:dossier, procedure: procedure) } + + it { expect(Dossier.count).to eq(2) } + it { expect(Dossier.all).to include(dossier, dossier2) } + + context "when hidding procedure" do + before do + procedure.hide! + end + + it { expect(procedure.dossiers.count).to eq(0) } + it { expect(Dossier.count).to eq(0) } + end + end end