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

This avoids the broad use of `unscoped` to remove the `hidden_at`
clause.
This commit is contained in:
Pierre de La Morinerie 2020-01-28 16:41:15 +01:00
parent 4efea77280
commit 1448d5b098
7 changed files with 28 additions and 6 deletions

View file

@ -12,7 +12,7 @@ module Manager
Procedure Procedure
else else
# … but allow them to be searched and displayed. # … but allow them to be searched and displayed.
Procedure.unscope(:where) Procedure.with_hidden
end end
end end

View file

@ -45,6 +45,8 @@ class Procedure < ApplicationRecord
accepts_nested_attributes_for :types_de_champ_private, reject_if: proc { |attributes| attributes['libelle'].blank? }, allow_destroy: true accepts_nested_attributes_for :types_de_champ_private, reject_if: proc { |attributes| attributes['libelle'].blank? }, allow_destroy: true
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 :brouillons, -> { where(aasm_state: :brouillon) } scope :brouillons, -> { where(aasm_state: :brouillon) }
scope :publiees, -> { where(aasm_state: :publiee) } scope :publiees, -> { where(aasm_state: :publiee) }
scope :closes, -> { where(aasm_state: [:close, :depubliee]) } scope :closes, -> { where(aasm_state: [:close, :depubliee]) }

View file

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

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.unscoped.where.not(hidden_at: nil).update_all(aasm_state: :hidden) Procedure.rewhere(hidden_at: nil).update_all(aasm_state: :hidden)
end end
end end

View file

@ -3,11 +3,11 @@ namespace :after_party do
task create_dummy_paths_for_archived_and_hidden_procedures: :environment do task create_dummy_paths_for_archived_and_hidden_procedures: :environment do
rake_puts "Running deploy task 'create_dummy_paths_for_archived_procedures'" rake_puts "Running deploy task 'create_dummy_paths_for_archived_procedures'"
Procedure.unscoped.archivees.where(path: nil).each do |p| Procedure.with_hidden.archivees.where(path: nil).each do |p|
p.update_column(:path, SecureRandom.uuid) p.update_column(:path, SecureRandom.uuid)
end end
Procedure.unscoped.hidden.where(path: nil).each do |p| Procedure.hidden.where(path: nil).each do |p|
p.update_column(:path, SecureRandom.uuid) p.update_column(:path, SecureRandom.uuid)
end end

View file

@ -2,7 +2,7 @@ namespace :after_party do
desc 'Deployment task: create_default_groupe_instructeur' desc 'Deployment task: create_default_groupe_instructeur'
task create_default_groupe_instructeur: :environment do task create_default_groupe_instructeur: :environment do
Procedure Procedure
.unscoped .with_hidden
.left_outer_joins(:groupe_instructeurs) .left_outer_joins(:groupe_instructeurs)
.where('groupe_instructeurs.id is null') .where('groupe_instructeurs.id is null')
.find_each do |procedure| .find_each do |procedure|

View file

@ -151,6 +151,26 @@ describe Procedure do
end end
end end
describe 'scopes' do
let!(:procedure) { create(:procedure) }
let!(:hidden_procedure) { create(:procedure, :hidden) }
describe 'default_scope' do
subject { Procedure.all }
it { is_expected.to match_array([procedure]) }
end
describe '.hidden' do
subject { Procedure.all.hidden }
it { is_expected.to match_array([hidden_procedure]) }
end
describe '.with_hidden' do
subject { Procedure.all.with_hidden }
it { is_expected.to match_array([procedure, hidden_procedure]) }
end
end
describe 'validation' do describe 'validation' do
context 'libelle' do context 'libelle' do
it { is_expected.not_to allow_value(nil).for(:libelle) } it { is_expected.not_to allow_value(nil).for(:libelle) }