diff --git a/app/models/logic/champ_value.rb b/app/models/logic/champ_value.rb index e9ca6e0c8..4560953d7 100644 --- a/app/models/logic/champ_value.rb +++ b/app/models/logic/champ_value.rb @@ -33,7 +33,7 @@ class Logic::ChampValue < Logic::Term end end - def to_s = "#{type_de_champ&.libelle} Nº#{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é diff --git a/app/models/procedure_revision.rb b/app/models/procedure_revision.rb index c9c9c254f..1762ea6f1 100644 --- a/app/models/procedure_revision.rb +++ b/app/models/procedure_revision.rb @@ -377,6 +377,30 @@ class ProcedureRevision < ApplicationRecord stable_id: from_type_de_champ.stable_id } end + + from_condition = from_type_de_champ.condition + to_condition = to_type_de_champ.condition + if from_condition != to_condition + condition_change = { + 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 + } + if from_condition.nil? + condition_change[:condition_op] = :add + elsif to_condition.nil? + condition_change[:condition_op] = :remove + else + condition_change[:condition_op] = :update + end + changes << condition_change + 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 << { diff --git a/app/views/administrateurs/conditions/_update.turbo_stream.haml b/app/views/administrateurs/conditions/_update.turbo_stream.haml index d5dcb27a6..5956947fd 100644 --- a/app/views/administrateurs/conditions/_update.turbo_stream.haml +++ b/app/views/administrateurs/conditions/_update.turbo_stream.haml @@ -1,8 +1,8 @@ - rendered = render @condition_component - if rendered.present? - = turbo_stream.morph dom_id(@tdc, :conditions) do + = turbo_stream.morph "conditions_type_de_champ_#{@tdc.stable_id}" do - rendered end - else - = turbo_stream.remove dom_id(@tdc, :conditions) + = turbo_stream.remove "conditions_type_de_champ_#{@tdc.stable_id}" diff --git a/app/views/administrateurs/procedures/_revision_change_type_de_champ.html.haml b/app/views/administrateurs/procedures/_revision_change_type_de_champ.html.haml index dd043c6f7..23a7feb03 100644 --- a/app/views/administrateurs/procedures/_revision_change_type_de_champ.html.haml +++ b/app/views/administrateurs/procedures/_revision_change_type_de_champ.html.haml @@ -54,3 +54,11 @@ %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 + - case change[:condition_op] + - when :add + %li= t(:add_condition, scope: [:administrateurs, :revision_changes], label: change[:label], to: change[:to]) + - when :remove + %li= t(:remove_condition, scope: [:administrateurs, :revision_changes], label: change[:label], to: change[:to]) + - when :update + %li= t(:update_condition, scope: [:administrateurs, :revision_changes], label: change[:label], to: change[:to]) diff --git a/config/locales/views/administrateurs/revision_changes/fr.yml b/config/locales/views/administrateurs/revision_changes/fr.yml index 9c9e8b9e6..595f566fe 100644 --- a/config/locales/views/administrateurs/revision_changes/fr.yml +++ b/config/locales/views/administrateurs/revision_changes/fr.yml @@ -46,6 +46,9 @@ fr: update_carte_layers_private: Les référentiels cartographiques de l’annotation 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 diff --git a/spec/models/procedure_revision_spec.rb b/spec/models/procedure_revision_spec.rb index 2f524de82..73fb53151 100644 --- a/spec/models/procedure_revision_spec.rb +++ b/spec/models/procedure_revision_spec.rb @@ -322,11 +322,98 @@ 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, + :condition_op => :add, + :from => "", + :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, + :condition_op => :remove, + :from => "(l1 == 2)", + :label => "l2", + :model => :type_de_champ, + :op => :update, + :private => false, + :stable_id => second_tdc.stable_id, + :to => "" + } + ]) + 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, + :condition_op => :update, + :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