From bacdedec2e6fa14c0e11f80b77ca45d361ae49f0 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Wed, 6 Nov 2024 22:29:59 +0100 Subject: [PATCH] refactor(champ): add is_type? method --- app/models/champ.rb | 4 ++-- app/models/columns/champ_column.rb | 2 +- app/models/type_de_champ.rb | 4 ++-- .../multiple_drop_down_list_type_de_champ.rb | 2 +- app/services/dossier_projection_service.rb | 20 +++++++------------ spec/models/columns/champ_column_spec.rb | 12 +++++------ 6 files changed, 19 insertions(+), 25 deletions(-) diff --git a/app/models/champ.rb b/app/models/champ.rb index 8efa30748..548ce15e0 100644 --- a/app/models/champ.rb +++ b/app/models/champ.rb @@ -121,8 +121,8 @@ class Champ < ApplicationRecord TypeDeChamp::CHAMP_TYPE_TO_TYPE_CHAMP.fetch(type) end - def last_write_column_type - TypeDeChamp.column_type(last_write_type_champ) + def is_type?(type_champ) + last_write_type_champ == type_champ end def main_value_name diff --git a/app/models/columns/champ_column.rb b/app/models/columns/champ_column.rb index 329605a15..fa83db9c6 100644 --- a/app/models/columns/champ_column.rb +++ b/app/models/columns/champ_column.rb @@ -24,7 +24,7 @@ class Columns::ChampColumn < Column return if champ.nil? # nominal case - if @tdc_type == champ.last_write_type_champ + if champ.is_type?(@tdc_type) typed_value(champ) else cast_value(champ) diff --git a/app/models/type_de_champ.rb b/app/models/type_de_champ.rb index 62121d949..ffc84c3bf 100644 --- a/app/models/type_de_champ.rb +++ b/app/models/type_de_champ.rb @@ -723,7 +723,7 @@ class TypeDeChamp < ApplicationRecord # no champ return true if champ.nil? # type de champ on the revision changed - if champ.last_write_type_champ == type_champ || castable_on_change?(champ.last_write_type_champ, type_champ) + if champ.is_type?(type_champ) || castable_on_change?(champ.last_write_type_champ, type_champ) dynamic_type.champ_blank?(champ) else true @@ -734,7 +734,7 @@ class TypeDeChamp < ApplicationRecord # no champ return true if champ.nil? # type de champ on the revision changed - if champ.last_write_type_champ == type_champ || castable_on_change?(champ.last_write_type_champ, type_champ) + if champ.is_type?(type_champ) || castable_on_change?(champ.last_write_type_champ, type_champ) mandatory? && dynamic_type.champ_blank_or_invalid?(champ) else true diff --git a/app/models/types_de_champ/multiple_drop_down_list_type_de_champ.rb b/app/models/types_de_champ/multiple_drop_down_list_type_de_champ.rb index e14bd6cfa..1be2be9d5 100644 --- a/app/models/types_de_champ/multiple_drop_down_list_type_de_champ.rb +++ b/app/models/types_de_champ/multiple_drop_down_list_type_de_champ.rb @@ -20,7 +20,7 @@ class TypesDeChamp::MultipleDropDownListTypeDeChamp < TypesDeChamp::TypeDeChampB def selected_options(champ) return [] if champ.value.blank? - if champ.last_write_type_champ == TypeDeChamp.type_champs.fetch(:drop_down_list) + if champ.is_type?(TypeDeChamp.type_champs.fetch(:drop_down_list)) [champ.value] else JSON.parse(champ.value) diff --git a/app/services/dossier_projection_service.rb b/app/services/dossier_projection_service.rb index 40705ed17..3c3f2a438 100644 --- a/app/services/dossier_projection_service.rb +++ b/app/services/dossier_projection_service.rb @@ -80,8 +80,9 @@ class DossierProjectionService fields .filter { |f| f[STABLE_ID] == stable_id } .each do |field| - field[:id_value_h] = champs.to_h { |c| [c.dossier_id, champ_value.(c, field[:original_column])] } - end + column = field[:original_column] + field[:id_value_h] = champs.to_h { [_1.dossier_id, column.is_a?(Columns::JSONPathColumn) ? column.value(_1) : champ_value.(_1)] } + end end when 'self' Dossier @@ -203,17 +204,10 @@ class DossierProjectionService .group_by(&:first) .transform_values { _1.map { |_, type_de_champ| [type_de_champ.stable_id, type_de_champ] }.to_h } stable_ids_and_types_de_champ_by_dossier_ids = revision_ids_by_dossier_ids.transform_values { stable_ids_and_types_de_champ_by_revision_ids[_1] }.compact - -> (champ, column) { - type_de_champ = stable_ids_and_types_de_champ_by_dossier_ids.fetch(champ.dossier_id, {})[champ.stable_id] - if type_de_champ.present? && type_de_champ.type_champ == champ.last_write_type_champ - if column.is_a?(Columns::JSONPathColumn) - column.value(champ) - else - type_de_champ.champ_value(champ) - end - else - '' - end + -> (champ) { + type_de_champ = stable_ids_and_types_de_champ_by_dossier_ids + .fetch(champ.dossier_id, {})[champ.stable_id] + type_de_champ&.champ_value(champ) } end end diff --git a/spec/models/columns/champ_column_spec.rb b/spec/models/columns/champ_column_spec.rb index f197af3cd..681f38ab2 100644 --- a/spec/models/columns/champ_column_spec.rb +++ b/spec/models/columns/champ_column_spec.rb @@ -48,7 +48,7 @@ describe Columns::ChampColumn do def column(label) = procedure.find_column(label:) context 'from a integer_number' do - let(:champ) { double(last_write_type_champ: 'integer_number', value: '42') } + let(:champ) { Champs::IntegerNumberChamp.new(value: '42') } it do expect(column('decimal_number').value(champ)).to eq(42.0) @@ -57,7 +57,7 @@ describe Columns::ChampColumn do end context 'from a decimal_number' do - let(:champ) { double(last_write_type_champ: 'decimal_number', value: '42.1') } + let(:champ) { Champs::DecimalNumberChamp.new(value: '42.1') } it do expect(column('integer_number').value(champ)).to eq(42) @@ -66,7 +66,7 @@ describe Columns::ChampColumn do end context 'from a date' do - let(:champ) { double(last_write_type_champ: 'date', value:) } + let(:champ) { Champs::DateChamp.new(value:) } describe 'when the value is valid' do let(:value) { '2019-07-10' } @@ -82,7 +82,7 @@ describe Columns::ChampColumn do end context 'from a datetime' do - let(:champ) { double(last_write_type_champ: 'datetime', value:) } + let(:champ) { Champs::DatetimeChamp.new(value:) } describe 'when the value is valid' do let(:value) { '1962-09-15T15:35:00+01:00' } @@ -98,7 +98,7 @@ describe Columns::ChampColumn do end context 'from a drop_down_list' do - let(:champ) { double(last_write_type_champ: 'drop_down_list', value: 'val1') } + let(:champ) { Champs::DropDownListChamp.new(value: 'val1') } it do expect(column('multiple_drop_down_list').value(champ)).to eq(['val1']) @@ -107,7 +107,7 @@ describe Columns::ChampColumn do end context 'from a multiple_drop_down_list' do - let(:champ) { double(last_write_type_champ: 'multiple_drop_down_list', value: '["val1","val2"]') } + let(:champ) { Champs::MultipleDropDownListChamp.new(value: '["val1","val2"]') } it do expect(column('simple_drop_down_list').value(champ)).to eq('val1')