refactor(champ): add is_type? method

This commit is contained in:
Paul Chavard 2024-11-06 22:29:59 +01:00
parent f2b33677f8
commit bacdedec2e
No known key found for this signature in database
6 changed files with 19 additions and 25 deletions

View file

@ -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

View file

@ -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)

View file

@ -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

View file

@ -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)

View file

@ -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

View file

@ -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')