diff --git a/app/controllers/admin_controller.rb b/app/controllers/admin_controller.rb index 9cc29b377..97a035387 100644 --- a/app/controllers/admin_controller.rb +++ b/app/controllers/admin_controller.rb @@ -4,20 +4,4 @@ class AdminController < ApplicationController def index redirect_to(admin_procedures_path) end - - def retrieve_procedure - id = params[:procedure_id] || params[:id] - - @procedure = current_administrateur.procedures.find(id) - - rescue ActiveRecord::RecordNotFound - flash.alert = 'Démarche inexistante' - redirect_to admin_procedures_path, status: 404 - end - - def reset_procedure - if @procedure.brouillon? - @procedure.reset! - end - end end diff --git a/app/controllers/administrateurs/administrateur_controller.rb b/app/controllers/administrateurs/administrateur_controller.rb index 1c187123c..9a9726694 100644 --- a/app/controllers/administrateurs/administrateur_controller.rb +++ b/app/controllers/administrateurs/administrateur_controller.rb @@ -13,9 +13,7 @@ module Administrateurs end def reset_procedure - if @procedure.brouillon? || @procedure.draft_changed? - @procedure.reset! - end + @procedure.reset! end def ensure_not_super_admin! diff --git a/app/controllers/administrateurs/conditions_controller.rb b/app/controllers/administrateurs/conditions_controller.rb index 4df461e0e..42cb328c1 100644 --- a/app/controllers/administrateurs/conditions_controller.rb +++ b/app/controllers/administrateurs/conditions_controller.rb @@ -3,6 +3,7 @@ module Administrateurs include Logic before_action :retrieve_procedure, :retrieve_coordinate_and_uppers + after_action :reset_procedure def update condition = condition_form.to_condition diff --git a/app/controllers/administrateurs/procedures_controller.rb b/app/controllers/administrateurs/procedures_controller.rb index fe4108141..4d4c2fc44 100644 --- a/app/controllers/administrateurs/procedures_controller.rb +++ b/app/controllers/administrateurs/procedures_controller.rb @@ -4,6 +4,7 @@ module Administrateurs before_action :retrieve_procedure, only: [:champs, :annotations, :modifications, :edit, :zones, :monavis, :update_monavis, :jeton, :update_jeton, :publication, :publish, :transfert, :close, :allow_expert_review, :experts_require_administrateur_invitation, :reset_draft] before_action :draft_valid?, only: [:apercu] + after_action :reset_procedure, only: [:update] ITEMS_PER_PAGE = 25 @@ -139,7 +140,6 @@ module Administrateurs render 'edit' end elsif @procedure.brouillon? - reset_procedure flash.notice = 'Démarche modifiée. Tous les dossiers de cette démarche ont été supprimés.' redirect_to admin_procedure_path(id: @procedure.id) else diff --git a/app/controllers/administrateurs/types_de_champ_controller.rb b/app/controllers/administrateurs/types_de_champ_controller.rb index e48d7b83c..2cb3e82b4 100644 --- a/app/controllers/administrateurs/types_de_champ_controller.rb +++ b/app/controllers/administrateurs/types_de_champ_controller.rb @@ -1,6 +1,7 @@ module Administrateurs class TypesDeChampController < AdministrateurController before_action :retrieve_procedure + after_action :reset_procedure, only: [:create, :update, :destroy] def create type_de_champ = draft.add_type_de_champ(type_de_champ_create_params) @@ -10,7 +11,6 @@ module Administrateurs @created = champ_component_from(@coordinate, focused: true) @morphed = champ_components_starting_at(@coordinate, 1) - reset_procedure flash.notice = "Formulaire enregistré" else flash.alert = type_de_champ.errors.full_messages @@ -24,7 +24,6 @@ module Administrateurs @coordinate = draft.coordinate_for(type_de_champ) @morphed = champ_components_starting_at(@coordinate) - reset_procedure flash.notice = "Formulaire enregistré" else flash.alert = type_de_champ.errors.full_messages @@ -69,7 +68,6 @@ module Administrateurs def destroy @coordinate = draft.remove_type_de_champ(params[:stable_id]) - reset_procedure flash.notice = "Formulaire enregistré" if @coordinate.present? diff --git a/app/models/concerns/dossier_rebase_concern.rb b/app/models/concerns/dossier_rebase_concern.rb index afe2e4c12..401451528 100644 --- a/app/models/concerns/dossier_rebase_concern.rb +++ b/app/models/concerns/dossier_rebase_concern.rb @@ -9,6 +9,10 @@ module DossierRebaseConcern end end + def rebase_later + DossierRebaseJob.perform_later(self) + end + def can_rebase? revision != procedure.published_revision && (brouillon? || accepted_en_construction_changes? || accepted_en_instruction_changes?) @@ -21,34 +25,32 @@ module DossierRebaseConcern private def accepted_en_construction_changes? - en_construction? && pending_changes.all? { |change| accepted_en_construction_change?(change) } + en_construction? && pending_changes.all? { |change| accepted_change?(change) } end def accepted_en_instruction_changes? - en_instruction? && pending_changes.all? { |change| accepted_en_instruction_change?(change) } + en_instruction? && pending_changes.all? { |change| accepted_change?(change) } end - def accepted_en_construction_change?(change) - if change[:op] == :move || change[:op] == :remove - true - elsif change[:op] == :update - case change[:attribute] - when :carte_layers - true - when :mandatory - change[:from] && !change[:to] - else - false - end - else + def accepted_change?(change) + return true if change[:private] + return true if change[:op].in?([:remove, :move]) + return !change[:mandatory] if change[:op] == :add + + case change[:attribute] + when :drop_down_options + (change[:from] - change[:to]).empty? + when :drop_down_other + !change[:from] && change[:to] + when :mandatory + change[:from] && !change[:to] + when :type_champ, :condition false + else + true end end - def accepted_en_instruction_change?(change) - false - end - def rebase # revision we are rebasing to target_revision = procedure.published_revision @@ -93,12 +95,14 @@ module DossierRebaseConcern def apply(change, champs) case change[:attribute] when :type_champ - champs.each { |champ| champ.piece_justificative_file.purge_later } # FIX ME: change updated_at + champs.each { |champ| purge_piece_justificative_file(champ) } GeoArea.where(champ: champs).destroy_all + Etablissement.where(champ: champs).destroy_all { type: "Champs::#{change[:to].classify}Champ", value: nil, + value_json: nil, external_id: nil, data: nil } @@ -152,4 +156,8 @@ module DossierRebaseConcern .where(dossier: self, types_de_champ: { stable_id: stable_id }) .destroy_all end + + def purge_piece_justificative_file(champ) + ActiveStorage::Attachment.where(id: champ.piece_justificative_file.ids).delete_all + end end diff --git a/app/models/procedure.rb b/app/models/procedure.rb index da98e1e4b..708b6bf4d 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -783,7 +783,7 @@ class Procedure < ApplicationRecord end dossiers .state_not_termine - .find_each { |dossier| DossierRebaseJob.perform_later(dossier) } + .find_each(&:rebase_later) end def reset_draft_revision! diff --git a/app/models/procedure_revision.rb b/app/models/procedure_revision.rb index 78a9d2cfc..27f09a6a8 100644 --- a/app/models/procedure_revision.rb +++ b/app/models/procedure_revision.rb @@ -252,7 +252,7 @@ class ProcedureRevision < ApplicationRecord end added = (to_sids - from_sids).map do |sid| - { model: :type_de_champ, op: :add, label: to_h[sid].libelle, private: to_h[sid].private?, _position: to_sids.index(sid), stable_id: sid } + { model: :type_de_champ, op: :add, label: to_h[sid].libelle, private: to_h[sid].private?, mandatory: to_h[sid].mandatory?, _position: to_sids.index(sid), stable_id: sid } end kept = from_sids.intersection(to_sids) diff --git a/spec/models/dossier_rebase_concern_spec.rb b/spec/models/dossier_rebase_concern_spec.rb index f2332bda3..29fa88ba9 100644 --- a/spec/models/dossier_rebase_concern_spec.rb +++ b/spec/models/dossier_rebase_concern_spec.rb @@ -1,12 +1,11 @@ describe Dossier do describe '#can_rebase?' do - let(:procedure) { create(:procedure, :with_type_de_champ_mandatory, :with_yes_no, attestation_template: build(:attestation_template)) } + let(:procedure) { create(:procedure, :with_type_de_champ_mandatory, :with_type_de_champ_private, :with_yes_no) } let(:attestation_template) { procedure.draft_revision.attestation_template.find_or_revise! } let(:type_de_champ) { procedure.active_revision.types_de_champ_public.find { |tdc| !tdc.mandatory? } } + let(:private_type_de_champ) { procedure.active_revision.types_de_champ_private.first } let(:mandatory_type_de_champ) { procedure.active_revision.types_de_champ_public.find(&:mandatory?) } - before { Flipper.enable(:procedure_revisions, procedure) } - context 'en_construction' do let(:dossier) { create(:dossier, :en_construction, procedure: procedure) } @@ -26,6 +25,23 @@ describe Dossier do dossier.reload end + it 'should be true' do + expect(dossier.pending_changes).not_to be_empty + expect(dossier.can_rebase?).to be_truthy + end + end + + context 'with added mandatory type de champ' do + before do + procedure.draft_revision.add_type_de_champ({ + type_champ: TypeDeChamp.type_champs.fetch(:text), + libelle: "Un champ text", + mandatory: true + }) + procedure.publish_revision! + dossier.reload + end + it 'should be false' do expect(dossier.pending_changes).not_to be_empty expect(dossier.can_rebase?).to be_falsey @@ -58,6 +74,34 @@ describe Dossier do end end + context 'with type de champ change type' do + context 'type de champ public' do + before do + procedure.draft_revision.find_and_ensure_exclusive_use(type_de_champ.stable_id).update(type_champ: :checkbox) + procedure.publish_revision! + dossier.reload + end + + it 'should be false' do + expect(dossier.pending_changes).not_to be_empty + expect(dossier.can_rebase?).to be_falsey + end + end + + context 'type de champ private' do + before do + procedure.draft_revision.find_and_ensure_exclusive_use(private_type_de_champ.stable_id).update(type_champ: :checkbox) + procedure.publish_revision! + dossier.reload + end + + it 'should be true' do + expect(dossier.pending_changes).not_to be_empty + expect(dossier.can_rebase?).to be_truthy + end + end + end + context 'with removed type de champ' do before do procedure.draft_revision.remove_type_de_champ(type_de_champ.stable_id) @@ -91,15 +135,19 @@ describe Dossier do dossier.reload end - it 'should be false' do + it 'should be true' do expect(dossier.pending_changes).not_to be_empty - expect(dossier.can_rebase?).to be_falsey + expect(dossier.can_rebase?).to be_truthy end end - context 'with removed type de champ' do + context 'with added mandatory type de champ' do before do - procedure.draft_revision.remove_type_de_champ(type_de_champ.stable_id) + procedure.draft_revision.add_type_de_champ({ + type_champ: TypeDeChamp.type_champs.fetch(:text), + libelle: "Un champ text", + mandatory: true + }) procedure.publish_revision! dossier.reload end @@ -117,11 +165,65 @@ describe Dossier do dossier.reload end + it 'should be true' do + expect(dossier.pending_changes).not_to be_empty + expect(dossier.can_rebase?).to be_truthy + end + end + + context 'with type de champ made mandatory' do + before do + procedure.draft_revision.find_and_ensure_exclusive_use(type_de_champ.stable_id).update(mandatory: true) + procedure.publish_revision! + dossier.reload + end + it 'should be false' do expect(dossier.pending_changes).not_to be_empty expect(dossier.can_rebase?).to be_falsey end end + + context 'with type de champ change type' do + context 'type de champ public' do + before do + procedure.draft_revision.find_and_ensure_exclusive_use(type_de_champ.stable_id).update(type_champ: :checkbox) + procedure.publish_revision! + dossier.reload + end + + it 'should be false' do + expect(dossier.pending_changes).not_to be_empty + expect(dossier.can_rebase?).to be_falsey + end + end + + context 'type de champ private' do + before do + procedure.draft_revision.find_and_ensure_exclusive_use(private_type_de_champ.stable_id).update(type_champ: :checkbox) + procedure.publish_revision! + dossier.reload + end + + it 'should be true' do + expect(dossier.pending_changes).not_to be_empty + expect(dossier.can_rebase?).to be_truthy + end + end + end + + context 'with removed type de champ' do + before do + procedure.draft_revision.remove_type_de_champ(type_de_champ.stable_id) + procedure.publish_revision! + dossier.reload + end + + it 'should be true' do + expect(dossier.pending_changes).not_to be_empty + expect(dossier.can_rebase?).to be_truthy + end + end end end @@ -330,8 +432,7 @@ describe Dossier do it { expect { subject }.to change { first_champ.data }.from({ 'a' => 1 }).to(nil) } it { expect { subject }.to change { first_champ.geo_areas.count }.from(1).to(0) } it { expect { subject }.to change { first_champ.piece_justificative_file.attached? }.from(true).to(false) } - # pb with pj.purge_later - xit { expect { subject }.not_to change { first_champ.updated_at }.from(Time.zone.parse('01/01/1901')) } + it { expect { subject }.not_to change { first_champ.updated_at }.from(Time.zone.parse('01/01/1901')) } end end diff --git a/spec/models/procedure_revision_spec.rb b/spec/models/procedure_revision_spec.rb index d311c2b45..9d9e2e4aa 100644 --- a/spec/models/procedure_revision_spec.rb +++ b/spec/models/procedure_revision_spec.rb @@ -433,6 +433,7 @@ describe ProcedureRevision do op: :add, label: "Un champ text", private: false, + mandatory: false, stable_id: new_tdc.stable_id } ])