From 08490bfb82fb4558a4e8d4798f765a55faa0cad2 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Tue, 22 Oct 2024 18:15:28 +0200 Subject: [PATCH] =?UTF-8?q?ETQ=20Instructeur,=20je=20veux=20pouvoir=20fair?= =?UTF-8?q?e=20la=20difference=20entre=20un=20champ=20Oui/Non=20vide=20et?= =?UTF-8?q?=20=E2=80=9CNon=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/graphql/api/v2/schema.rb | 1 + app/graphql/api/v2/stored_query.rb | 3 +++ app/graphql/schema.graphql | 25 +++++++++++++++++++ app/graphql/types/champ_type.rb | 8 +++++- app/graphql/types/champs/yes_no_champ_type.rb | 17 +++++++++++++ .../types_de_champ/yes_no_type_de_champ.rb | 13 ++-------- spec/models/champ_spec.rb | 4 +-- spec/models/champs/checkbox_champ_spec.rb | 2 +- spec/models/champs/yes_no_champ_spec.rb | 2 +- .../shared_examples_for_boolean_champs.rb | 4 +-- 10 files changed, 61 insertions(+), 18 deletions(-) create mode 100644 app/graphql/types/champs/yes_no_champ_type.rb diff --git a/app/graphql/api/v2/schema.rb b/app/graphql/api/v2/schema.rb index 17ca8d19b..fdd16a39b 100644 --- a/app/graphql/api/v2/schema.rb +++ b/app/graphql/api/v2/schema.rb @@ -78,6 +78,7 @@ class API::V2::Schema < GraphQL::Schema Types::Champs::TextChampType, Types::Champs::TitreIdentiteChampType, Types::Champs::EngagementJuridiqueChampType, + Types::Champs::YesNoChampType, Types::GeoAreas::ParcelleCadastraleType, Types::GeoAreas::SelectionUtilisateurType, Types::PersonneMoraleType, diff --git a/app/graphql/api/v2/stored_query.rb b/app/graphql/api/v2/stored_query.rb index 0997ad69a..93682590b 100644 --- a/app/graphql/api/v2/stored_query.rb +++ b/app/graphql/api/v2/stored_query.rb @@ -509,6 +509,9 @@ class API::V2::StoredQuery ... on CheckboxChamp { checked: value } + ... on YesNoChamp { + selected: value + } ... on DecimalNumberChamp { decimalNumber: value } diff --git a/app/graphql/schema.graphql b/app/graphql/schema.graphql index 2b54aedb8..c89a4dcc8 100644 --- a/app/graphql/schema.graphql +++ b/app/graphql/schema.graphql @@ -4605,6 +4605,31 @@ type WarningMessage { message: String! } +type YesNoChamp implements Champ { + """ + L'identifiant du champDescriptor de ce champ + """ + champDescriptorId: String! + id: ID! + + """ + Libellé du champ. + """ + label: String! + prefilled: Boolean! + + """ + La valeur du champ sous forme texte. + """ + stringValue: String + + """ + Date de dernière modification du champ. + """ + updatedAt: ISO8601DateTime! + value: Boolean +} + type YesNoChampDescriptor implements ChampDescriptor { """ Description des champs d’un bloc répétable. diff --git a/app/graphql/types/champ_type.rb b/app/graphql/types/champ_type.rb index 183955fec..b13f6e250 100644 --- a/app/graphql/types/champ_type.rb +++ b/app/graphql/types/champ_type.rb @@ -24,8 +24,14 @@ module Types else Types::Champs::TextChampType end - when ::Champs::YesNoChamp, ::Champs::CheckboxChamp + when ::Champs::CheckboxChamp Types::Champs::CheckboxChampType + when ::Champs::YesNoChamp + if context.has_fragment?(:YesNoChamp) + Types::Champs::YesNoChampType + else + Types::Champs::CheckboxChampType + end when ::Champs::DateChamp Types::Champs::DateChampType when ::Champs::DatetimeChamp diff --git a/app/graphql/types/champs/yes_no_champ_type.rb b/app/graphql/types/champs/yes_no_champ_type.rb new file mode 100644 index 000000000..29c942098 --- /dev/null +++ b/app/graphql/types/champs/yes_no_champ_type.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +module Types::Champs + class YesNoChampType < Types::BaseObject + implements Types::ChampType + + field :value, Boolean, null: true + + def value + if object.blank? + nil + else + object.true? + end + end + end +end diff --git a/app/models/types_de_champ/yes_no_type_de_champ.rb b/app/models/types_de_champ/yes_no_type_de_champ.rb index 33148b161..947e31d79 100644 --- a/app/models/types_de_champ/yes_no_type_de_champ.rb +++ b/app/models/types_de_champ/yes_no_type_de_champ.rb @@ -33,20 +33,11 @@ class TypesDeChamp::YesNoTypeDeChamp < TypesDeChamp::CheckboxTypeDeChamp end def champ_default_value - 'Non' + '' end def champ_default_export_value(path = :value) - 'Non' - end - - def champ_default_api_value(version = 2) - case version - when 2 - 'false' - else - nil - end + '' end private diff --git a/spec/models/champ_spec.rb b/spec/models/champ_spec.rb index 1afb2ce2f..3c2368f95 100644 --- a/spec/models/champ_spec.rb +++ b/spec/models/champ_spec.rb @@ -212,7 +212,7 @@ describe Champ do context 'if nil' do let(:value) { nil } - it { expect(value_for_export).to eq('Non') } + it { expect(value_for_export).to eq('') } end end @@ -236,7 +236,7 @@ describe Champ do allow(champ_text).to receive(:type_de_champ).and_return(type_de_champ_text) end it { expect(type_de_champ_text.champ_value_for_export(champ_yes_no)).to eq(nil) } - it { expect(type_de_champ_yes_no.champ_value_for_export(champ_text)).to eq('Non') } + it { expect(type_de_champ_yes_no.champ_value_for_export(champ_text)).to eq('') } end end diff --git a/spec/models/champs/checkbox_champ_spec.rb b/spec/models/champs/checkbox_champ_spec.rb index bbe6edca0..6c93e3176 100644 --- a/spec/models/champs/checkbox_champ_spec.rb +++ b/spec/models/champs/checkbox_champ_spec.rb @@ -3,7 +3,7 @@ describe Champs::CheckboxChamp do let(:boolean_champ) { described_class.new(value: value) } before { allow(boolean_champ).to receive(:type_de_champ).and_return(build(:type_de_champ_checkbox)) } - it_behaves_like "a boolean champ" + it_behaves_like "a boolean champ", false # TODO remove when normalize_checkbox_values is over describe '#true?' do diff --git a/spec/models/champs/yes_no_champ_spec.rb b/spec/models/champs/yes_no_champ_spec.rb index 3a8ccb04e..de3983bd7 100644 --- a/spec/models/champs/yes_no_champ_spec.rb +++ b/spec/models/champs/yes_no_champ_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true describe Champs::YesNoChamp do - it_behaves_like "a boolean champ" do + it_behaves_like "a boolean champ", true do let(:boolean_champ) { described_class.new(value: value) } before { allow(boolean_champ).to receive(:type_de_champ).and_return(build(:type_de_champ_yes_no)) } end diff --git a/spec/support/shared_examples_for_boolean_champs.rb b/spec/support/shared_examples_for_boolean_champs.rb index bb7219717..8aba22115 100644 --- a/spec/support/shared_examples_for_boolean_champs.rb +++ b/spec/support/shared_examples_for_boolean_champs.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -RSpec.shared_examples "a boolean champ" do +RSpec.shared_examples "a boolean champ" do |nullable| describe 'before validation' do subject { boolean_champ.valid? } @@ -55,7 +55,7 @@ RSpec.shared_examples "a boolean champ" do context 'when the value is nil' do let(:value) { nil } - it { is_expected.to eq("Non") } + it { is_expected.to eq(nullable ? '' : 'Non') } end end end