diff --git a/app/models/dossier.rb b/app/models/dossier.rb index bc03925bc..29ea1344b 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -95,6 +95,8 @@ class Dossier < ApplicationRecord end default_scope { where(hidden_at: nil) } + scope :hidden, -> { unscope(where: :hidden_at).where.not(hidden_at: nil) } + scope :with_hidden, -> { unscope(where: :hidden_at) } scope :state_brouillon, -> { where(state: states.fetch(:brouillon)) } scope :state_not_brouillon, -> { where.not(state: states.fetch(:brouillon)) } scope :state_en_construction, -> { where(state: states.fetch(:en_construction)) } diff --git a/lib/tasks/2017_09_19_set_confidentialite_to_old_avis.rake b/lib/tasks/2017_09_19_set_confidentialite_to_old_avis.rake index 9579811da..cea377ea1 100644 --- a/lib/tasks/2017_09_19_set_confidentialite_to_old_avis.rake +++ b/lib/tasks/2017_09_19_set_confidentialite_to_old_avis.rake @@ -1,5 +1,5 @@ namespace :'2017_09_19_set_confidentialite_to_old_avis' do task set: :environment do - Avis.unscoped.update_all(confidentiel: true) + Avis.unscope(:joins).update_all(confidentiel: true) end end diff --git a/lib/tasks/2017_09_22_set_dossier_updated_replied_to_initiated.rake b/lib/tasks/2017_09_22_set_dossier_updated_replied_to_initiated.rake index 001672458..ede50d42f 100644 --- a/lib/tasks/2017_09_22_set_dossier_updated_replied_to_initiated.rake +++ b/lib/tasks/2017_09_22_set_dossier_updated_replied_to_initiated.rake @@ -1,5 +1,5 @@ namespace :'2017_09_22_set_dossier_updated_replied_to_initiated' do task set: :environment do - Dossier.unscoped.where(state: [:updated, :replied]).update_all(state: :initiated) + Dossier.with_hidden.where(state: [:updated, :replied]).update_all(state: :initiated) end end diff --git a/lib/tasks/2017_10_18_regenerate_attestation.rake b/lib/tasks/2017_10_18_regenerate_attestation.rake index 32da7d59a..5037d2e1f 100644 --- a/lib/tasks/2017_10_18_regenerate_attestation.rake +++ b/lib/tasks/2017_10_18_regenerate_attestation.rake @@ -13,7 +13,7 @@ namespace :'2017_10_18_regenerate_attestation' do def regenerate_attestations(attestation) Procedure.with_hidden do - Dossier.unscoped do + Dossier.with_hidden do dossier = attestation.dossier procedure = dossier.procedure diff --git a/lib/tasks/2017_12_04_translate_dossier_state_to_french.rake b/lib/tasks/2017_12_04_translate_dossier_state_to_french.rake index 8c54200f2..9b1516ce7 100644 --- a/lib/tasks/2017_12_04_translate_dossier_state_to_french.rake +++ b/lib/tasks/2017_12_04_translate_dossier_state_to_french.rake @@ -1,37 +1,37 @@ namespace :'2017_12_04_translate_dossier_state_to_french' do task brouillon: :environment do - Dossier.unscoped.where(state: 'draft').update_all(state: 'brouillon') + Dossier.with_hidden.where(state: 'draft').update_all(state: 'brouillon') end task en_construction: :environment do - Dossier.unscoped.where(state: 'initiated').update_all(state: 'en_construction') + Dossier.with_hidden.where(state: 'initiated').update_all(state: 'en_construction') end task en_instruction: :environment do - Dossier.unscoped.where(state: 'received').update_all(state: 'en_instruction') + Dossier.with_hidden.where(state: 'received').update_all(state: 'en_instruction') end task accepte: :environment do - Dossier.unscoped.where(state: 'closed').update_all(state: 'accepte') + Dossier.with_hidden.where(state: 'closed').update_all(state: 'accepte') end task refuse: :environment do - Dossier.unscoped.where(state: 'refused').update_all(state: 'refuse') + Dossier.with_hidden.where(state: 'refused').update_all(state: 'refuse') end task sans_suite: :environment do - Dossier.unscoped.where(state: 'without_continuation').update_all(state: 'sans_suite') + Dossier.with_hidden.where(state: 'without_continuation').update_all(state: 'sans_suite') end task all: [:brouillon, :en_construction, :en_instruction, :accepte, :refuse, :sans_suite] do end task revert: :environment do - Dossier.unscoped.where(state: 'brouillon').update_all(state: 'draft') - Dossier.unscoped.where(state: 'en_construction').update_all(state: 'initiated') - Dossier.unscoped.where(state: 'en_instruction').update_all(state: 'received') - Dossier.unscoped.where(state: 'accepte').update_all(state: 'closed') - Dossier.unscoped.where(state: 'refuse').update_all(state: 'refused') - Dossier.unscoped.where(state: 'sans_suite').update_all(state: 'without_continuation') + Dossier.with_hidden.where(state: 'brouillon').update_all(state: 'draft') + Dossier.with_hidden.where(state: 'en_construction').update_all(state: 'initiated') + Dossier.with_hidden.where(state: 'en_instruction').update_all(state: 'received') + Dossier.with_hidden.where(state: 'accepte').update_all(state: 'closed') + Dossier.with_hidden.where(state: 'refuse').update_all(state: 'refused') + Dossier.with_hidden.where(state: 'sans_suite').update_all(state: 'without_continuation') end end diff --git a/lib/tasks/2018_04_04_fetch_etablissement_with_no_entreprise.rake b/lib/tasks/2018_04_04_fetch_etablissement_with_no_entreprise.rake index 6609339fc..4292d7093 100644 --- a/lib/tasks/2018_04_04_fetch_etablissement_with_no_entreprise.rake +++ b/lib/tasks/2018_04_04_fetch_etablissement_with_no_entreprise.rake @@ -4,7 +4,7 @@ namespace :'2018_04_04_fetch_etablissement_with_no_entreprise' do task fetch: :environment do dossiers = Entreprise.joins('LEFT JOIN etablissements et ON entreprises.id = et.entreprise_id') .where('et.id IS NULL') - .map(&:dossier_id).map { |id| Dossier.unscoped.find_by(id: id) }.compact + .map(&:dossier_id).map { |id| Dossier.with_hidden.find_by(id: id) }.compact dossiers.each do |dossier| siret = dossier.entreprise.siret_siege_social diff --git a/lib/tasks/2018_05_15_add_aasm_state_to_procedure.rake b/lib/tasks/2018_05_15_add_aasm_state_to_procedure.rake index 18cd20ee2..2612df82b 100644 --- a/lib/tasks/2018_05_15_add_aasm_state_to_procedure.rake +++ b/lib/tasks/2018_05_15_add_aasm_state_to_procedure.rake @@ -3,6 +3,6 @@ namespace :'2018_05_15_add_aasm_state_to_procedure' do Procedure.archivees.update_all(aasm_state: :archivee) Procedure.publiees.update_all(aasm_state: :publiee) Procedure.brouillons.update_all(aasm_state: :brouillon) - Procedure.rewhere(hidden_at: nil).update_all(aasm_state: :hidden) + Procedure.hidden.update_all(aasm_state: :hidden) end end diff --git a/lib/tasks/2018_06_13_unhide_dossiers.rake b/lib/tasks/2018_06_13_unhide_dossiers.rake index c5221fe74..9351f1c6e 100644 --- a/lib/tasks/2018_06_13_unhide_dossiers.rake +++ b/lib/tasks/2018_06_13_unhide_dossiers.rake @@ -1,6 +1,6 @@ namespace :'2018_06_13_unhide_dossiers' do task run: :environment do - Dossier.unscoped.where.not(hidden_at: nil).state_instruction_commencee.each do |d| + Dossier.hidden.state_instruction_commencee.each do |d| if !d.procedure.nil? # ensure the procedure was not deleted by administrateur for testing d.update(hidden_at: nil) DeletedDossier.find_by(dossier_id: d.id)&.destroy diff --git a/lib/tasks/2019_06_06_fix_timestamps_of_migrated_dossiers.rake b/lib/tasks/2019_06_06_fix_timestamps_of_migrated_dossiers.rake index b563540e6..bfdf51a36 100644 --- a/lib/tasks/2019_06_06_fix_timestamps_of_migrated_dossiers.rake +++ b/lib/tasks/2019_06_06_fix_timestamps_of_migrated_dossiers.rake @@ -4,7 +4,7 @@ namespace :fix_timestamps_of_migrated_dossiers do desc 'Fix the timestamps of dossiers affected by the faulty PJ migration' task run: :environment do affected_time_range = Time.utc(2019, 6, 4, 8, 0)..Time.utc(2019, 6, 4, 18, 0) - dossiers = Dossier.unscoped.includes(:groupe_instructeur).where(groupe_instructeurs: { procedure_id: 0..1000 }).where(updated_at: affected_time_range) + dossiers = Dossier.with_hidden.includes(:groupe_instructeur).where(groupe_instructeurs: { procedure_id: 0..1000 }).where(updated_at: affected_time_range) progress = ProgressReport.new(dossiers.count) diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index 241fc7a81..eb3c59399 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -15,6 +15,24 @@ describe Dossier do it { is_expected.to match_array([dossier]) } end + describe '.hidden' do + let!(:dossier) { create(:dossier) } + let!(:hidden_dossier) { create(:dossier, :hidden) } + + subject { Dossier.all.hidden } + + it { is_expected.to match_array([hidden_dossier]) } + end + + describe '.with_hidden' do + let!(:dossier) { create(:dossier) } + let!(:hidden_dossier) { create(:dossier, :hidden) } + + subject { Dossier.all.with_hidden } + + it { is_expected.to match_array([dossier, hidden_dossier]) } + end + describe '.without_followers' do let!(:dossier_with_follower) { create(:dossier, :followed, :with_entreprise, user: user) } let!(:dossier_without_follower) { create(:dossier, :with_entreprise, user: user) }