Refacto comparaison typed id

This commit is contained in:
Damien Le Thiec 2023-02-23 11:00:09 +01:00
parent 61d2edc42e
commit 17f8475b07
3 changed files with 10 additions and 10 deletions

View file

@ -19,6 +19,14 @@ class ApplicationRecord < ActiveRecord::Base
GraphQL::Schema::UniqueWithinType.decode(id)[1]
end
def self.stable_id_from_typed_id(prefixed_typed_id)
return nil unless prefixed_typed_id.starts_with?("champ_")
self.id_from_typed_id(prefixed_typed_id.gsub("champ_", "")).to_i
rescue
nil
end
def to_typed_id
GraphQL::Schema::UniqueWithinType.encode(self.class.name, id)
end

View file

@ -14,7 +14,7 @@ class PrefillParams
def build_prefill_values
value_by_stable_id = params
.map { |prefixed_typed_id, value| [stable_id_from_typed_id(prefixed_typed_id), value] }
.map { |prefixed_typed_id, value| [Champ.stable_id_from_typed_id(prefixed_typed_id), value] }
.filter { |stable_id, value| stable_id.present? && value.present? }
.to_h
@ -24,14 +24,6 @@ class PrefillParams
.map { |champ, value| PrefillValue.new(champ:, value:, dossier:) }
end
def stable_id_from_typed_id(prefixed_typed_id)
return nil unless prefixed_typed_id.starts_with?("champ_")
Champ.id_from_typed_id(prefixed_typed_id.gsub("champ_", "")).to_i
rescue
nil
end
class PrefillValue
NEED_VALIDATION_TYPES_DE_CHAMPS = [
TypeDeChamp.type_champs.fetch(:decimal_number),

View file

@ -59,7 +59,7 @@ class TypesDeChamp::PrefillRepetitionTypeDeChamp < TypesDeChamp::PrefillTypeDeCh
repetition.map do |key, value|
next unless key.is_a?(String) && key.starts_with?("champ_")
subchamp = row.find { |champ| champ.type_de_champ_to_typed_id_for_query == key.split("_").last }
subchamp = row.find { |champ| champ.stable_id == Champ.stable_id_from_typed_id(key) }
next unless subchamp
TypesDeChamp::PrefillTypeDeChamp.build(subchamp.type_de_champ, revision).to_assignable_attributes(subchamp, value)