show all available tdc for procedure presentation

This commit is contained in:
simon lehericey 2021-06-16 10:48:15 +02:00 committed by Paul Chavard
parent 8d49b5556a
commit eadae7af6b
3 changed files with 126 additions and 11 deletions

View file

@ -87,6 +87,41 @@ class Procedure < ApplicationRecord
brouillon? ? draft_types_de_champ_private : published_types_de_champ_private
end
def types_de_champ_for_procedure_presentation
explanatory_types_de_champ = [:header_section, :explication].map { |k| TypeDeChamp.type_champs.fetch(k) }
if brouillon?
TypeDeChamp
.joins(:revisions)
.where.not(type_champ: explanatory_types_de_champ)
.where(procedure_revisions: { id: draft_revision_id })
.order(:position)
else
# 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
.joins(:revisions)
.where.not(type_champ: explanatory_types_de_champ)
.where(procedure_revisions: { procedure_id: id })
.where.not(procedure_revisions: { id: draft_revision_id })
.group(:stable_id)
.select('MAX(types_de_champ.id)')
# fetch the more recent procedure_revision_types_de_champ
# which includes recents_ids
recents_prtdc = ProcedureRevisionTypeDeChamp
.where(type_de_champ_id: recent_ids)
.where.not(revision_id: draft_revision_id)
.group(:type_de_champ_id)
.select('MAX(id)')
TypeDeChamp
.joins(:revision_types_de_champ)
.where(revision_types_de_champ: { id: recents_prtdc })
.order(:position, 'revision_types_de_champ.revision_id': :desc)
end
end
def types_de_champ_for_tags
if brouillon?
draft_types_de_champ

View file

@ -66,20 +66,10 @@ class ProcedurePresentation < ApplicationRecord
)
end
explanatory_types_de_champ = [:header_section, :explication].map { |k| TypeDeChamp.type_champs.fetch(k) }
fields.concat procedure.types_de_champ
.where.not(type_champ: explanatory_types_de_champ)
.order(:id)
fields.concat procedure.types_de_champ_for_procedure_presentation
.pluck(:libelle, :stable_id)
.map { |(libelle, stable_id)| field_hash(libelle, TYPE_DE_CHAMP, stable_id.to_s) }
fields.concat procedure.types_de_champ_private
.where.not(type_champ: explanatory_types_de_champ)
.order(:id)
.pluck(:libelle, :stable_id)
.map { |(libelle, stable_id)| field_hash(libelle, TYPE_DE_CHAMP_PRIVATE, stable_id.to_s) }
fields
end

View file

@ -0,0 +1,90 @@
describe ProcedurePresentation do
describe "#types_de_champ_for_procedure_presentation" do
subject { procedure.types_de_champ_for_procedure_presentation.pluck(:libelle) }
context 'for a draft procedure' do
let(:procedure) { create(:procedure) }
context 'when there are one tdc on a published revision' do
let!(:tdc) { { type_champ: :number, libelle: 'libelle 1' } }
before { procedure.draft_revision.add_type_de_champ(tdc) }
it { is_expected.to match(['libelle 1']) }
end
end
context 'for a published procedure' do
let(:procedure) { create(:procedure, :published) }
let!(:tdc) { { type_champ: :number, libelle: 'libelle 1' } }
before do
procedure.draft_revision.add_type_de_champ(tdc)
procedure.publish_revision!
end
it { is_expected.to match(['libelle 1']) }
context 'when there is another published revision with an added tdc' do
let!(:added_tdc) { { type_champ: :number, libelle: 'libelle 2' } }
before do
procedure.draft_revision.add_type_de_champ(added_tdc)
procedure.publish_revision!
end
it { is_expected.to match(['libelle 1', 'libelle 2']) }
end
context 'add one tdc above the first one' do
let!(:tdc2) { { type_champ: :number, libelle: 'libelle 2' } }
before do
created_tdc2 = procedure.draft_revision.add_type_de_champ(tdc2)
procedure.draft_revision.move_type_de_champ(created_tdc2.stable_id, 0)
procedure.publish_revision!
end
it { is_expected.to match(['libelle 2', 'libelle 1']) }
context 'and finaly, when this tdc is removed' do
let!(:previous_tdc2) { procedure.published_revision.types_de_champ.find_by(libelle: 'libelle 2') }
before do
procedure.draft_revision.remove_type_de_champ(previous_tdc2.stable_id)
procedure.publish_revision!
end
it { is_expected.to match(['libelle 1', 'libelle 2']) }
end
end
context 'when there is another published revision with a renamed tdc' do
let!(:previous_tdc) { procedure.published_revision.types_de_champ.first }
let!(:changed_tdc) { { type_champ: :number, libelle: 'changed libelle 1' } }
before do
type_de_champ = procedure.draft_revision.find_or_clone_type_de_champ(previous_tdc.id)
type_de_champ.update(changed_tdc)
procedure.publish_revision!
end
it { is_expected.to match(['changed libelle 1']) }
end
context 'when there is another published which removes a previous tdc' do
let!(:previous_tdc) { procedure.published_revision.types_de_champ.first }
before do
type_de_champ = procedure.draft_revision.remove_type_de_champ(previous_tdc.id)
procedure.publish_revision!
end
it { is_expected.to match(['libelle 1']) }
end
end
end
end