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
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
if full_address?
address.fetch('department_code')

View file

@ -17,28 +17,8 @@ class Champs::BooleanChamp < Champ
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
def processed_value
true? ? 'Oui' : 'Non'
end
def set_value_to_nil
self.value = nil
end

View file

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

View file

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

View file

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

View file

@ -2,28 +2,6 @@ class Champs::CommuneChamp < Champs::TextChamp
store_accessor :value_json, :code_departement, :code_postal, :code_region
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
APIGeoService.departement_name(code_departement)
end
@ -60,10 +38,6 @@ class Champs::CommuneChamp < Champs::TextChamp
APIGeoService.safely_normalize_city_name(code_departement, code, safe_to_s)
end
def to_s
code_postal? ? "#{name} (#{code_postal})" : name
end
def code
external_id
end

View file

@ -6,16 +6,6 @@ class Champs::DateChamp < Champ
# Text search is pretty useless for dates so were not including these champs
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
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
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
def convert_to_iso8601

View file

@ -17,14 +17,6 @@ class Champs::DecimalNumberChamp < Champ
}
}, if: :validate_champ_value_or_prefill?
def for_export(path = :value)
processed_value
end
def for_api
processed_value
end
private
def format_value
@ -32,10 +24,4 @@ class Champs::DecimalNumberChamp < Champ
self.value = value.tr(",", ".")
end
def processed_value
return unless valid_champ_value?
value&.to_f
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? }
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
code
end
@ -71,10 +41,6 @@ class Champs::DepartementChamp < Champs::TextChamp
private
def formatted_value
blank? ? "" : "#{code} #{name}"
end
def value_in_departement_names
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 :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
APIGeoService.departement_name(code_departement)
end

View file

@ -8,20 +8,4 @@ class Champs::IntegerNumberChamp < Champ
object.errors.generate_message(:value, :not_an_integer)
}
}, 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

View file

@ -37,36 +37,6 @@ class Champs::LinkedDropDownListChamp < Champ
:primary_value
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?
primary_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)
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?
enabled_non_empty_options.size <= THRESHOLD_NB_OPTIONS_AS_CHECKBOX
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
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
code || value
end

View file

@ -1,41 +1,9 @@
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,
phone: {
possible: true,
allow_blank: true,
message: I18n.t(:not_a_phone, scope: 'activerecord.errors.messages')
},
if: -> { !Phonelib.valid_for_countries?(value, 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
if: -> { !Phonelib.valid_for_countries?(value, TypesDeChamp::PhoneTypeDeChamp::DEFAULT_COUNTRY_CODES) && validate_champ_value_or_prefill? }
end

View file

@ -25,20 +25,4 @@ class Champs::PieceJustificativeChamp < Champ
def blank?
piece_justificative_file.blank?
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

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 :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
code
end

View file

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

View file

@ -25,36 +25,6 @@ class Champs::RNFChamp < Champ
rnf_id.blank?
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
address.present? && address['departmentCode']
end

View file

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

View file

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