fix(attestation_template): add revision diff support

This commit is contained in:
Paul Chavard 2022-02-10 19:42:39 +01:00
parent e269077c40
commit 76b1b85fa7
8 changed files with 199 additions and 66 deletions

View file

@ -122,6 +122,22 @@ class AttestationTemplate < ApplicationRecord
revisions.last&.procedure
end
def logo_checksum
logo.attached? ? logo.checksum : nil
end
def signature_checksum
signature.attached? ? signature.checksum : nil
end
def logo_filename
logo.attached? ? logo.filename : nil
end
def signature_filename
signature.attached? ? signature.filename : nil
end
private
def used_tags

View file

@ -14,7 +14,9 @@ module DossierRebaseConcern
def rebase
attachments_to_purge = []
geo_areas_to_delete = []
changes_by_type_de_champ = revision.compare(procedure.published_revision).group_by { |change| change[:stable_id] }
changes_by_type_de_champ = revision.compare(procedure.published_revision)
.filter { |change| change[:model] == :type_de_champ }
.group_by { |change| change[:stable_id] }
changes_by_type_de_champ.each do |stable_id, changes|
type_de_champ = find_type_de_champ_by_stable_id(stable_id)

View file

@ -108,13 +108,16 @@ class ProcedureRevision < ApplicationRecord
end
def different_from?(revision)
types_de_champ != revision.types_de_champ || types_de_champ_private != revision.types_de_champ_private
types_de_champ != revision.types_de_champ ||
types_de_champ_private != revision.types_de_champ_private ||
attestation_template != revision.attestation_template
end
def compare(revision)
changes = []
changes += compare_types_de_champ(types_de_champ, revision.types_de_champ)
changes += compare_types_de_champ(types_de_champ_private, revision.types_de_champ_private)
changes += compare_attestation_template(attestation_template, revision.attestation_template)
changes
end
@ -127,12 +130,65 @@ class ProcedureRevision < ApplicationRecord
)
end
def attestation_template
super || procedure.attestation_template
end
private
def compare_attestation_template(from_at, to_at)
changes = []
if from_at.nil? && to_at.present?
changes << {
model: :attestation_template,
op: :add
}
elsif to_at.present?
if from_at.title != to_at.title
changes << {
model: :attestation_template,
op: :update,
attribute: :title,
from: from_at.title,
to: to_at.title
}
end
if from_at.body != to_at.body
changes << {
model: :attestation_template,
op: :update,
attribute: :body,
from: from_at.body,
to: to_at.body
}
end
if from_at.footer != to_at.footer
changes << {
model: :attestation_template,
op: :update,
attribute: :footer,
from: from_at.footer,
to: to_at.footer
}
end
if from_at.logo_checksum != to_at.logo_checksum
changes << {
model: :attestation_template,
op: :update,
attribute: :logo,
from: from_at.logo_filename,
to: to_at.logo_filename
}
end
if from_at.signature_checksum != to_at.signature_checksum
changes << {
model: :attestation_template,
op: :update,
attribute: :signature,
from: from_at.signature_filename,
to: to_at.signature_filename
}
end
end
changes
end
def compare_types_de_champ(from_tdc, to_tdc)
if from_tdc == to_tdc
[]
@ -144,11 +200,11 @@ class ProcedureRevision < ApplicationRecord
to_sids = to_h.keys
removed = (from_sids - to_sids).map do |sid|
{ op: :remove, label: from_h[sid].libelle, private: from_h[sid].private?, position: from_sids.index(sid), stable_id: sid }
{ model: :type_de_champ, op: :remove, label: from_h[sid].libelle, private: from_h[sid].private?, position: from_sids.index(sid), stable_id: sid }
end
added = (to_sids - from_sids).map do |sid|
{ op: :add, label: to_h[sid].libelle, private: to_h[sid].private?, position: to_sids.index(sid), stable_id: sid }
{ model: :type_de_champ, op: :add, label: to_h[sid].libelle, private: to_h[sid].private?, position: to_sids.index(sid), stable_id: sid }
end
kept = from_sids.intersection(to_sids)
@ -157,7 +213,7 @@ class ProcedureRevision < ApplicationRecord
.map { |sid| [sid, from_sids.index(sid), to_sids.index(sid)] }
.filter { |_, from_index, to_index| from_index != to_index }
.map do |sid, from_index, to_index|
{ op: :move, label: from_h[sid].libelle, private: from_h[sid].private?, from: from_index, to: to_index, position: to_index, stable_id: sid }
{ model: :type_de_champ, op: :move, label: from_h[sid].libelle, private: from_h[sid].private?, from: from_index, to: to_index, position: to_index, stable_id: sid }
end
changed = kept
@ -177,6 +233,7 @@ class ProcedureRevision < ApplicationRecord
changes = []
if from_type_de_champ.type_champ != to_type_de_champ.type_champ
changes << {
model: :type_de_champ,
op: :update,
attribute: :type_champ,
label: from_type_de_champ.libelle,
@ -188,6 +245,7 @@ class ProcedureRevision < ApplicationRecord
end
if from_type_de_champ.libelle != to_type_de_champ.libelle
changes << {
model: :type_de_champ,
op: :update,
attribute: :libelle,
label: from_type_de_champ.libelle,
@ -199,6 +257,7 @@ class ProcedureRevision < ApplicationRecord
end
if from_type_de_champ.description != to_type_de_champ.description
changes << {
model: :type_de_champ,
op: :update,
attribute: :description,
label: from_type_de_champ.libelle,
@ -210,6 +269,7 @@ class ProcedureRevision < ApplicationRecord
end
if from_type_de_champ.mandatory? != to_type_de_champ.mandatory?
changes << {
model: :type_de_champ,
op: :update,
attribute: :mandatory,
label: from_type_de_champ.libelle,
@ -222,6 +282,7 @@ class ProcedureRevision < ApplicationRecord
if to_type_de_champ.drop_down_list?
if from_type_de_champ.drop_down_list_options != to_type_de_champ.drop_down_list_options
changes << {
model: :type_de_champ,
op: :update,
attribute: :drop_down_options,
label: from_type_de_champ.libelle,
@ -234,6 +295,7 @@ class ProcedureRevision < ApplicationRecord
if to_type_de_champ.linked_drop_down_list?
if from_type_de_champ.drop_down_secondary_libelle != to_type_de_champ.drop_down_secondary_libelle
changes << {
model: :type_de_champ,
op: :update,
attribute: :drop_down_secondary_libelle,
label: from_type_de_champ.libelle,
@ -244,6 +306,7 @@ class ProcedureRevision < ApplicationRecord
end
if from_type_de_champ.drop_down_secondary_description != to_type_de_champ.drop_down_secondary_description
changes << {
model: :type_de_champ,
op: :update,
attribute: :drop_down_secondary_description,
label: from_type_de_champ.libelle,
@ -255,6 +318,7 @@ class ProcedureRevision < ApplicationRecord
end
if from_type_de_champ.drop_down_other != to_type_de_champ.drop_down_other
changes << {
model: :type_de_champ,
op: :update,
attribute: :drop_down_other,
label: from_type_de_champ.libelle,
@ -267,6 +331,7 @@ class ProcedureRevision < ApplicationRecord
elsif to_type_de_champ.carte?
if from_type_de_champ.carte_optional_layers != to_type_de_champ.carte_optional_layers
changes << {
model: :type_de_champ,
op: :update,
attribute: :carte_layers,
label: from_type_de_champ.libelle,
@ -279,6 +344,7 @@ class ProcedureRevision < ApplicationRecord
elsif to_type_de_champ.piece_justificative?
if from_type_de_champ.piece_justificative_template_checksum != to_type_de_champ.piece_justificative_template_checksum
changes << {
model: :type_de_champ,
op: :update,
attribute: :piece_justificative_template,
label: from_type_de_champ.libelle,

View file

@ -0,0 +1,15 @@
- case change[:op]
- when :add
%li.mb-1= t(:add, scope: [:administrateurs, :revision_changes, :attestation_template])
- when :update
- case change[:attribute]
- when :title
%li.mb-1= t(:update_title, scope: [:administrateurs, :revision_changes, :attestation_template], to: change[:to])
- when :body
%li.mb-1= t(:update_body, scope: [:administrateurs, :revision_changes, :attestation_template])
- when :footer
%li.mb-1= t(:update_footer, scope: [:administrateurs, :revision_changes, :attestation_template])
- when :logo
%li.mb-1= t(:update_logo, scope: [:administrateurs, :revision_changes, :attestation_template], to: change[:to])
- when :signature
%li.mb-1= t(:update_signature, scope: [:administrateurs, :revision_changes, :attestation_template], to: change[:to])

View file

@ -0,0 +1,56 @@
- postfix = change[:private] ? '_private' : ''
- case change[:op]
- when :add
%li.mb-1= t("add#{postfix}", label: change[:label], scope: [:administrateurs, :revision_changes])
- when :remove
%li.mb-1= t("remove#{postfix}", label: change[:label], scope: [:administrateurs, :revision_changes])
- when :update
- case change[:attribute]
- when :libelle
%li.mb-1= t("update_libelle#{postfix}", label: change[:label], to: change[:to], scope: [:administrateurs, :revision_changes])
- when :type_champ
%li.mb-1= t("update_type_champ#{postfix}", label: change[:label], to: t("activerecord.attributes.type_de_champ.type_champs.#{change[:to]}"), scope: [:administrateurs, :revision_changes])
- when :description
%li.mb-1= t("update_description#{postfix}", label: change[:label], to: change[:to], scope: [:administrateurs, :revision_changes])
- when :drop_down_secondary_libelle
%li.mb-1= t("update_drop_down_secondary_libelle#{postfix}", label: change[:label], to: change[:to], scope: [:administrateurs, :revision_changes])
- when :drop_down_secondary_description
%li.mb-1= t("update_drop_down_secondary_description#{postfix}", label: change[:label], to: change[:to], scope: [:administrateurs, :revision_changes])
- when :mandatory
- if change[:from] == false
-# i18n-tasks-use t('administrateurs.revision_changes.update_mandatory.enabled')
-# i18n-tasks-use t('administrateurs.revision_changes.update_mandatory_private.enabled')
%li.mb-1= t("administrateurs.revision_changes.update_mandatory#{postfix}.enabled", label: change[:label])
- else
-# i18n-tasks-use t('administrateurs.revision_changes.update_mandatory.disabled')
-# i18n-tasks-use t('administrateurs.revision_changes.update_mandatory_private.disabled')
%li.mb-1= t("administrateurs.revision_changes.update_mandatory#{postfix}.disabled", label: change[:label])
- when :piece_justificative_template
-# i18n-tasks-use t('administrateurs.revision_changes.update_piece_justificative_template')
-# i18n-tasks-use t('administrateurs.revision_changes.update_piece_justificative_template_private')
%li.mb-1= t("administrateurs.revision_changes.update_piece_justificative_template#{postfix}", label: change[:label])
- when :drop_down_options
- added = change[:to].sort - change[:from].sort
- removed = change[:from].sort - change[:to].sort
%li.mb-1
= t("update_drop_down_options#{postfix}", scope: [:administrateurs, :revision_changes], label: change[:label])
%ul
- if added.present?
%li= t(:add_option, scope: [:administrateurs, :revision_changes], items: added.map{ |term| "« #{term.strip} »" }.join(", "))
- if removed.present?
%li= t(:remove_option, scope: [:administrateurs, :revision_changes], items: removed.map{ |term| "« #{term.strip} »" }.join(", "))
- when :drop_down_other
- if change[:from] == false
%li.mb-1= t("administrateurs.revision_changes.update_drop_down_other#{postfix}.enabled", label: change[:label])
- else
%li.mb-1= t("administrateurs.revision_changes.update_drop_down_other#{postfix}.disabled", label: change[:label])
- when :carte_layers
- added = change[:to].sort - change[:from].sort
- removed = change[:from].sort - change[:to].sort
%li.mb-1
= t("update_carte_layers#{postfix}", scope: [:administrateurs, :revision_changes], label: change[:label])
%ul
- if added.present?
%li= t(:add_option, scope: [:administrateurs, :revision_changes], items: added.map{ |term| "« #{t(term, scope: [:administrateurs, :carte_layers])} »" }.join(", "))
- if removed.present?
%li= t(:remove_option, scope: [:administrateurs, :revision_changes], items: removed.map{ |term| "« #{t(term, scope: [:administrateurs, :carte_layers])} »" }.join(", "))

View file

@ -1,61 +1,8 @@
%ul.revision-changes
- changes.each do |change|
- postfix = change[:private] ? '_private' : ''
- case change[:op]
- when :add
%li.mb-1= t("add#{postfix}", label: change[:label], scope: [:administrateurs, :revision_changes])
- when :remove
%li.mb-1= t("remove#{postfix}", label: change[:label], scope: [:administrateurs, :revision_changes])
- when :update
- case change[:attribute]
- when :libelle
%li.mb-1= t("update_libelle#{postfix}", label: change[:label], to: change[:to], scope: [:administrateurs, :revision_changes])
- when :type_champ
%li.mb-1= t("update_type_champ#{postfix}", label: change[:label], to: t("activerecord.attributes.type_de_champ.type_champs.#{change[:to]}"), scope: [:administrateurs, :revision_changes])
- when :description
%li.mb-1= t("update_description#{postfix}", label: change[:label], to: change[:to], scope: [:administrateurs, :revision_changes])
- when :drop_down_secondary_libelle
%li.mb-1= t("update_drop_down_secondary_libelle#{postfix}", label: change[:label], to: change[:to], scope: [:administrateurs, :revision_changes])
- when :drop_down_secondary_description
%li.mb-1= t("update_drop_down_secondary_description#{postfix}", label: change[:label], to: change[:to], scope: [:administrateurs, :revision_changes])
- when :mandatory
- if change[:from] == false
-# i18n-tasks-use t('administrateurs.revision_changes.update_mandatory.enabled')
-# i18n-tasks-use t('administrateurs.revision_changes.update_mandatory_private.enabled')
%li.mb-1= t("administrateurs.revision_changes.update_mandatory#{postfix}.enabled", label: change[:label])
- else
-# i18n-tasks-use t('administrateurs.revision_changes.update_mandatory.disabled')
-# i18n-tasks-use t('administrateurs.revision_changes.update_mandatory_private.disabled')
%li.mb-1= t("administrateurs.revision_changes.update_mandatory#{postfix}.disabled", label: change[:label])
- when :piece_justificative_template
-# i18n-tasks-use t('administrateurs.revision_changes.update_piece_justificative_template')
-# i18n-tasks-use t('administrateurs.revision_changes.update_piece_justificative_template_private')
%li.mb-1= t("administrateurs.revision_changes.update_piece_justificative_template#{postfix}", label: change[:label])
- when :drop_down_options
- added = change[:to].sort - change[:from].sort
- removed = change[:from].sort - change[:to].sort
%li.mb-1
= t("update_drop_down_options#{postfix}", scope: [:administrateurs, :revision_changes], label: change[:label])
%ul
- if added.present?
%li= t(:add_option, scope: [:administrateurs, :revision_changes], items: added.map{ |term| "« #{term.strip} »" }.join(", "))
- if removed.present?
%li= t(:remove_option, scope: [:administrateurs, :revision_changes], items: removed.map{ |term| "« #{term.strip} »" }.join(", "))
- when :drop_down_other
- if change[:from] == false
%li.mb-1= t("administrateurs.revision_changes.update_drop_down_other#{postfix}.enabled", label: change[:label])
- else
%li.mb-1= t("administrateurs.revision_changes.update_drop_down_other#{postfix}.disabled", label: change[:label])
- when :carte_layers
- added = change[:to].sort - change[:from].sort
- removed = change[:from].sort - change[:to].sort
%li.mb-1
= t("update_carte_layers#{postfix}", scope: [:administrateurs, :revision_changes], label: change[:label])
%ul
- if added.present?
%li= t(:add_option, scope: [:administrateurs, :revision_changes], items: added.map{ |term| "« #{t(term, scope: [:administrateurs, :carte_layers])} »" }.join(", "))
- if removed.present?
%li= t(:remove_option, scope: [:administrateurs, :revision_changes], items: removed.map{ |term| "« #{t(term, scope: [:administrateurs, :carte_layers])} »" }.join(", "))
- changes.filter { |change| change[:model] == :attestation_template }.each do |change|
= render partial: 'administrateurs/procedures/revision_change_attestation_template', locals: { change: change }
- changes.filter { |change| change[:model] == :type_de_champ }.each do |change|
= render partial: 'administrateurs/procedures/revision_change_type_de_champ', locals: { change: change }
- move_changes, move_private_changes = changes.filter { |change| change[:op] == :move }.partition { |change| !change[:private] }
- if move_changes.size != 0
%li.mb-1= t(:move, scope: [:administrateurs, :revision_changes], count: move_changes.size)

View file

@ -1,6 +1,13 @@
fr:
administrateurs:
revision_changes:
attestation_template:
add: Un model dattestation à été ajouté
update_title: Le titre de lattestation à été modifié. Le nouveau titre est « %{to} »
update_body: Le corps du document de lattestation à été modifié
update_footer: Le pied de page de lattestation à été modifié
update_logo: Le logo de lattestation à été modifié. Le nouveau logo est « %{to} »
update_signature: La signature de lattestation à été modifié. La nouvelle signature est « %{to} »
has_changes: Modifications en cours (appliqué à la prochaine publication)
add: Le champ « %{label} » a été ajouté
remove: Le champ « %{label} » a été supprimé

View file

@ -172,6 +172,7 @@ describe ProcedureRevision do
expect(procedure.active_revision.different_from?(new_revision)).to be_truthy
expect(procedure.active_revision.compare(new_revision)).to eq([
{
model: :type_de_champ,
op: :add,
label: "Un champ text",
private: false,
@ -182,6 +183,7 @@ describe ProcedureRevision do
new_revision.find_or_clone_type_de_champ(new_revision.types_de_champ.first.stable_id).update(libelle: 'modifier le libelle')
expect(procedure.active_revision.compare(new_revision.reload)).to eq([
{
model: :type_de_champ,
op: :update,
attribute: :libelle,
label: type_de_champ_first.libelle,
@ -191,6 +193,7 @@ describe ProcedureRevision do
stable_id: type_de_champ_first.stable_id
},
{
model: :type_de_champ,
op: :add,
label: "Un champ text",
private: false,
@ -202,6 +205,7 @@ describe ProcedureRevision do
new_revision.move_type_de_champ(new_revision.types_de_champ.second.stable_id, 2)
expect(procedure.active_revision.compare(new_revision.reload)).to eq([
{
model: :type_de_champ,
op: :update,
attribute: :libelle,
label: type_de_champ_first.libelle,
@ -211,12 +215,14 @@ describe ProcedureRevision do
stable_id: type_de_champ_first.stable_id
},
{
model: :type_de_champ,
op: :add,
label: "Un champ text",
private: false,
stable_id: new_type_de_champ.stable_id
},
{
model: :type_de_champ,
op: :move,
label: type_de_champ_second.libelle,
private: false,
@ -230,12 +236,14 @@ describe ProcedureRevision do
new_revision.remove_type_de_champ(new_revision.types_de_champ.first.stable_id)
expect(procedure.active_revision.compare(new_revision.reload)).to eq([
{
model: :type_de_champ,
op: :remove,
label: type_de_champ_first.libelle,
private: false,
stable_id: type_de_champ_first.stable_id
},
{
model: :type_de_champ,
op: :add,
label: "Un champ text",
private: false,
@ -247,18 +255,21 @@ describe ProcedureRevision do
new_revision.find_or_clone_type_de_champ(new_revision.types_de_champ.last.stable_id).update(mandatory: true)
expect(procedure.active_revision.compare(new_revision.reload)).to eq([
{
model: :type_de_champ,
op: :remove,
label: type_de_champ_first.libelle,
private: false,
stable_id: type_de_champ_first.stable_id
},
{
model: :type_de_champ,
op: :add,
label: "Un champ text",
private: false,
stable_id: new_type_de_champ.stable_id
},
{
model: :type_de_champ,
op: :update,
attribute: :description,
label: type_de_champ_second.libelle,
@ -268,6 +279,7 @@ describe ProcedureRevision do
stable_id: type_de_champ_second.stable_id
},
{
model: :type_de_champ,
op: :update,
attribute: :mandatory,
label: type_de_champ_second.libelle,
@ -282,18 +294,21 @@ describe ProcedureRevision do
new_revision.find_or_clone_type_de_champ(new_revision.types_de_champ.last.types_de_champ.first.stable_id).update(drop_down_options: ['one', 'two'])
expect(procedure.active_revision.compare(new_revision.reload)).to eq([
{
model: :type_de_champ,
op: :remove,
label: type_de_champ_first.libelle,
private: false,
stable_id: type_de_champ_first.stable_id
},
{
model: :type_de_champ,
op: :add,
label: "Un champ text",
private: false,
stable_id: new_type_de_champ.stable_id
},
{
model: :type_de_champ,
op: :update,
attribute: :description,
label: type_de_champ_second.libelle,
@ -303,6 +318,7 @@ describe ProcedureRevision do
stable_id: type_de_champ_second.stable_id
},
{
model: :type_de_champ,
op: :update,
attribute: :mandatory,
label: type_de_champ_second.libelle,
@ -312,6 +328,7 @@ describe ProcedureRevision do
stable_id: type_de_champ_second.stable_id
},
{
model: :type_de_champ,
op: :update,
attribute: :type_champ,
label: "sub type de champ",
@ -321,6 +338,7 @@ describe ProcedureRevision do
stable_id: new_revision.types_de_champ.last.types_de_champ.first.stable_id
},
{
model: :type_de_champ,
op: :update,
attribute: :drop_down_options,
label: "sub type de champ",
@ -335,18 +353,21 @@ describe ProcedureRevision do
new_revision.find_or_clone_type_de_champ(new_revision.types_de_champ.last.types_de_champ.first.stable_id).update(options: { cadastres: true, znieff: true })
expect(procedure.active_revision.compare(new_revision.reload)).to eq([
{
model: :type_de_champ,
op: :remove,
label: type_de_champ_first.libelle,
private: false,
stable_id: type_de_champ_first.stable_id
},
{
model: :type_de_champ,
op: :add,
label: "Un champ text",
private: false,
stable_id: new_type_de_champ.stable_id
},
{
model: :type_de_champ,
op: :update,
attribute: :description,
label: type_de_champ_second.libelle,
@ -356,6 +377,7 @@ describe ProcedureRevision do
stable_id: type_de_champ_second.stable_id
},
{
model: :type_de_champ,
op: :update,
attribute: :mandatory,
label: type_de_champ_second.libelle,
@ -365,6 +387,7 @@ describe ProcedureRevision do
stable_id: type_de_champ_second.stable_id
},
{
model: :type_de_champ,
op: :update,
attribute: :type_champ,
label: "sub type de champ",
@ -374,6 +397,7 @@ describe ProcedureRevision do
stable_id: new_revision.types_de_champ.last.types_de_champ.first.stable_id
},
{
model: :type_de_champ,
op: :update,
attribute: :carte_layers,
label: "sub type de champ",