extend procedure.all_revision_types_de_champ

for header section

Co-authored-by: mfo <mfo@users.noreply.github.com>
This commit is contained in:
Christophe Robillard 2024-10-25 15:09:18 +02:00 committed by mfo
parent 94b3655ff7
commit 16b8995191
No known key found for this signature in database
GPG key ID: 7CE3E1F5B794A8EC
3 changed files with 52 additions and 7 deletions

View file

@ -71,10 +71,11 @@ class Procedure < ApplicationRecord
brouillon? ? draft_revision : published_revision
end
def all_revisions_types_de_champ(parent: nil)
def all_revisions_types_de_champ(parent: nil, with_header_section: false)
types_de_champ_scope = with_header_section ? TypeDeChamp.with_header_section : TypeDeChamp.fillable
if brouillon?
if parent.nil?
TypeDeChamp.fillable
types_de_champ_scope
.joins(:revision_types_de_champ)
.where(revision_types_de_champ: { revision_id: draft_revision_id, parent_id: nil })
.order(:private, :position)
@ -82,8 +83,8 @@ class Procedure < ApplicationRecord
draft_revision.children_of(parent)
end
else
cache_key = ['all_revisions_types_de_champ', published_revision, parent].compact
Rails.cache.fetch(cache_key, expires_in: 1.month) { published_revisions_types_de_champ(parent) }
cache_key = ['all_revisions_types_de_champ', published_revision, parent, with_header_section].compact
Rails.cache.fetch(cache_key, expires_in: 1.month) { published_revisions_types_de_champ(parent:, with_header_section:) }
end
end
@ -875,7 +876,7 @@ class Procedure < ApplicationRecord
@stable_ids_used_by_routing_rules ||= groupe_instructeurs.flat_map { _1.routing_rule&.sources }.compact.uniq
end
def published_revisions_types_de_champ(parent = nil)
def published_revisions_types_de_champ(parent: nil, with_header_section: false)
# all published revisions
revision_ids = revisions.ids - [draft_revision_id]
# fetch all parent types de champ
@ -889,8 +890,8 @@ class Procedure < ApplicationRecord
# fetch all type_de_champ.stable_id for all the revisions expect draft
# and for each stable_id take the bigger (more recent) type_de_champ.id
recent_ids = TypeDeChamp
.fillable
types_de_champ_scope = with_header_section ? TypeDeChamp.with_header_section : TypeDeChamp.fillable
recent_ids = types_de_champ_scope
.joins(:revision_types_de_champ)
.where(revision_types_de_champ: { revision_id: revision_ids, parent_id: parent_ids })
.group(:stable_id).select('MAX(types_de_champ.id)')

View file

@ -171,6 +171,7 @@ class TypeDeChamp < ApplicationRecord
scope :not_repetition, -> { where.not(type_champ: type_champs.fetch(:repetition)) }
scope :not_condition, -> { where(condition: nil) }
scope :fillable, -> { where.not(type_champ: [type_champs.fetch(:header_section), type_champs.fetch(:explication)]) }
scope :with_header_section, -> { where.not(type_champ: TypeDeChamp.type_champs[:explication]) }
scope :dubious, -> {
where("unaccent(types_de_champ.libelle) ~* unaccent(?)", DubiousProcedure.forbidden_regexp)

View file

@ -1885,6 +1885,49 @@ describe Procedure do
end
end
describe '#all_revisions_types_de_champ' do
let(:types_de_champ_public) do
[
{ type: :text },
{ type: :header_section }
]
end
context 'when procedure brouillon' do
let(:procedure) { create(:procedure, types_de_champ_public:) }
it 'returns one type de champ' do
expect(procedure.all_revisions_types_de_champ.size).to eq 1
end
it 'returns also section type de champ' do
expect(procedure.all_revisions_types_de_champ(with_header_section: true).size).to eq 2
end
it "returns types de champ on draft revision" do
procedure.draft_revision.add_type_de_champ(type_champ: :text, libelle: 'onemorechamp')
expect(procedure.reload.all_revisions_types_de_champ.size).to eq 2
end
end
context 'when procedure is published' do
let(:procedure) { create(:procedure, :published, types_de_champ_public:) }
it 'returns one type de champ' do
expect(procedure.all_revisions_types_de_champ.size).to eq 1
end
it 'returns also section type de champ' do
expect(procedure.all_revisions_types_de_champ(with_header_section: true).size).to eq 2
end
it "doesn't return types de champ on draft revision" do
procedure.draft_revision.add_type_de_champ(type_champ: :text, libelle: 'onemorechamp')
expect(procedure.reload.all_revisions_types_de_champ.size).to eq 1
end
end
end
private
def create_dossier_with_pj_of_size(size, procedure)