Merge pull request #11027 from tchak/refactor-champ-same-type
refactor(champ): add is_type? method
This commit is contained in:
commit
0617f4c3e8
6 changed files with 19 additions and 25 deletions
|
@ -103,8 +103,8 @@ class Champ < ApplicationRecord
|
||||||
TypeDeChamp::CHAMP_TYPE_TO_TYPE_CHAMP.fetch(type)
|
TypeDeChamp::CHAMP_TYPE_TO_TYPE_CHAMP.fetch(type)
|
||||||
end
|
end
|
||||||
|
|
||||||
def last_write_column_type
|
def is_type?(type_champ)
|
||||||
TypeDeChamp.column_type(last_write_type_champ)
|
last_write_type_champ == type_champ
|
||||||
end
|
end
|
||||||
|
|
||||||
def main_value_name
|
def main_value_name
|
||||||
|
|
|
@ -24,7 +24,7 @@ class Columns::ChampColumn < Column
|
||||||
return if champ.nil?
|
return if champ.nil?
|
||||||
|
|
||||||
# nominal case
|
# nominal case
|
||||||
if @tdc_type == champ.last_write_type_champ
|
if champ.is_type?(@tdc_type)
|
||||||
typed_value(champ)
|
typed_value(champ)
|
||||||
else
|
else
|
||||||
cast_value(champ)
|
cast_value(champ)
|
||||||
|
|
|
@ -615,7 +615,7 @@ class TypeDeChamp < ApplicationRecord
|
||||||
# no champ
|
# no champ
|
||||||
return true if champ.nil?
|
return true if champ.nil?
|
||||||
# type de champ on the revision changed
|
# 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)
|
dynamic_type.champ_blank?(champ)
|
||||||
else
|
else
|
||||||
true
|
true
|
||||||
|
@ -626,7 +626,7 @@ class TypeDeChamp < ApplicationRecord
|
||||||
# no champ
|
# no champ
|
||||||
return true if champ.nil?
|
return true if champ.nil?
|
||||||
# type de champ on the revision changed
|
# 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)
|
mandatory? && dynamic_type.champ_blank_or_invalid?(champ)
|
||||||
else
|
else
|
||||||
true
|
true
|
||||||
|
|
|
@ -20,7 +20,7 @@ class TypesDeChamp::MultipleDropDownListTypeDeChamp < TypesDeChamp::TypeDeChampB
|
||||||
def selected_options(champ)
|
def selected_options(champ)
|
||||||
return [] if champ.value.blank?
|
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]
|
[champ.value]
|
||||||
else
|
else
|
||||||
JSON.parse(champ.value)
|
JSON.parse(champ.value)
|
||||||
|
|
|
@ -80,8 +80,9 @@ class DossierProjectionService
|
||||||
fields
|
fields
|
||||||
.filter { |f| f[STABLE_ID] == stable_id }
|
.filter { |f| f[STABLE_ID] == stable_id }
|
||||||
.each do |field|
|
.each do |field|
|
||||||
field[:id_value_h] = champs.to_h { |c| [c.dossier_id, champ_value.(c, field[:original_column])] }
|
column = field[:original_column]
|
||||||
end
|
field[:id_value_h] = champs.to_h { [_1.dossier_id, column.is_a?(Columns::JSONPathColumn) ? column.value(_1) : champ_value.(_1)] }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
when 'self'
|
when 'self'
|
||||||
Dossier
|
Dossier
|
||||||
|
@ -203,17 +204,10 @@ class DossierProjectionService
|
||||||
.group_by(&:first)
|
.group_by(&:first)
|
||||||
.transform_values { _1.map { |_, type_de_champ| [type_de_champ.stable_id, type_de_champ] }.to_h }
|
.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
|
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) {
|
-> (champ) {
|
||||||
type_de_champ = stable_ids_and_types_de_champ_by_dossier_ids.fetch(champ.dossier_id, {})[champ.stable_id]
|
type_de_champ = stable_ids_and_types_de_champ_by_dossier_ids
|
||||||
if type_de_champ.present? && type_de_champ.type_champ == champ.last_write_type_champ
|
.fetch(champ.dossier_id, {})[champ.stable_id]
|
||||||
if column.is_a?(Columns::JSONPathColumn)
|
type_de_champ&.champ_value(champ)
|
||||||
column.value(champ)
|
|
||||||
else
|
|
||||||
type_de_champ.champ_value(champ)
|
|
||||||
end
|
|
||||||
else
|
|
||||||
''
|
|
||||||
end
|
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -48,7 +48,7 @@ describe Columns::ChampColumn do
|
||||||
def column(label) = procedure.find_column(label:)
|
def column(label) = procedure.find_column(label:)
|
||||||
|
|
||||||
context 'from a integer_number' do
|
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
|
it do
|
||||||
expect(column('decimal_number').value(champ)).to eq(42.0)
|
expect(column('decimal_number').value(champ)).to eq(42.0)
|
||||||
|
@ -57,7 +57,7 @@ describe Columns::ChampColumn do
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'from a decimal_number' do
|
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
|
it do
|
||||||
expect(column('integer_number').value(champ)).to eq(42)
|
expect(column('integer_number').value(champ)).to eq(42)
|
||||||
|
@ -66,7 +66,7 @@ describe Columns::ChampColumn do
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'from a date' do
|
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
|
describe 'when the value is valid' do
|
||||||
let(:value) { '2019-07-10' }
|
let(:value) { '2019-07-10' }
|
||||||
|
@ -82,7 +82,7 @@ describe Columns::ChampColumn do
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'from a datetime' do
|
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
|
describe 'when the value is valid' do
|
||||||
let(:value) { '1962-09-15T15:35:00+01:00' }
|
let(:value) { '1962-09-15T15:35:00+01:00' }
|
||||||
|
@ -98,7 +98,7 @@ describe Columns::ChampColumn do
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'from a drop_down_list' do
|
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
|
it do
|
||||||
expect(column('multiple_drop_down_list').value(champ)).to eq(['val1'])
|
expect(column('multiple_drop_down_list').value(champ)).to eq(['val1'])
|
||||||
|
@ -107,7 +107,7 @@ describe Columns::ChampColumn do
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'from a multiple_drop_down_list' do
|
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
|
it do
|
||||||
expect(column('simple_drop_down_list').value(champ)).to eq('val1')
|
expect(column('simple_drop_down_list').value(champ)).to eq('val1')
|
||||||
|
|
Loading…
Reference in a new issue