diff --git a/app/models/column.rb b/app/models/column.rb index 198aada85..b9c223fe8 100644 --- a/app/models/column.rb +++ b/app/models/column.rb @@ -52,15 +52,12 @@ class Column procedure.find_column(h_id: h_id) end - def get_value(champ) + def value(champ) return if champ.nil? - value = get_raw_value(champ) - if should_cast? - from_type = champ.last_write_column_type - to_type = type - parsed_value = parse_value(value, from_type) - cast_value(parsed_value, from_type:, to_type:) + value = typed_value(champ) + if default_column? + cast_value(value, from_type: champ.last_write_column_type, to_type: type) else value end @@ -68,15 +65,15 @@ class Column private - def get_raw_value(champ) - champ.public_send(value_column) + def typed_value(champ) + value = string_value(champ) + parse_value(value, type: champ.last_write_column_type) end - def should_cast? - true - end + def string_value(champ) = champ.public_send(value_column) + def default_column? = value_column.in?([:value, :external_id]) - def parse_value(value, type) + def parse_value(value, type:) return if value.blank? case type diff --git a/app/models/columns/dossier_column.rb b/app/models/columns/dossier_column.rb index 138b6092b..72ec97405 100644 --- a/app/models/columns/dossier_column.rb +++ b/app/models/columns/dossier_column.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true class Columns::DossierColumn < Column - def get_value(dossier) + def value(dossier) case table when 'self' dossier.public_send(column) diff --git a/app/models/columns/json_path_column.rb b/app/models/columns/json_path_column.rb index 210e41e7a..f78585688 100644 --- a/app/models/columns/json_path_column.rb +++ b/app/models/columns/json_path_column.rb @@ -25,14 +25,10 @@ class Columns::JSONPathColumn < Column private - def get_raw_value(champ) + def typed_value(champ) champ.value_json&.dig(*value_column) end - def should_cast? - false - end - def stable_id @column end diff --git a/app/models/columns/linked_drop_down_column.rb b/app/models/columns/linked_drop_down_column.rb index 396dcb8cd..bab15cd68 100644 --- a/app/models/columns/linked_drop_down_column.rb +++ b/app/models/columns/linked_drop_down_column.rb @@ -2,11 +2,8 @@ class Columns::LinkedDropDownColumn < Column def column - if value_column == :value - super - else - "#{@column}->#{value_column}" # override column otherwise json path facets will have same id as other - end + return super if default_column? + "#{@column}->#{value_column}" # override column otherwise json path facets will have same id as other end def filtered_ids(dossiers, values) @@ -17,11 +14,11 @@ class Columns::LinkedDropDownColumn < Column private - def get_raw_value(champ) + def typed_value(champ) + return nil if default_column? + primary_value, secondary_value = unpack_values(champ.value) case value_column - when :value - nil when :primary primary_value when :secondary @@ -29,10 +26,6 @@ class Columns::LinkedDropDownColumn < Column end end - def should_cast? - false - end - def unpack_values(value) JSON.parse(value) rescue JSON::ParserError diff --git a/app/models/columns/titre_identite_column.rb b/app/models/columns/titre_identite_column.rb index 3141df1d8..9ce491e51 100644 --- a/app/models/columns/titre_identite_column.rb +++ b/app/models/columns/titre_identite_column.rb @@ -3,7 +3,7 @@ class Columns::TitreIdentiteColumn < Column private - def get_raw_value(champ) - champ.piece_justificative_file.attached?.to_s + def typed_value(champ) + champ.piece_justificative_file.attached? end end diff --git a/spec/models/column_spec.rb b/spec/models/column_spec.rb index f91798d7a..74634c718 100644 --- a/spec/models/column_spec.rb +++ b/spec/models/column_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true describe Column do - describe 'get_value' do + describe 'value' do let(:groupe_instructeur) { create(:groupe_instructeur, instructeurs: [create(:instructeur)]) } 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) } it 'retrieve individual information' do - expect(procedure.find_column(label: "Prénom").get_value(dossier)).to eq("Paul") - expect(procedure.find_column(label: "Nom").get_value(dossier)).to eq("Sim") - expect(procedure.find_column(label: "Civilité").get_value(dossier)).to eq("M.") + expect(procedure.find_column(label: "Prénom").value(dossier)).to eq("Paul") + expect(procedure.find_column(label: "Nom").value(dossier)).to eq("Sim") + expect(procedure.find_column(label: "Civilité").value(dossier)).to eq("M.") end end @@ -22,7 +22,7 @@ describe Column do let(:dossier) { create(:dossier, :en_instruction, :with_entreprise, procedure:) } 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 @@ -31,7 +31,7 @@ describe Column do let(:dossier) { create(:dossier, :en_instruction, procedure:) } 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 @@ -85,7 +85,7 @@ describe Column do type_de_champ = types_de_champ.find { _1.type_champ == type } champ = dossier.send(:filled_champ, type_de_champ, nil) 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 def retrieve_champ(type)