models: add a Dossier#with_hidden scope, and remove unscoped usages

This commit is contained in:
Pierre de La Morinerie 2020-01-28 16:55:07 +01:00
parent 1448d5b098
commit 96932faa3f
10 changed files with 39 additions and 19 deletions

View file

@ -95,6 +95,8 @@ class Dossier < ApplicationRecord
end end
default_scope { where(hidden_at: nil) } 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_brouillon, -> { where(state: states.fetch(:brouillon)) }
scope :state_not_brouillon, -> { where.not(state: states.fetch(:brouillon)) } scope :state_not_brouillon, -> { where.not(state: states.fetch(:brouillon)) }
scope :state_en_construction, -> { where(state: states.fetch(:en_construction)) } scope :state_en_construction, -> { where(state: states.fetch(:en_construction)) }

View file

@ -1,5 +1,5 @@
namespace :'2017_09_19_set_confidentialite_to_old_avis' do namespace :'2017_09_19_set_confidentialite_to_old_avis' do
task set: :environment do task set: :environment do
Avis.unscoped.update_all(confidentiel: true) Avis.unscope(:joins).update_all(confidentiel: true)
end end
end end

View file

@ -1,5 +1,5 @@
namespace :'2017_09_22_set_dossier_updated_replied_to_initiated' do namespace :'2017_09_22_set_dossier_updated_replied_to_initiated' do
task set: :environment 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
end end

View file

@ -13,7 +13,7 @@ namespace :'2017_10_18_regenerate_attestation' do
def regenerate_attestations(attestation) def regenerate_attestations(attestation)
Procedure.with_hidden do Procedure.with_hidden do
Dossier.unscoped do Dossier.with_hidden do
dossier = attestation.dossier dossier = attestation.dossier
procedure = dossier.procedure procedure = dossier.procedure

View file

@ -1,37 +1,37 @@
namespace :'2017_12_04_translate_dossier_state_to_french' do namespace :'2017_12_04_translate_dossier_state_to_french' do
task brouillon: :environment 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 end
task en_construction: :environment do 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 end
task en_instruction: :environment do 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 end
task accepte: :environment do task accepte: :environment do
Dossier.unscoped.where(state: 'closed').update_all(state: 'accepte') Dossier.with_hidden.where(state: 'closed').update_all(state: 'accepte')
end end
task refuse: :environment do task refuse: :environment do
Dossier.unscoped.where(state: 'refused').update_all(state: 'refuse') Dossier.with_hidden.where(state: 'refused').update_all(state: 'refuse')
end end
task sans_suite: :environment do 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 end
task all: [:brouillon, :en_construction, :en_instruction, :accepte, :refuse, :sans_suite] do task all: [:brouillon, :en_construction, :en_instruction, :accepte, :refuse, :sans_suite] do
end end
task revert: :environment do task revert: :environment do
Dossier.unscoped.where(state: 'brouillon').update_all(state: 'draft') Dossier.with_hidden.where(state: 'brouillon').update_all(state: 'draft')
Dossier.unscoped.where(state: 'en_construction').update_all(state: 'initiated') Dossier.with_hidden.where(state: 'en_construction').update_all(state: 'initiated')
Dossier.unscoped.where(state: 'en_instruction').update_all(state: 'received') Dossier.with_hidden.where(state: 'en_instruction').update_all(state: 'received')
Dossier.unscoped.where(state: 'accepte').update_all(state: 'closed') Dossier.with_hidden.where(state: 'accepte').update_all(state: 'closed')
Dossier.unscoped.where(state: 'refuse').update_all(state: 'refused') Dossier.with_hidden.where(state: 'refuse').update_all(state: 'refused')
Dossier.unscoped.where(state: 'sans_suite').update_all(state: 'without_continuation') Dossier.with_hidden.where(state: 'sans_suite').update_all(state: 'without_continuation')
end end
end end

View file

@ -4,7 +4,7 @@ namespace :'2018_04_04_fetch_etablissement_with_no_entreprise' do
task fetch: :environment do task fetch: :environment do
dossiers = Entreprise.joins('LEFT JOIN etablissements et ON entreprises.id = et.entreprise_id') dossiers = Entreprise.joins('LEFT JOIN etablissements et ON entreprises.id = et.entreprise_id')
.where('et.id IS NULL') .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| dossiers.each do |dossier|
siret = dossier.entreprise.siret_siege_social siret = dossier.entreprise.siret_siege_social

View file

@ -3,6 +3,6 @@ namespace :'2018_05_15_add_aasm_state_to_procedure' do
Procedure.archivees.update_all(aasm_state: :archivee) Procedure.archivees.update_all(aasm_state: :archivee)
Procedure.publiees.update_all(aasm_state: :publiee) Procedure.publiees.update_all(aasm_state: :publiee)
Procedure.brouillons.update_all(aasm_state: :brouillon) 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
end end

View file

@ -1,6 +1,6 @@
namespace :'2018_06_13_unhide_dossiers' do namespace :'2018_06_13_unhide_dossiers' do
task run: :environment 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 if !d.procedure.nil? # ensure the procedure was not deleted by administrateur for testing
d.update(hidden_at: nil) d.update(hidden_at: nil)
DeletedDossier.find_by(dossier_id: d.id)&.destroy DeletedDossier.find_by(dossier_id: d.id)&.destroy

View file

@ -4,7 +4,7 @@ namespace :fix_timestamps_of_migrated_dossiers do
desc 'Fix the timestamps of dossiers affected by the faulty PJ migration' desc 'Fix the timestamps of dossiers affected by the faulty PJ migration'
task run: :environment do task run: :environment do
affected_time_range = Time.utc(2019, 6, 4, 8, 0)..Time.utc(2019, 6, 4, 18, 0) 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) progress = ProgressReport.new(dossiers.count)

View file

@ -15,6 +15,24 @@ describe Dossier do
it { is_expected.to match_array([dossier]) } it { is_expected.to match_array([dossier]) }
end 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 describe '.without_followers' do
let!(:dossier_with_follower) { create(:dossier, :followed, :with_entreprise, user: user) } let!(:dossier_with_follower) { create(:dossier, :followed, :with_entreprise, user: user) }
let!(:dossier_without_follower) { create(:dossier, :with_entreprise, user: user) } let!(:dossier_without_follower) { create(:dossier, :with_entreprise, user: user) }