refactor(export): remove old formatting code from champs

This commit is contained in:
Paul Chavard 2024-04-23 10:02:42 +02:00
parent 99834e0cf5
commit 371b8b0b46
22 changed files with 1 additions and 373 deletions

View file

@ -38,36 +38,6 @@ class Champs::AddressChamp < Champs::TextChamp
end end
end end
def to_s
address_label.presence || ''
end
def for_tag(path = :value)
case path
when :value
address_label
when :departement
departement_code_and_name || ''
when :commune
commune_name || ''
end
end
def for_export(path = :value)
case path
when :value
value.present? ? address_label : nil
when :departement
departement_code_and_name
when :commune
commune_name
end
end
def for_api
address_label
end
def code_departement def code_departement
if full_address? if full_address?
address.fetch('department_code') address.fetch('department_code')

View file

@ -17,28 +17,8 @@ class Champs::BooleanChamp < Champ
end end
end end
def to_s
processed_value
end
def for_tag(path = :value)
processed_value
end
def for_export(path = :value)
processed_value
end
def for_api_v2
true? ? 'true' : 'false'
end
private private
def processed_value
true? ? 'Oui' : 'Non'
end
def set_value_to_nil def set_value_to_nil
self.value = nil self.value = nil
end end

View file

@ -83,14 +83,6 @@ class Champs::CarteChamp < Champ
end end
end end
def for_api
nil
end
def for_export(path = :value)
geo_areas.map(&:label).join("\n")
end
def blank? def blank?
geo_areas.blank? geo_areas.blank?
end end

View file

@ -1,8 +1,4 @@
class Champs::CheckboxChamp < Champs::BooleanChamp class Champs::CheckboxChamp < Champs::BooleanChamp
def for_export(path = :value)
true? ? 'on' : 'off'
end
def mandatory_blank? def mandatory_blank?
mandatory? && (blank? || !true?) mandatory? && (blank? || !true?)
end end

View file

@ -34,10 +34,6 @@ class Champs::COJOChamp < Champ
COJOService.new.(accreditation_number:, accreditation_birthdate:) COJOService.new.(accreditation_number:, accreditation_birthdate:)
end end
def to_s
"#{accreditation_number} #{accreditation_birthdate}"
end
def accreditation_number_input_id def accreditation_number_input_id
"#{input_id}-accreditation_number" "#{input_id}-accreditation_number"
end end

View file

@ -2,28 +2,6 @@ class Champs::CommuneChamp < Champs::TextChamp
store_accessor :value_json, :code_departement, :code_postal, :code_region store_accessor :value_json, :code_departement, :code_postal, :code_region
before_save :on_codes_change, if: :should_refresh_after_code_change? before_save :on_codes_change, if: :should_refresh_after_code_change?
def for_export(path = :value)
case path
when :value
to_s
when :departement
departement_code_and_name || ''
when :code
code || ''
end
end
def for_tag(path = :value)
case path
when :value
to_s
when :departement
departement_code_and_name || ''
when :code
code || ''
end
end
def departement_name def departement_name
APIGeoService.departement_name(code_departement) APIGeoService.departement_name(code_departement)
end end
@ -60,10 +38,6 @@ class Champs::CommuneChamp < Champs::TextChamp
APIGeoService.safely_normalize_city_name(code_departement, code, safe_to_s) APIGeoService.safely_normalize_city_name(code_departement, code, safe_to_s)
end end
def to_s
code_postal? ? "#{name} (#{code_postal})" : name
end
def code def code
external_id external_id
end end

View file

@ -6,16 +6,6 @@ class Champs::DateChamp < Champ
# Text search is pretty useless for dates so were not including these champs # Text search is pretty useless for dates so were not including these champs
end end
def to_s
value.present? ? I18n.l(Time.zone.parse(value), format: '%d %B %Y') : ""
rescue ArgumentError
value.presence || "" # old dossiers can have not parseable dates
end
def for_tag(path = :value)
to_s if path == :value
end
private private
def convert_to_iso8601 def convert_to_iso8601

View file

@ -6,14 +6,6 @@ class Champs::DatetimeChamp < Champ
# Text search is pretty useless for datetimes so were not including these champs # Text search is pretty useless for datetimes so were not including these champs
end end
def to_s
value.present? ? I18n.l(Time.zone.parse(value)) : ""
end
def for_tag(path = :value)
value.present? ? I18n.l(Time.zone.parse(value)) : ""
end
private private
def convert_to_iso8601 def convert_to_iso8601

View file

@ -17,14 +17,6 @@ class Champs::DecimalNumberChamp < Champ
} }
}, if: :validate_champ_value_or_prefill? }, if: :validate_champ_value_or_prefill?
def for_export(path = :value)
processed_value
end
def for_api
processed_value
end
private private
def format_value def format_value
@ -32,10 +24,4 @@ class Champs::DecimalNumberChamp < Champ
self.value = value.tr(",", ".") self.value = value.tr(",", ".")
end end
def processed_value
return unless valid_champ_value?
value&.to_f
end
end end

View file

@ -5,36 +5,6 @@ class Champs::DepartementChamp < Champs::TextChamp
validate :external_id_in_departement_codes, if: -> { validate_champ_value_or_prefill? && !external_id.nil? } validate :external_id_in_departement_codes, if: -> { validate_champ_value_or_prefill? && !external_id.nil? }
before_save :store_code_region before_save :store_code_region
def for_export(path = :value)
case path
when :code
code
when :value
name
end
end
def to_s
formatted_value
end
def for_tag(path = :value)
case path
when :code
code
when :value
formatted_value
end
end
def for_api
formatted_value
end
def for_api_v2
formatted_value.tr('', '-')
end
def selected def selected
code code
end end
@ -71,10 +41,6 @@ class Champs::DepartementChamp < Champs::TextChamp
private private
def formatted_value
blank? ? "" : "#{code} #{name}"
end
def value_in_departement_names def value_in_departement_names
return if value.in?(APIGeoService.departements.pluck(:name)) return if value.in?(APIGeoService.departements.pluck(:name))

View file

@ -7,28 +7,6 @@ class Champs::EpciChamp < Champs::TextChamp
validate :external_id_in_departement_epci_codes, if: -> { !(code_departement.nil? || external_id.nil?) && validate_champ_value_or_prefill? } validate :external_id_in_departement_epci_codes, if: -> { !(code_departement.nil? || external_id.nil?) && validate_champ_value_or_prefill? }
validate :value_in_departement_epci_names, if: -> { !(code_departement.nil? || external_id.nil? || value.nil?) && validate_champ_value_or_prefill? } validate :value_in_departement_epci_names, if: -> { !(code_departement.nil? || external_id.nil? || value.nil?) && validate_champ_value_or_prefill? }
def for_export(path = :value)
case path
when :value
value
when :code
code
when :departement
departement_code_and_name
end
end
def for_tag(path = :value)
case path
when :value
value
when :code
code
when :departement
departement_code_and_name
end
end
def departement_name def departement_name
APIGeoService.departement_name(code_departement) APIGeoService.departement_name(code_departement)
end end

View file

@ -8,20 +8,4 @@ class Champs::IntegerNumberChamp < Champ
object.errors.generate_message(:value, :not_an_integer) object.errors.generate_message(:value, :not_an_integer)
} }
}, if: :validate_champ_value_or_prefill? }, if: :validate_champ_value_or_prefill?
def for_export(path = :value)
processed_value
end
def for_api
processed_value
end
private
def processed_value
return unless valid_champ_value?
value&.to_i
end
end end

View file

@ -37,36 +37,6 @@ class Champs::LinkedDropDownListChamp < Champ
:primary_value :primary_value
end end
def to_s
value.present? ? [primary_value, secondary_value].filter(&:present?).join(' / ') : ""
end
def for_tag(path = :value)
case path
when :primary
primary_value
when :secondary
secondary_value
when :value
value.present? ? [primary_value, secondary_value].filter(&:present?).join(' / ') : ""
end
end
def for_export(path = :value)
case path
when :primary
primary_value
when :secondary
secondary_value
when :value
value.present? ? "#{primary_value || ''};#{secondary_value || ''}" : nil
end
end
def for_api
value.present? ? { primary: primary_value, secondary: secondary_value } : nil
end
def blank? def blank?
primary_value.blank? || primary_value.blank? ||
(has_secondary_options_for_primary? && secondary_value.blank?) (has_secondary_options_for_primary? && secondary_value.blank?)

View file

@ -19,18 +19,6 @@ class Champs::MultipleDropDownListChamp < Champ
value.blank? ? [] : JSON.parse(value) value.blank? ? [] : JSON.parse(value)
end end
def to_s
selected_options.join(', ')
end
def for_tag(path = :value)
selected_options.join(', ')
end
def for_export(path = :value)
value.present? ? selected_options.join(', ') : nil
end
def render_as_checkboxes? def render_as_checkboxes?
enabled_non_empty_options.size <= THRESHOLD_NB_OPTIONS_AS_CHECKBOX enabled_non_empty_options.size <= THRESHOLD_NB_OPTIONS_AS_CHECKBOX
end end

View file

@ -11,28 +11,6 @@ class Champs::PaysChamp < Champs::TextChamp
validates :value, inclusion: APIGeoService.countries.pluck(:name), allow_nil: false, allow_blank: false validates :value, inclusion: APIGeoService.countries.pluck(:name), allow_nil: false, allow_blank: false
end end
def for_export(path = :value)
case path
when :code
code
when :value
name
end
end
def to_s
name
end
def for_tag(path = :value)
case path
when :code
code
when :value
name
end
end
def selected def selected
code || value code || value
end end

View file

@ -1,41 +1,9 @@
class Champs::PhoneChamp < Champs::TextChamp class Champs::PhoneChamp < Champs::TextChamp
# We want to allow:
# * international (e164) phone numbers
# * “french format” (ten digits with a leading 0)
# * DROM numbers
#
# However, we need to special-case some ten-digit numbers,
# because the ARCEP assigns some blocks of "O6 XX XX XX XX" numbers to DROM operators.
# Guadeloupe | GP | +590 | 0690XXXXXX, 0691XXXXXX
# Guyane | GF | +594 | 0694XXXXXX
# Martinique | MQ | +596 | 0696XXXXXX, 0697XXXXXX
# Réunion | RE | +262 | 0692XXXXXX, 0693XXXXXX
# Mayotte | YT | +262 | 0692XXXXXX, 0693XXXXXX
# Nouvelle-Calédonie | NC | +687 |
# Polynésie française | PF | +689 | 40XXXXXX, 45XXXXXX, 87XXXXXX, 88XXXXXX, 89XXXXXX
#
# Cf: Plan national de numérotation téléphonique,
# https://www.arcep.fr/uploads/tx_gsavis/05-1085.pdf “Numéros mobiles à 10 chiffres”, page 6
#
# See issue #6996.
DEFAULT_COUNTRY_CODES = [:FR, :GP, :GF, :MQ, :RE, :YT, :NC, :PF].freeze
validates :value, validates :value,
phone: { phone: {
possible: true, possible: true,
allow_blank: true, allow_blank: true,
message: I18n.t(:not_a_phone, scope: 'activerecord.errors.messages') message: I18n.t(:not_a_phone, scope: 'activerecord.errors.messages')
}, },
if: -> { !Phonelib.valid_for_countries?(value, DEFAULT_COUNTRY_CODES) && validate_champ_value_or_prefill? } if: -> { !Phonelib.valid_for_countries?(value, TypesDeChamp::PhoneTypeDeChamp::DEFAULT_COUNTRY_CODES) && validate_champ_value_or_prefill? }
def to_s
return '' if value.blank?
if Phonelib.valid_for_countries?(value, DEFAULT_COUNTRY_CODES)
Phonelib.parse_for_countries(value, DEFAULT_COUNTRY_CODES).full_national
else
# When he phone number is possible for the default countries, but not strictly valid,
# `full_national` could mess up the formatting. In this case just return the original.
value
end
end
end end

View file

@ -25,20 +25,4 @@ class Champs::PieceJustificativeChamp < Champ
def blank? def blank?
piece_justificative_file.blank? piece_justificative_file.blank?
end end
def for_export(path = :value)
piece_justificative_file.map { _1.filename.to_s }.join(', ')
end
def for_api
return nil unless piece_justificative_file.attached?
# API v1 don't support multiple PJ
attachment = piece_justificative_file.first
return nil if attachment.nil?
if attachment.virus_scanner.safe? || attachment.virus_scanner.pending?
attachment.url
end
end
end end

View file

@ -2,24 +2,6 @@ class Champs::RegionChamp < Champs::TextChamp
validate :value_in_region_names, if: -> { !value.nil? && validate_champ_value_or_prefill? } validate :value_in_region_names, if: -> { !value.nil? && validate_champ_value_or_prefill? }
validate :external_id_in_region_codes, if: -> { !external_id.nil? && validate_champ_value_or_prefill? } validate :external_id_in_region_codes, if: -> { !external_id.nil? && validate_champ_value_or_prefill? }
def for_export(path = :value)
case path
when :value
name
when :code
code
end
end
def for_tag(path = :value)
case path
when :value
name
when :code
code
end
end
def selected def selected
code code
end end

View file

@ -15,10 +15,6 @@ class Champs::RNAChamp < Champ
title.present? ? "#{value} (#{title})" : value title.present? ? "#{value} (#{title})" : value
end end
def for_export(path = :value)
identifier
end
def search_terms def search_terms
etablissement.present? ? etablissement.search_terms : [value] etablissement.present? ? etablissement.search_terms : [value]
end end

View file

@ -25,36 +25,6 @@ class Champs::RNFChamp < Champ
rnf_id.blank? rnf_id.blank?
end end
def for_export(path = :value)
case path
when :value
rnf_id
when :departement
departement_code_and_name
when :code_insee
commune&.fetch(:code)
when :address
full_address
when :nom
title
end
end
def for_tag(path = :value)
case path
when :value
rnf_id
when :departement
departement_code_and_name || ''
when :code_insee
commune&.fetch(:code) || ''
when :address
full_address || ''
when :nom
title || ''
end
end
def code_departement def code_departement
address.present? && address['departmentCode'] address.present? && address['departmentCode']
end end

View file

@ -1,8 +1,4 @@
class Champs::TextareaChamp < Champs::TextChamp class Champs::TextareaChamp < Champs::TextChamp
def for_export(path = :value)
value.present? ? ActionView::Base.full_sanitizer.sanitize(value) : nil
end
def remaining_characters def remaining_characters
character_limit_base - character_count if character_count >= character_limit_threshold_75 character_limit_base - character_count if character_count >= character_limit_threshold_75
end end

View file

@ -19,12 +19,4 @@ class Champs::TitreIdentiteChamp < Champ
def blank? def blank?
piece_justificative_file.blank? piece_justificative_file.blank?
end end
def for_export(path = :value)
piece_justificative_file.attached? ? "présent" : "absent"
end
def for_api
nil
end
end end