Merge pull request #7569 from betagouv/make_condition_revisable

feat(condition): rend les modifications de conditions visibles entre révisions
This commit is contained in:
LeSim 2022-07-13 16:12:09 +02:00 committed by GitHub
commit ee545a82ca
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 122 additions and 9 deletions

View file

@ -22,7 +22,7 @@ class TypesDeChampEditor::ConditionsComponent < ApplicationComponent
def logic_conditionnel_button
if @condition.nil?
submit_tag(t('.enable_conditionnel'), formaction: add_row_admin_procedure_condition_path(@procedure_id, @tdc.id))
submit_tag(t('.enable_conditionnel'), formaction: add_row_admin_procedure_condition_path(@procedure_id, @tdc.stable_id))
else
submit_tag(
t('.disable_conditionnel'),
@ -185,7 +185,7 @@ class TypesDeChampEditor::ConditionsComponent < ApplicationComponent
def add_condition_tag
tag.button(
tag.span('', class: 'icon add') + tag.span(t('.add_condition')),
formaction: add_row_admin_procedure_condition_path(@procedure_id, @tdc.id),
formaction: add_row_admin_procedure_condition_path(@procedure_id, @tdc.stable_id),
formnovalidate: true,
class: 'add-row'
)
@ -194,7 +194,7 @@ class TypesDeChampEditor::ConditionsComponent < ApplicationComponent
def delete_condition_tag(row_index)
tag.button(
tag.span('', class: 'icon delete') + tag.span(t('.remove_a_row'), class: 'sr-only'),
formaction: delete_row_admin_procedure_condition_path(@procedure_id, @tdc.id, row_index: row_index),
formaction: delete_row_admin_procedure_condition_path(@procedure_id, @tdc.stable_id, row_index: row_index),
formmethod: 'delete',
formnovalidate: true
)
@ -209,7 +209,7 @@ class TypesDeChampEditor::ConditionsComponent < ApplicationComponent
end
def input_id_for(name, row_index)
"#{@tdc.id}-#{name}-#{row_index}"
"#{@tdc.stable_id}-#{name}-#{row_index}"
end
def input_prefix

View file

@ -1,5 +1,5 @@
.flex.justify-start.section{ id: dom_id(@tdc, :conditions) }
= form_with url: admin_procedure_condition_path(@procedure_id, @tdc), method: :patch, class: 'form width-100' do |f|
.flex.justify-start.section{ id: dom_id(@tdc.stable_self, :conditions) }
= form_with url: admin_procedure_condition_path(@procedure_id, @tdc.stable_id), method: :patch, class: 'form width-100' do |f|
.conditionnel.mt-2.width-100
.flex
%p.mr-2 Logique conditionnelle

View file

@ -33,7 +33,7 @@ class Logic::ChampValue < Logic::Term
end
end
def to_s = "#{type_de_champ&.libelle}#{stable_id}" # TODO: gerer le cas ou un tdc est supprimé
def to_s = type_de_champ&.libelle # TODO: gerer le cas ou un tdc est supprimé
def type
case type_de_champ&.type_champ # TODO: gerer le cas ou un tdc est supprimé

View file

@ -377,6 +377,20 @@ class ProcedureRevision < ApplicationRecord
stable_id: from_type_de_champ.stable_id
}
end
if from_type_de_champ.condition != to_type_de_champ.condition
changes << {
model: :type_de_champ,
op: :update,
attribute: :condition,
label: from_type_de_champ.libelle,
private: from_type_de_champ.private?,
from: from_type_de_champ.condition&.to_s,
to: to_type_de_champ.condition&.to_s,
stable_id: from_type_de_champ.stable_id
}
end
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 << {

View file

@ -360,6 +360,11 @@ class TypeDeChamp < ApplicationRecord
end
end
def stable_self
OpenStruct.new(to_key: [stable_id],
model_name: OpenStruct.new(param_key: model_name.param_key))
end
private
DEFAULT_EMPTY = ['']

View file

@ -1,8 +1,8 @@
- rendered = render @condition_component
- if rendered.present?
= turbo_stream.morph dom_id(@tdc, :conditions) do
= turbo_stream.morph dom_id(@tdc.stable_self, :conditions) do
- rendered
end
- else
= turbo_stream.remove dom_id(@tdc, :conditions)
= turbo_stream.remove dom_id(@tdc.stable_self, :conditions)

View file

@ -54,3 +54,10 @@
%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(", "))
- when :condition
- if change[:from].nil?
%li= t(:add_condition, scope: [:administrateurs, :revision_changes], label: change[:label], to: change[:to])
- elsif change[:to].nil?
%li= t(:remove_condition, scope: [:administrateurs, :revision_changes], label: change[:label])
- else
%li= t(:update_condition, scope: [:administrateurs, :revision_changes], label: change[:label], to: change[:to])

View file

@ -46,6 +46,9 @@ fr:
update_carte_layers_private: Les référentiels cartographiques de lannotation privée « %{label} » ont été modifiés
add_option: "ajoutés : %{items}"
remove_option: "supprimés : %{items}"
add_condition: Une condition a été ajoutée sur le champ « %{label} ». La nouvelle condition est « %{to} »
remove_condition: La condition du champ « %{label} » a été supprimée.
update_condition: La condition du champ « %{label} » a été modifiée. La nouvelle condition est « %{to} »
carte_layers:
unesco: UNESCO
arretes_protection: Arrêtés de protection

View file

@ -322,11 +322,95 @@ describe ProcedureRevision do
end
describe '#compare' do
include Logic
let(:first_tdc) { draft.types_de_champ_public.first }
let(:second_tdc) { draft.types_de_champ_public.second }
let(:new_draft) { procedure.create_new_revision }
subject { procedure.active_revision.compare(new_draft.reload) }
context 'with a procedure with 2 tdcs' do
let(:procedure) do
create(:procedure).tap do |p|
p.draft_revision.add_type_de_champ(type_champ: :integer_number, libelle: 'l1')
p.draft_revision.add_type_de_champ(type_champ: :text, libelle: 'l2')
end
end
context 'when a condition is added' do
before do
second = new_draft.find_and_ensure_exclusive_use(second_tdc.stable_id)
second.update(condition: ds_eq(champ_value(first_tdc.stable_id), constant(3)))
end
it do
is_expected.to eq([
{
attribute: :condition,
from: nil,
label: "l2",
model: :type_de_champ,
op: :update,
private: false,
stable_id: second_tdc.stable_id,
to: "(l1 == 3)"
}
])
end
end
context 'when a condition is removed' do
before do
second_tdc.update(condition: ds_eq(champ_value(first_tdc.stable_id), constant(2)))
draft.reload
second = new_draft.find_and_ensure_exclusive_use(second_tdc.stable_id)
second.update(condition: nil)
end
it do
is_expected.to eq([
{
attribute: :condition,
from: "(l1 == 2)",
label: "l2",
model: :type_de_champ,
op: :update,
private: false,
stable_id: second_tdc.stable_id,
to: nil
}
])
end
end
context 'when a condition is changed' do
before do
second_tdc.update(condition: ds_eq(champ_value(first_tdc.stable_id), constant(2)))
draft.reload
second = new_draft.find_and_ensure_exclusive_use(second_tdc.stable_id)
second.update(condition: ds_eq(champ_value(first_tdc.stable_id), constant(3)))
end
it do
is_expected.to eq([
{
attribute: :condition,
from: "(l1 == 2)",
label: "l2",
model: :type_de_champ,
op: :update,
private: false,
stable_id: second_tdc.stable_id,
to: "(l1 == 3)"
}
])
end
end
end
context 'when a type de champ is added' do
let(:procedure) { create(:procedure) }
let(:new_tdc) do