refactor(export): expose new interface on type de champ and use it in champs

This commit is contained in:
Paul Chavard 2024-04-23 10:03:46 +02:00
parent 371b8b0b46
commit 39364961ab
4 changed files with 108 additions and 17 deletions

View file

@ -109,24 +109,29 @@ class Champ < ApplicationRecord
[to_s]
end
def to_s
value.present? ? value.to_s : ''
end
def for_export(path = :value)
path == :value ? value.presence : nil
end
def for_api
def valid_value
return unless valid_champ_value?
value
end
def to_s
TypeDeChamp.champ_value(type_champ, self)
end
def for_api
TypeDeChamp.champ_value_for_api(type_champ, self, 1)
end
def for_api_v2
to_s
TypeDeChamp.champ_value_for_api(type_champ, self, 2)
end
def for_export(path = :value)
TypeDeChamp.champ_value_for_export(type_champ, self, path)
end
def for_tag(path = :value)
path == :value && value.present? ? value.to_s : ''
TypeDeChamp.champ_value_for_tag(type_champ, self, path)
end
def main_value_name

View file

@ -25,7 +25,7 @@ module DossierChampsConcern
types_de_champ.flat_map do |type_de_champ|
champ = champ_for_export(type_de_champ, row_id)
type_de_champ.libelles_for_export.map do |(libelle, path)|
[libelle, champ&.for_export(path)]
[libelle, TypeDeChamp.champ_value_for_export(type_de_champ.type_champ, champ, path)]
end
end
end

View file

@ -250,7 +250,7 @@ class TypeDeChamp < ApplicationRecord
def params_for_champ
{
private: private?,
type: "Champs::#{type_champ.classify}Champ",
type: self.class.type_champ_to_champ_class_name(type_champ),
stable_id:,
stream: 'main'
}
@ -463,10 +463,6 @@ class TypeDeChamp < ApplicationRecord
!private?
end
def self.type_champ_to_class_name(type_champ)
"TypesDeChamp::#{type_champ.classify}TypeDeChamp"
end
def filename_for_attachement(attachment_sym)
attachment = send(attachment_sym)
if attachment.attached?
@ -687,6 +683,56 @@ class TypeDeChamp < ApplicationRecord
end
end
class << self
def champ_value(type_champ, champ)
dynamic_type_class = type_champ_to_class_name(type_champ).constantize
# special case for linked drop down champ it's blank implementation is not what you think
if champ.blank? && (type_champ != TypeDeChamp.type_champs.fetch(:linked_drop_down_list) || champ.nil? || champ.value.blank?)
dynamic_type_class.champ_default_value
else
dynamic_type_class.champ_value(champ)
end
end
def champ_value_for_api(type_champ, champ, version = 2)
dynamic_type_class = type_champ_to_class_name(type_champ).constantize
# special case for linked drop down champ it's blank implementation is not what you think
if champ.blank? && (type_champ != TypeDeChamp.type_champs.fetch(:linked_drop_down_list) || champ.nil? || champ.value.blank?)
dynamic_type_class.champ_default_api_value(version)
else
dynamic_type_class.champ_value_for_api(champ, version)
end
end
def champ_value_for_export(type_champ, champ, path = :value)
dynamic_type_class = type_champ_to_class_name(type_champ).constantize
# special case for linked drop down champ it's blank implementation is not what you think
if champ.blank? && (type_champ != TypeDeChamp.type_champs.fetch(:linked_drop_down_list) || champ.nil? || champ.value.blank?)
dynamic_type_class.champ_default_export_value(path)
else
dynamic_type_class.champ_value_for_export(champ, path)
end
end
def champ_value_for_tag(type_champ, champ, path = :value)
dynamic_type_class = type_champ_to_class_name(type_champ).constantize
# special case for linked drop down champ it's blank implementation is not what you think
if champ.blank? && (type_champ != TypeDeChamp.type_champs.fetch(:linked_drop_down_list) || champ.nil? || champ.value.blank?)
''
else
dynamic_type_class.champ_value_for_tag(champ, path)
end
end
def type_champ_to_champ_class_name(type_champ)
"Champs::#{type_champ.classify}Champ"
end
def type_champ_to_class_name(type_champ)
"TypesDeChamp::#{type_champ.classify}TypeDeChamp"
end
end
private
DEFAULT_EMPTY = ['']

View file

@ -56,6 +56,46 @@ class TypesDeChamp::TypeDeChampBase
human_value
end
class << self
def champ_value(champ)
champ.value.present? ? champ.value.to_s : champ_default_value
end
def champ_value_for_api(champ, version = 2)
case version
when 2
champ_value(champ)
else
champ.valid_value.presence || champ_default_api_value(version)
end
end
def champ_value_for_export(champ, path = :value)
path == :value ? champ.valid_value.presence : champ_default_export_value(path)
end
def champ_value_for_tag(champ, path = :value)
path == :value ? champ_value(champ) : nil
end
def champ_default_value
''
end
def champ_default_export_value(path = :value)
nil
end
def champ_default_api_value(version = 2)
case version
when 2
''
else
nil
end
end
end
private
def paths