diff --git a/app/models/champ.rb b/app/models/champ.rb index 02f82d25a..fb7bf56e9 100644 --- a/app/models/champ.rb +++ b/app/models/champ.rb @@ -99,11 +99,11 @@ class Champ < ApplicationRecord end def mandatory_blank? - mandatory? && blank? + type_de_champ.mandatory_blank?(self) end def blank? - value.blank? + type_de_champ.champ_blank?(self) end def search_terms diff --git a/app/models/champs/carte_champ.rb b/app/models/champs/carte_champ.rb index 49c4e90ac..aa7e1560f 100644 --- a/app/models/champs/carte_champ.rb +++ b/app/models/champs/carte_champ.rb @@ -85,10 +85,6 @@ class Champs::CarteChamp < Champ end end - def blank? - geo_areas.blank? - end - private def selection_utilisateur_legacy_geometry diff --git a/app/models/champs/checkbox_champ.rb b/app/models/champs/checkbox_champ.rb index 2610b6bb0..111eaeb00 100644 --- a/app/models/champs/checkbox_champ.rb +++ b/app/models/champs/checkbox_champ.rb @@ -1,10 +1,6 @@ # frozen_string_literal: true class Champs::CheckboxChamp < Champs::BooleanChamp - def mandatory_blank? - mandatory? && (blank? || !true?) - end - def legend_label? false end @@ -13,11 +9,6 @@ class Champs::CheckboxChamp < Champs::BooleanChamp [[I18n.t('activerecord.attributes.type_de_champ.type_champs.checkbox_true'), true], [I18n.t('activerecord.attributes.type_de_champ.type_champs.checkbox_false'), false]] end - # TODO remove when normalize_checkbox_values is over - def true? - value_with_legacy == TRUE_VALUE - end - def html_label? false end @@ -25,11 +16,4 @@ class Champs::CheckboxChamp < Champs::BooleanChamp def single_checkbox? true end - - private - - # TODO remove when normalize_checkbox_values is over - def value_with_legacy - value == 'on' ? TRUE_VALUE : value - end end diff --git a/app/models/champs/cnaf_champ.rb b/app/models/champs/cnaf_champ.rb index abbca23d1..c0cdc9383 100644 --- a/app/models/champs/cnaf_champ.rb +++ b/app/models/champs/cnaf_champ.rb @@ -8,10 +8,6 @@ class Champs::CnafChamp < Champs::TextChamp store_accessor :value_json, :numero_allocataire, :code_postal - def blank? - external_id.nil? - end - def fetch_external_data? true end diff --git a/app/models/champs/cojo_champ.rb b/app/models/champs/cojo_champ.rb index 14dc41816..a770521eb 100644 --- a/app/models/champs/cojo_champ.rb +++ b/app/models/champs/cojo_champ.rb @@ -20,10 +20,6 @@ class Champs::COJOChamp < Champ accreditation_success == false end - def blank? - accreditation_success != true - end - def fetch_external_data? true end diff --git a/app/models/champs/dgfip_champ.rb b/app/models/champs/dgfip_champ.rb index 3e959659f..c64e55b9a 100644 --- a/app/models/champs/dgfip_champ.rb +++ b/app/models/champs/dgfip_champ.rb @@ -7,10 +7,6 @@ class Champs::DgfipChamp < Champs::TextChamp store_accessor :value_json, :numero_fiscal, :reference_avis - def blank? - external_id.nil? - end - def fetch_external_data? true end diff --git a/app/models/champs/linked_drop_down_list_champ.rb b/app/models/champs/linked_drop_down_list_champ.rb index 7bb349476..65e6c576c 100644 --- a/app/models/champs/linked_drop_down_list_champ.rb +++ b/app/models/champs/linked_drop_down_list_champ.rb @@ -35,11 +35,6 @@ class Champs::LinkedDropDownListChamp < Champ :primary_value end - def blank? - primary_value.blank? || - (has_secondary_options_for_primary? && secondary_value.blank?) - end - def search_terms [primary_value, secondary_value] end diff --git a/app/models/champs/mesri_champ.rb b/app/models/champs/mesri_champ.rb index eab2e5a44..2d8bc2114 100644 --- a/app/models/champs/mesri_champ.rb +++ b/app/models/champs/mesri_champ.rb @@ -4,10 +4,6 @@ class Champs::MesriChamp < Champs::TextChamp # see https://github.com/betagouv/api-particulier/blob/master/src/presentation/middlewares/mesri-input-validation.middleware.ts store_accessor :value_json, :ine - def blank? - external_id.nil? - end - def fetch_external_data? true end diff --git a/app/models/champs/multiple_drop_down_list_champ.rb b/app/models/champs/multiple_drop_down_list_champ.rb index 8ea0bec25..2bc715a06 100644 --- a/app/models/champs/multiple_drop_down_list_champ.rb +++ b/app/models/champs/multiple_drop_down_list_champ.rb @@ -29,10 +29,6 @@ class Champs::MultipleDropDownListChamp < Champ render_as_checkboxes? end - def blank? - selected_options.blank? - end - def in?(options) (selected_options - options).size != selected_options.size end diff --git a/app/models/champs/pays_champ.rb b/app/models/champs/pays_champ.rb index bcd1827b6..a1a6e3d37 100644 --- a/app/models/champs/pays_champ.rb +++ b/app/models/champs/pays_champ.rb @@ -35,10 +35,6 @@ class Champs::PaysChamp < Champs::TextChamp end end - def blank? - value.blank? && external_id.blank? - end - def code external_id || APIGeoService.country_code(value) end diff --git a/app/models/champs/piece_justificative_champ.rb b/app/models/champs/piece_justificative_champ.rb index 5d64239b2..b282aee35 100644 --- a/app/models/champs/piece_justificative_champ.rb +++ b/app/models/champs/piece_justificative_champ.rb @@ -21,12 +21,4 @@ class Champs::PieceJustificativeChamp < Champ def search_terms # We don’t know how to search inside documents yet end - - def mandatory_blank? - mandatory? && !piece_justificative_file.attached? - end - - def blank? - piece_justificative_file.blank? - end end diff --git a/app/models/champs/pole_emploi_champ.rb b/app/models/champs/pole_emploi_champ.rb index 374debacd..5a5d917f4 100644 --- a/app/models/champs/pole_emploi_champ.rb +++ b/app/models/champs/pole_emploi_champ.rb @@ -4,10 +4,6 @@ class Champs::PoleEmploiChamp < Champs::TextChamp # see https://github.com/betagouv/api-particulier/blob/master/src/presentation/middlewares/pole-emploi-input-validation.middleware.ts store_accessor :value_json, :identifiant - def blank? - external_id.nil? - end - def fetch_external_data? true end diff --git a/app/models/champs/repetition_champ.rb b/app/models/champs/repetition_champ.rb index 4bb65c484..e94bbf518 100644 --- a/app/models/champs/repetition_champ.rb +++ b/app/models/champs/repetition_champ.rb @@ -23,10 +23,6 @@ class Champs::RepetitionChamp < Champ rows.last&.first&.focusable_input_id end - def blank? - row_ids.empty? - end - def search_terms # The user cannot enter any information here so it doesn’t make much sense to search end diff --git a/app/models/champs/rnf_champ.rb b/app/models/champs/rnf_champ.rb index 56850c55f..3b9a8fa7a 100644 --- a/app/models/champs/rnf_champ.rb +++ b/app/models/champs/rnf_champ.rb @@ -27,10 +27,6 @@ class Champs::RNFChamp < Champ true end - def blank? - rnf_id.blank? - end - def code_departement address.present? && address['departmentCode'] end diff --git a/app/models/champs/siret_champ.rb b/app/models/champs/siret_champ.rb index bb48c77d9..f124d218d 100644 --- a/app/models/champs/siret_champ.rb +++ b/app/models/champs/siret_champ.rb @@ -6,8 +6,4 @@ class Champs::SiretChamp < Champ def search_terms etablissement.present? ? etablissement.search_terms : [value] end - - def mandatory_blank? - mandatory? && Siret.new(siret: value).invalid? - end end diff --git a/app/models/champs/titre_identite_champ.rb b/app/models/champs/titre_identite_champ.rb index a58cdcdbd..790259f7c 100644 --- a/app/models/champs/titre_identite_champ.rb +++ b/app/models/champs/titre_identite_champ.rb @@ -16,12 +16,4 @@ class Champs::TitreIdentiteChamp < Champ def search_terms # We don’t know how to search inside documents yet end - - def mandatory_blank? - mandatory? && !piece_justificative_file.attached? - end - - def blank? - piece_justificative_file.blank? - end end diff --git a/app/models/type_de_champ.rb b/app/models/type_de_champ.rb index 79bfa7088..b979fdf45 100644 --- a/app/models/type_de_champ.rb +++ b/app/models/type_de_champ.rb @@ -709,7 +709,7 @@ class TypeDeChamp < ApplicationRecord end def champ_value(champ) - if use_default_value?(champ) + if champ_blank?(champ) dynamic_type.champ_default_value else dynamic_type.champ_value(champ) @@ -717,7 +717,7 @@ class TypeDeChamp < ApplicationRecord end def champ_value_for_api(champ, version: 2) - if use_default_value?(champ) + if champ_blank?(champ) dynamic_type.champ_default_api_value(version) else dynamic_type.champ_value_for_api(champ, version:) @@ -725,7 +725,7 @@ class TypeDeChamp < ApplicationRecord end def champ_value_for_export(champ, path = :value) - if use_default_value?(champ) + if champ_blank?(champ) dynamic_type.champ_default_export_value(path) else dynamic_type.champ_value_for_export(champ, path) @@ -733,13 +733,35 @@ class TypeDeChamp < ApplicationRecord end def champ_value_for_tag(champ, path = :value) - if use_default_value?(champ) + if champ_blank?(champ) '' else dynamic_type.champ_value_for_tag(champ, path) end end + def champ_blank?(champ) + # 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) + dynamic_type.champ_blank?(champ) + else + true + end + end + + def mandatory_blank?(champ) + # 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) + mandatory? && dynamic_type.champ_blank_or_invalid?(champ) + else + true + end + end + def html_id(row_id = nil) "champ-#{public_id(row_id)}" end @@ -758,24 +780,12 @@ class TypeDeChamp < ApplicationRecord private - def use_default_value?(champ) - # no champ - return true if champ.nil? - # type de champ on the revision changed - if champ.last_write_type_champ != type_champ - return !castable_on_change?(champ.last_write_type_champ, type_champ) - end - # special case for linked drop down champ – it's blank implementation is not what you think - return champ.value.blank? if type_champ == TypeDeChamp.type_champs.fetch(:linked_drop_down_list) - champ.blank? - end - def castable_on_change?(from_type, to_type) case [from_type, to_type] when ['integer_number', 'decimal_number'], # recast numbers automatically ['decimal_number', 'integer_number'], # may lose some data, but who cares ? ['text', 'textarea'], # allow short text to long text - # ['drop_down_list', 'multiple_drop_down_list'], # single list can become multi + ['drop_down_list', 'multiple_drop_down_list'], # single list can become multi ['date', 'datetime'], # date <=> datetime ['datetime', 'date'] # may lose some data, but who cares ? true diff --git a/app/models/types_de_champ/carte_type_de_champ.rb b/app/models/types_de_champ/carte_type_de_champ.rb index 01cbd2a20..5903c5a2c 100644 --- a/app/models/types_de_champ/carte_type_de_champ.rb +++ b/app/models/types_de_champ/carte_type_de_champ.rb @@ -28,6 +28,8 @@ class TypesDeChamp::CarteTypeDeChamp < TypesDeChamp::TypeDeChampBase champ.geo_areas.map(&:label).join("\n") end + def champ_blank?(champ) = champ.geo_areas.blank? + def columns(procedure_id:, displayable: true, prefix: nil) [] end diff --git a/app/models/types_de_champ/checkbox_type_de_champ.rb b/app/models/types_de_champ/checkbox_type_de_champ.rb index 338352c22..9cf47a847 100644 --- a/app/models/types_de_champ/checkbox_type_de_champ.rb +++ b/app/models/types_de_champ/checkbox_type_de_champ.rb @@ -12,21 +12,17 @@ class TypesDeChamp::CheckboxTypeDeChamp < TypesDeChamp::TypeDeChampBase end def champ_value(champ) - champ.true? ? 'Oui' : 'Non' - end - - def champ_value_for_tag(champ, path = :value) - champ_value(champ) + champ_value_true?(champ) ? 'Oui' : 'Non' end def champ_value_for_export(champ, path = :value) - champ.true? ? 'on' : 'off' + champ_value_true?(champ) ? 'on' : 'off' end def champ_value_for_api(champ, version: 2) case version when 2 - champ.true? ? 'true' : 'false' + champ_value_true?(champ).to_s else super end @@ -48,4 +44,10 @@ class TypesDeChamp::CheckboxTypeDeChamp < TypesDeChamp::TypeDeChampBase nil end end + + def champ_blank_or_invalid?(champ) = !champ_value_true?(champ) + + private + + def champ_value_true?(champ) = champ.value == 'true' end diff --git a/app/models/types_de_champ/cnaf_type_de_champ.rb b/app/models/types_de_champ/cnaf_type_de_champ.rb index 70dafb9a6..9a6d1b58e 100644 --- a/app/models/types_de_champ/cnaf_type_de_champ.rb +++ b/app/models/types_de_champ/cnaf_type_de_champ.rb @@ -4,4 +4,6 @@ class TypesDeChamp::CnafTypeDeChamp < TypesDeChamp::TextTypeDeChamp def estimated_fill_duration(revision) FILL_DURATION_MEDIUM end + + def champ_blank?(champ) = champ.external_id.blank? end diff --git a/app/models/types_de_champ/cojo_type_de_champ.rb b/app/models/types_de_champ/cojo_type_de_champ.rb index d596d6a7c..5d65a80ff 100644 --- a/app/models/types_de_champ/cojo_type_de_champ.rb +++ b/app/models/types_de_champ/cojo_type_de_champ.rb @@ -4,4 +4,6 @@ class TypesDeChamp::COJOTypeDeChamp < TypesDeChamp::TextTypeDeChamp def champ_value(champ) "#{champ.accreditation_number} – #{champ.accreditation_birthdate}" end + + def champ_blank?(champ) = champ.accreditation_success != true end diff --git a/app/models/types_de_champ/dgfip_type_de_champ.rb b/app/models/types_de_champ/dgfip_type_de_champ.rb index ecc107530..7c20a7c7d 100644 --- a/app/models/types_de_champ/dgfip_type_de_champ.rb +++ b/app/models/types_de_champ/dgfip_type_de_champ.rb @@ -4,4 +4,6 @@ class TypesDeChamp::DgfipTypeDeChamp < TypesDeChamp::TextTypeDeChamp def estimated_fill_duration(revision) FILL_DURATION_MEDIUM end + + def champ_blank?(champ) = champ.external_id.blank? end diff --git a/app/models/types_de_champ/linked_drop_down_list_type_de_champ.rb b/app/models/types_de_champ/linked_drop_down_list_type_de_champ.rb index 44333e0b2..577936c9b 100644 --- a/app/models/types_de_champ/linked_drop_down_list_type_de_champ.rb +++ b/app/models/types_de_champ/linked_drop_down_list_type_de_champ.rb @@ -11,11 +11,6 @@ class TypesDeChamp::LinkedDropDownListTypeDeChamp < TypesDeChamp::TypeDeChampBas [[path[:libelle], path[:path]]] end - def add_blank_option_when_not_mandatory(options) - return options if mandatory - options.unshift('') - end - def primary_options primary_options = unpack_options.map(&:first) if primary_options.present? @@ -33,15 +28,15 @@ class TypesDeChamp::LinkedDropDownListTypeDeChamp < TypesDeChamp::TypeDeChampBas end def champ_value(champ) - [champ.primary_value, champ.secondary_value].filter(&:present?).join(' / ') + [primary_value(champ), secondary_value(champ)].filter(&:present?).join(' / ') end def champ_value_for_tag(champ, path = :value) case path when :primary - champ.primary_value + primary_value(champ) when :secondary - champ.secondary_value + secondary_value(champ) when :value champ_value(champ) end @@ -50,23 +45,32 @@ class TypesDeChamp::LinkedDropDownListTypeDeChamp < TypesDeChamp::TypeDeChampBas def champ_value_for_export(champ, path = :value) case path when :primary - champ.primary_value + primary_value(champ) when :secondary - champ.secondary_value + secondary_value(champ) when :value - "#{champ.primary_value || ''};#{champ.secondary_value || ''}" + "#{primary_value(champ) || ''};#{secondary_value(champ) || ''}" end end def champ_value_for_api(champ, version: 2) case version when 1 - { primary: champ.primary_value, secondary: champ.secondary_value } + { primary: primary_value(champ), secondary: secondary_value(champ) } else super end end + def champ_blank?(champ) + primary_value(champ).blank? && secondary_value(champ).blank? + end + + def champ_blank_or_invalid?(champ) + primary_value(champ).blank? || + (has_secondary_options_for_primary?(champ) && secondary_value(champ).blank?) + end + def columns(procedure_id:, displayable: true, prefix: nil) [ Columns::LinkedDropDownColumn.new( @@ -101,6 +105,19 @@ class TypesDeChamp::LinkedDropDownListTypeDeChamp < TypesDeChamp::TypeDeChampBas private + def add_blank_option_when_not_mandatory(options) + return options if mandatory + options.unshift('') + end + + def primary_value(champ) = unpack_value(champ.value, 0) + def secondary_value(champ) = unpack_value(champ.value, 1) + def unpack_value(value, index) = value&.then { JSON.parse(_1)[index] rescue nil } + + def has_secondary_options_for_primary?(champ) + primary_value(champ).present? && secondary_options[primary_value(champ)]&.any?(&:present?) + end + def paths paths = super paths.push({ diff --git a/app/models/types_de_champ/mesri_type_de_champ.rb b/app/models/types_de_champ/mesri_type_de_champ.rb index 3d947c0a1..ff0584800 100644 --- a/app/models/types_de_champ/mesri_type_de_champ.rb +++ b/app/models/types_de_champ/mesri_type_de_champ.rb @@ -4,4 +4,6 @@ class TypesDeChamp::MesriTypeDeChamp < TypesDeChamp::TextTypeDeChamp def estimated_fill_duration(revision) FILL_DURATION_MEDIUM end + + def champ_blank?(champ) = champ.external_id.blank? end 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 3a0d3c2c6..e14bd6cfa 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 @@ -2,14 +2,30 @@ class TypesDeChamp::MultipleDropDownListTypeDeChamp < TypesDeChamp::TypeDeChampBase def champ_value(champ) - champ.selected_options.join(', ') + selected_options(champ).join(', ') end def champ_value_for_tag(champ, path = :value) - ChampPresentations::MultipleDropDownListPresentation.new(champ.selected_options) + ChampPresentations::MultipleDropDownListPresentation.new(selected_options(champ)) end def champ_value_for_export(champ, path = :value) - champ.selected_options.join(', ') + champ_value(champ) + end + + def champ_blank?(champ) = selected_options(champ).blank? + + private + + def selected_options(champ) + return [] if champ.value.blank? + + if champ.last_write_type_champ == TypeDeChamp.type_champs.fetch(:drop_down_list) + [champ.value] + else + JSON.parse(champ.value) + end + rescue JSON::ParserError + [] end end diff --git a/app/models/types_de_champ/pays_type_de_champ.rb b/app/models/types_de_champ/pays_type_de_champ.rb index 54aa9de9b..40d60e1b3 100644 --- a/app/models/types_de_champ/pays_type_de_champ.rb +++ b/app/models/types_de_champ/pays_type_de_champ.rb @@ -23,6 +23,10 @@ class TypesDeChamp::PaysTypeDeChamp < TypesDeChamp::TextTypeDeChamp end end + def champ_blank?(champ) + champ.value.blank? && champ.external_id.blank? + end + private def paths diff --git a/app/models/types_de_champ/piece_justificative_type_de_champ.rb b/app/models/types_de_champ/piece_justificative_type_de_champ.rb index 2c68ef035..5ddd1bef6 100644 --- a/app/models/types_de_champ/piece_justificative_type_de_champ.rb +++ b/app/models/types_de_champ/piece_justificative_type_de_champ.rb @@ -23,6 +23,8 @@ class TypesDeChamp::PieceJustificativeTypeDeChamp < TypesDeChamp::TypeDeChampBas end end + def champ_blank?(champ) = champ.piece_justificative_file.blank? + def columns(procedure_id:, displayable: true, prefix: nil) [] end diff --git a/app/models/types_de_champ/pole_emploi_type_de_champ.rb b/app/models/types_de_champ/pole_emploi_type_de_champ.rb index db6271939..e41113f6a 100644 --- a/app/models/types_de_champ/pole_emploi_type_de_champ.rb +++ b/app/models/types_de_champ/pole_emploi_type_de_champ.rb @@ -4,4 +4,6 @@ class TypesDeChamp::PoleEmploiTypeDeChamp < TypesDeChamp::TextTypeDeChamp def estimated_fill_duration(revision) FILL_DURATION_MEDIUM end + + def champ_blank?(champ) = champ.external_id.blank? end diff --git a/app/models/types_de_champ/repetition_type_de_champ.rb b/app/models/types_de_champ/repetition_type_de_champ.rb index b1325472f..28db1512d 100644 --- a/app/models/types_de_champ/repetition_type_de_champ.rb +++ b/app/models/types_de_champ/repetition_type_de_champ.rb @@ -3,9 +3,7 @@ class TypesDeChamp::RepetitionTypeDeChamp < TypesDeChamp::TypeDeChampBase def champ_value_for_tag(champ, path = :value) return nil if path != :value - return champ_default_value if champ.rows.blank? - - ChampPresentations::RepetitionPresentation.new(champ.libelle, champ.rows) + ChampPresentations::RepetitionPresentation.new(libelle, champ.dossier.project_rows_for(@type_de_champ)) end def estimated_fill_duration(revision) @@ -32,4 +30,6 @@ class TypesDeChamp::RepetitionTypeDeChamp < TypesDeChamp::TypeDeChampBase .all_revisions_types_de_champ(parent: @type_de_champ) .flat_map { _1.columns(procedure_id:, displayable: false, prefix: libelle) } end + + def champ_blank?(champ) = champ.dossier.repetition_row_ids(@type_de_champ).blank? end diff --git a/app/models/types_de_champ/rnf_type_de_champ.rb b/app/models/types_de_champ/rnf_type_de_champ.rb index b93ae872e..aacad9bae 100644 --- a/app/models/types_de_champ/rnf_type_de_champ.rb +++ b/app/models/types_de_champ/rnf_type_de_champ.rb @@ -33,6 +33,8 @@ class TypesDeChamp::RNFTypeDeChamp < TypesDeChamp::TextTypeDeChamp end end + def champ_blank?(champ) = champ.external_id.blank? + private def paths diff --git a/app/models/types_de_champ/siret_type_de_champ.rb b/app/models/types_de_champ/siret_type_de_champ.rb index b7ed7732b..a75a091d2 100644 --- a/app/models/types_de_champ/siret_type_de_champ.rb +++ b/app/models/types_de_champ/siret_type_de_champ.rb @@ -6,4 +6,6 @@ class TypesDeChamp::SiretTypeDeChamp < TypesDeChamp::TypeDeChampBase def estimated_fill_duration(revision) FILL_DURATION_MEDIUM end + + def champ_blank_or_invalid?(champ) = Siret.new(siret: champ.value).invalid? end diff --git a/app/models/types_de_champ/titre_identite_type_de_champ.rb b/app/models/types_de_champ/titre_identite_type_de_champ.rb index 617603c10..0cbd1e906 100644 --- a/app/models/types_de_champ/titre_identite_type_de_champ.rb +++ b/app/models/types_de_champ/titre_identite_type_de_champ.rb @@ -22,6 +22,8 @@ class TypesDeChamp::TitreIdentiteTypeDeChamp < TypesDeChamp::TypeDeChampBase "absent" end + def champ_blank?(champ) = champ.piece_justificative_file.blank? + def columns(procedure_id:, displayable: nil, prefix: nil) [ Columns::TitreIdentiteColumn.new( diff --git a/app/models/types_de_champ/type_de_champ_base.rb b/app/models/types_de_champ/type_de_champ_base.rb index e4babe257..336193dea 100644 --- a/app/models/types_de_champ/type_de_champ_base.rb +++ b/app/models/types_de_champ/type_de_champ_base.rb @@ -92,6 +92,9 @@ class TypesDeChamp::TypeDeChampBase end end + def champ_blank?(champ) = champ.value.blank? + def champ_blank_or_invalid?(champ) = champ_blank?(champ) + def columns(procedure_id:, displayable: true, prefix: nil) if fillable? [ diff --git a/app/models/types_de_champ/yes_no_type_de_champ.rb b/app/models/types_de_champ/yes_no_type_de_champ.rb index 947e31d79..1821ce4cb 100644 --- a/app/models/types_de_champ/yes_no_type_de_champ.rb +++ b/app/models/types_de_champ/yes_no_type_de_champ.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -class TypesDeChamp::YesNoTypeDeChamp < TypesDeChamp::CheckboxTypeDeChamp +class TypesDeChamp::YesNoTypeDeChamp < TypesDeChamp::TypeDeChampBase def filter_to_human(filter_value) if filter_value == "true" I18n.t('activerecord.attributes.type_de_champ.type_champs.yes_no_true') @@ -12,21 +12,17 @@ class TypesDeChamp::YesNoTypeDeChamp < TypesDeChamp::CheckboxTypeDeChamp end def champ_value(champ) - champ_formatted_value(champ) - end - - def champ_value_for_tag(champ, path = :value) - champ_formatted_value(champ) + champ_value_true?(champ) ? 'Oui' : 'Non' end def champ_value_for_export(champ, path = :value) - champ_formatted_value(champ) + champ_value_true?(champ) ? 'Oui' : 'Non' end def champ_value_for_api(champ, version: 2) case version when 2 - champ.true? ? 'true' : 'false' + champ_value_true?(champ).to_s else super end @@ -42,7 +38,7 @@ class TypesDeChamp::YesNoTypeDeChamp < TypesDeChamp::CheckboxTypeDeChamp private - def champ_formatted_value(champ) - champ.true? ? 'Oui' : 'Non' + def champ_value_true?(champ) + champ.value == 'true' end end diff --git a/spec/models/champ_spec.rb b/spec/models/champ_spec.rb index 3c2368f95..dd4598017 100644 --- a/spec/models/champ_spec.rb +++ b/spec/models/champ_spec.rb @@ -5,7 +5,7 @@ describe Champ do describe 'mandatory_blank?' do let(:type_de_champ) { build(:type_de_champ, mandatory: mandatory) } - let(:champ) { Champ.new(value: value) } + let(:champ) { Champs::TextChamp.new(value: value) } let(:value) { '' } let(:mandatory) { true } diff --git a/spec/models/champs/checkbox_champ_spec.rb b/spec/models/champs/checkbox_champ_spec.rb index 6c93e3176..720ae1ec4 100644 --- a/spec/models/champs/checkbox_champ_spec.rb +++ b/spec/models/champs/checkbox_champ_spec.rb @@ -9,12 +9,6 @@ describe Champs::CheckboxChamp do describe '#true?' do subject { boolean_champ.true? } - context "when the checkbox value is 'on'" do - let(:value) { 'on' } - - it { is_expected.to eq(true) } - end - context "when the checkbox value is 'off'" do let(:value) { 'off' } diff --git a/spec/models/type_de_champ_spec.rb b/spec/models/type_de_champ_spec.rb index a0481a31e..605ddb961 100644 --- a/spec/models/type_de_champ_spec.rb +++ b/spec/models/type_de_champ_spec.rb @@ -413,4 +413,66 @@ describe TypeDeChamp do end end end + + describe 'champ_value with cast' do + let(:procedure) { create(:procedure, types_de_champ_public: [{ type: type_champ }]) } + let(:dossier) { create(:dossier, procedure:) } + let(:type_champ) { :text } + let(:last_write_type_champ) { :text } + let(:champ_value) { 'hello' } + let(:champ_type) { TypeDeChamp.type_champ_to_champ_class_name(last_write_type_champ.to_s) } + let(:type_de_champ) { procedure.active_revision.types_de_champ.first } + let(:champ) { dossier.champs.first } + + subject { champ.update_columns(type: champ_type, value: champ_value); type_de_champ.champ_value(champ) } + + it { expect(subject).to eq('hello') } + + context 'text -> integer_number' do + let(:last_write_type_champ) { :text } + let(:type_champ) { :integer_number } + + it { expect(subject).to eq('') } + end + + context 'integer_number -> text' do + let(:last_write_type_champ) { :integer_number } + let(:type_champ) { :text } + let(:champ_value) { '42' } + + it { expect(subject).to eq('') } + end + + context 'integer_number -> decimal_number' do + let(:last_write_type_champ) { :integer_number } + let(:type_champ) { :decimal_number } + let(:champ_value) { '42' } + + it { expect(subject).to eq('42') } + end + + context 'decimal_number -> integer_number' do + let(:last_write_type_champ) { :decimal_number } + let(:type_champ) { :integer_number } + let(:champ_value) { '42.1' } + + it { expect(subject).to eq('42.1') } + end + + context 'drop_down_list -> multiple_drop_down_list' do + let(:last_write_type_champ) { :drop_down_list } + let(:type_champ) { :multiple_drop_down_list } + let(:champ_value) { type_de_champ.drop_down_options.first } + + it { expect(subject).to eq(champ_value) } + end + + context 'multiple_drop_down_list -> drop_down_list' do + let(:last_write_type_champ) { :multiple_drop_down_list } + let(:type_champ) { :drop_down_list } + let(:champ_value) { "[\"#{type_de_champ.drop_down_options.first}\"]" } + + it { expect(subject).to eq('') } + end + end end