refactor(column): no more java 🎉 get_value -> value

This commit is contained in:
Paul Chavard 2024-10-31 21:36:13 +01:00
parent af33e6a0e9
commit 7fddec484d
No known key found for this signature in database
6 changed files with 26 additions and 40 deletions

View file

@ -52,15 +52,12 @@ class Column
procedure.find_column(h_id: h_id) procedure.find_column(h_id: h_id)
end end
def get_value(champ) def value(champ)
return if champ.nil? return if champ.nil?
value = get_raw_value(champ) value = typed_value(champ)
if should_cast? if default_column?
from_type = champ.last_write_column_type cast_value(value, from_type: champ.last_write_column_type, to_type: type)
to_type = type
parsed_value = parse_value(value, from_type)
cast_value(parsed_value, from_type:, to_type:)
else else
value value
end end
@ -68,15 +65,15 @@ class Column
private private
def get_raw_value(champ) def typed_value(champ)
champ.public_send(value_column) value = string_value(champ)
parse_value(value, type: champ.last_write_column_type)
end end
def should_cast? def string_value(champ) = champ.public_send(value_column)
true def default_column? = value_column.in?([:value, :external_id])
end
def parse_value(value, type) def parse_value(value, type:)
return if value.blank? return if value.blank?
case type case type

View file

@ -1,7 +1,7 @@
# frozen_string_literal: true # frozen_string_literal: true
class Columns::DossierColumn < Column class Columns::DossierColumn < Column
def get_value(dossier) def value(dossier)
case table case table
when 'self' when 'self'
dossier.public_send(column) dossier.public_send(column)

View file

@ -25,14 +25,10 @@ class Columns::JSONPathColumn < Column
private private
def get_raw_value(champ) def typed_value(champ)
champ.value_json&.dig(*value_column) champ.value_json&.dig(*value_column)
end end
def should_cast?
false
end
def stable_id def stable_id
@column @column
end end

View file

@ -2,12 +2,9 @@
class Columns::LinkedDropDownColumn < Column class Columns::LinkedDropDownColumn < Column
def column def column
if value_column == :value return super if default_column?
super
else
"#{@column}->#{value_column}" # override column otherwise json path facets will have same id as other "#{@column}->#{value_column}" # override column otherwise json path facets will have same id as other
end end
end
def filtered_ids(dossiers, values) def filtered_ids(dossiers, values)
dossiers.with_type_de_champ(@column) dossiers.with_type_de_champ(@column)
@ -17,11 +14,11 @@ class Columns::LinkedDropDownColumn < Column
private private
def get_raw_value(champ) def typed_value(champ)
return nil if default_column?
primary_value, secondary_value = unpack_values(champ.value) primary_value, secondary_value = unpack_values(champ.value)
case value_column case value_column
when :value
nil
when :primary when :primary
primary_value primary_value
when :secondary when :secondary
@ -29,10 +26,6 @@ class Columns::LinkedDropDownColumn < Column
end end
end end
def should_cast?
false
end
def unpack_values(value) def unpack_values(value)
JSON.parse(value) JSON.parse(value)
rescue JSON::ParserError rescue JSON::ParserError

View file

@ -3,7 +3,7 @@
class Columns::TitreIdentiteColumn < Column class Columns::TitreIdentiteColumn < Column
private private
def get_raw_value(champ) def typed_value(champ)
champ.piece_justificative_file.attached?.to_s champ.piece_justificative_file.attached?
end end
end end

View file

@ -1,7 +1,7 @@
# frozen_string_literal: true # frozen_string_literal: true
describe Column do describe Column do
describe 'get_value' do describe 'value' do
let(:groupe_instructeur) { create(:groupe_instructeur, instructeurs: [create(:instructeur)]) } let(:groupe_instructeur) { create(:groupe_instructeur, instructeurs: [create(:instructeur)]) }
context 'when dossier columns' do context 'when dossier columns' do
@ -11,9 +11,9 @@ describe Column do
let(:dossier) { create(:dossier, individual:, mandataire_first_name: "Martin", mandataire_last_name: "Christophe", for_tiers: true) } let(:dossier) { create(:dossier, individual:, mandataire_first_name: "Martin", mandataire_last_name: "Christophe", for_tiers: true) }
it 'retrieve individual information' do it 'retrieve individual information' do
expect(procedure.find_column(label: "Prénom").get_value(dossier)).to eq("Paul") expect(procedure.find_column(label: "Prénom").value(dossier)).to eq("Paul")
expect(procedure.find_column(label: "Nom").get_value(dossier)).to eq("Sim") expect(procedure.find_column(label: "Nom").value(dossier)).to eq("Sim")
expect(procedure.find_column(label: "Civilité").get_value(dossier)).to eq("M.") expect(procedure.find_column(label: "Civilité").value(dossier)).to eq("M.")
end end
end end
@ -22,7 +22,7 @@ describe Column do
let(:dossier) { create(:dossier, :en_instruction, :with_entreprise, procedure:) } let(:dossier) { create(:dossier, :en_instruction, :with_entreprise, procedure:) }
it 'retrieve entreprise information' do it 'retrieve entreprise information' do
expect(procedure.find_column(label: "Libellé NAF").get_value(dossier)).to eq('Transports par conduites') expect(procedure.find_column(label: "Libellé NAF").value(dossier)).to eq('Transports par conduites')
end end
end end
@ -31,7 +31,7 @@ describe Column do
let(:dossier) { create(:dossier, :en_instruction, procedure:) } let(:dossier) { create(:dossier, :en_instruction, procedure:) }
it 'does not fail' do it 'does not fail' do
expect(procedure.find_column(label: "Date décision SVA").get_value(dossier)).to eq(nil) expect(procedure.find_column(label: "Date décision SVA").value(dossier)).to eq(nil)
end end
end end
end end
@ -85,7 +85,7 @@ describe Column do
type_de_champ = types_de_champ.find { _1.type_champ == type } type_de_champ = types_de_champ.find { _1.type_champ == type }
champ = dossier.send(:filled_champ, type_de_champ, nil) champ = dossier.send(:filled_champ, type_de_champ, nil)
columns = type_de_champ.columns(procedure_id: procedure.id) columns = type_de_champ.columns(procedure_id: procedure.id)
expect(columns.map { _1.get_value(champ) }).to eq(values) expect(columns.map { _1.value(champ) }).to eq(values)
end end
def retrieve_champ(type) def retrieve_champ(type)