From cb4e91c405def21a273eca115a6a957621de69d4 Mon Sep 17 00:00:00 2001 From: kara Diaby Date: Tue, 22 Sep 2020 16:04:57 +0200 Subject: [PATCH] Add iban type de champ --- Gemfile | 1 + Gemfile.lock | 2 ++ app/graphql/schema.graphql | 5 +++++ app/models/champs/iban_champ.rb | 19 +++++++++++++++++++ app/models/type_de_champ.rb | 3 ++- .../types_de_champ/iban_type_de_champ.rb | 2 ++ app/validators/iban_validator.rb | 11 +++++++++++ app/views/shared/champs/iban/_show.html.haml | 1 + .../shared/dossiers/_champ_row.html.haml | 2 ++ .../dossiers/editable_champs/_iban.html.haml | 4 ++++ config/locales/models/type_de_champ/fr.yml | 1 + spec/factories/champ.rb | 4 ++++ spec/factories/type_de_champ.rb | 3 +++ spec/models/champs/iban_champ_spec.rb | 12 ++++++++++++ .../services/procedure_export_service_spec.rb | 3 +++ 15 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 app/models/champs/iban_champ.rb create mode 100644 app/models/types_de_champ/iban_type_de_champ.rb create mode 100644 app/validators/iban_validator.rb create mode 100644 app/views/shared/champs/iban/_show.html.haml create mode 100644 app/views/shared/dossiers/editable_champs/_iban.html.haml create mode 100644 spec/models/champs/iban_champ_spec.rb diff --git a/Gemfile b/Gemfile index 388c88735..afed52c8e 100644 --- a/Gemfile +++ b/Gemfile @@ -42,6 +42,7 @@ gem 'groupdate' gem 'haml-rails' gem 'hashie' gem 'http_accept_language' +gem 'iban-tools' gem 'jquery-rails' # Use jquery as the JavaScript library gem 'jwt' gem 'kaminari', '1.2.1' # Pagination diff --git a/Gemfile.lock b/Gemfile.lock index 89a21c188..dd871a642 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -341,6 +341,7 @@ GEM httpclient (2.8.3) i18n (1.8.5) concurrent-ruby (~> 1.0) + iban-tools (1.1.0) ice_nine (0.11.2) ipaddress (0.8.3) jaro_winkler (1.5.4) @@ -813,6 +814,7 @@ DEPENDENCIES haml-rails hashie http_accept_language + iban-tools jquery-rails jwt kaminari (= 1.2.1) diff --git a/app/graphql/schema.graphql b/app/graphql/schema.graphql index e8cad479c..c84c2ee0c 100644 --- a/app/graphql/schema.graphql +++ b/app/graphql/schema.graphql @@ -1272,6 +1272,11 @@ enum TypeDeChamp { """ header_section + """ + Iban + """ + iban + """ Nombre entier """ diff --git a/app/models/champs/iban_champ.rb b/app/models/champs/iban_champ.rb new file mode 100644 index 000000000..dcb6cd5db --- /dev/null +++ b/app/models/champs/iban_champ.rb @@ -0,0 +1,19 @@ +# == Schema Information +# +# Table name: champs +# +# id :integer not null, primary key +# private :boolean default(FALSE), not null +# row :integer +# type :string +# value :string +# created_at :datetime +# updated_at :datetime +# dossier_id :integer +# etablissement_id :integer +# parent_id :bigint +# type_de_champ_id :integer +# +class Champs::IbanChamp < Champ + validates_with IbanValidator +end diff --git a/app/models/type_de_champ.rb b/app/models/type_de_champ.rb index 7a5835616..2f391cd11 100644 --- a/app/models/type_de_champ.rb +++ b/app/models/type_de_champ.rb @@ -46,7 +46,8 @@ class TypeDeChamp < ApplicationRecord siret: 'siret', carte: 'carte', repetition: 'repetition', - titre_identite: 'titre_identite' + titre_identite: 'titre_identite', + iban: 'iban' } belongs_to :revision, class_name: 'ProcedureRevision', optional: true diff --git a/app/models/types_de_champ/iban_type_de_champ.rb b/app/models/types_de_champ/iban_type_de_champ.rb new file mode 100644 index 000000000..204348185 --- /dev/null +++ b/app/models/types_de_champ/iban_type_de_champ.rb @@ -0,0 +1,2 @@ +class TypesDeChamp::IbanTypeDeChamp < TypesDeChamp::TypeDeChampBase +end diff --git a/app/validators/iban_validator.rb b/app/validators/iban_validator.rb new file mode 100644 index 000000000..d31c30a19 --- /dev/null +++ b/app/validators/iban_validator.rb @@ -0,0 +1,11 @@ +require 'iban-tools' + +class IbanValidator < ActiveModel::Validator + def validate(record) + if record.value.present? + unless IBANTools::IBAN.valid?(record.value) + record.errors.add :iban, record.errors.generate_message(:value, :invalid) + end + end + end +end diff --git a/app/views/shared/champs/iban/_show.html.haml b/app/views/shared/champs/iban/_show.html.haml new file mode 100644 index 000000000..6ee164a0a --- /dev/null +++ b/app/views/shared/champs/iban/_show.html.haml @@ -0,0 +1 @@ +%p= champ.blank? ? 'Iban non fourni' : champ diff --git a/app/views/shared/dossiers/_champ_row.html.haml b/app/views/shared/dossiers/_champ_row.html.haml index 6d6461468..261d6d373 100644 --- a/app/views/shared/dossiers/_champ_row.html.haml +++ b/app/views/shared/dossiers/_champ_row.html.haml @@ -30,6 +30,8 @@ = render partial: "shared/champs/piece_justificative/show", locals: { champ: c } - when TypeDeChamp.type_champs.fetch(:siret) = render partial: "shared/champs/siret/show", locals: { champ: c, profile: profile } + - when TypeDeChamp.type_champs.fetch(:iban) + = render partial: "shared/champs/iban/show", locals: { champ: c } - when TypeDeChamp.type_champs.fetch(:textarea) = render partial: "shared/champs/textarea/show", locals: { champ: c } - when TypeDeChamp.type_champs.fetch(:date) diff --git a/app/views/shared/dossiers/editable_champs/_iban.html.haml b/app/views/shared/dossiers/editable_champs/_iban.html.haml new file mode 100644 index 000000000..149b436db --- /dev/null +++ b/app/views/shared/dossiers/editable_champs/_iban.html.haml @@ -0,0 +1,4 @@ += form.text_field :value, + placeholder: "27 caractères au format FR7630006000011234567890189", + required: champ.mandatory?, + aria: { describedby: describedby_id(champ) } diff --git a/config/locales/models/type_de_champ/fr.yml b/config/locales/models/type_de_champ/fr.yml index bdc1513ac..e3d5395da 100644 --- a/config/locales/models/type_de_champ/fr.yml +++ b/config/locales/models/type_de_champ/fr.yml @@ -34,3 +34,4 @@ fr: carte: 'Carte' repetition: 'Bloc répétable' titre_identite: 'Titre identité' + iban: 'Iban' diff --git a/spec/factories/champ.rb b/spec/factories/champ.rb index 000924b78..dd7ca6e27 100644 --- a/spec/factories/champ.rb +++ b/spec/factories/champ.rb @@ -155,6 +155,10 @@ FactoryBot.define do type_de_champ { association :type_de_champ_carte, procedure: dossier.procedure } end + factory :champ_iban, class: 'Champs::IbanChamp' do + type_de_champ { association :type_de_champ_iban, procedure: dossier.procedure } + end + factory :champ_siret, class: 'Champs::SiretChamp' do association :type_de_champ, factory: [:type_de_champ_siret] association :etablissement, factory: [:etablissement] diff --git a/spec/factories/type_de_champ.rb b/spec/factories/type_de_champ.rb index eb43d56ff..0156e19d9 100644 --- a/spec/factories/type_de_champ.rb +++ b/spec/factories/type_de_champ.rb @@ -139,6 +139,9 @@ FactoryBot.define do factory :type_de_champ_siret do type_champ { TypeDeChamp.type_champs.fetch(:siret) } end + factory :type_de_champ_iban do + type_champ { TypeDeChamp.type_champs.fetch(:iban) } + end factory :type_de_champ_carte do type_champ { TypeDeChamp.type_champs.fetch(:carte) } end diff --git a/spec/models/champs/iban_champ_spec.rb b/spec/models/champs/iban_champ_spec.rb new file mode 100644 index 000000000..b23f78b89 --- /dev/null +++ b/spec/models/champs/iban_champ_spec.rb @@ -0,0 +1,12 @@ + +describe Champs::IbanChamp do + describe '#valid?' do + it do + expect(build(:champ_iban, value: nil)).to be_valid + expect(build(:champ_iban, value: "FR35 KDSQFDJQSMFDQMFDQ")).to_not be_valid + expect(build(:champ_iban, value: "FR7630006000011234567890189")).to be_valid + expect(build(:champ_iban, value: "FR76 3000 6000 0112 3456 7890 189")).to be_valid + expect(build(:champ_iban, value: "FR76 3000 6000 0112 3456 7890 189DSF")).to_not be_valid + end + end +end diff --git a/spec/services/procedure_export_service_spec.rb b/spec/services/procedure_export_service_spec.rb index 0f8cafdfe..34f140986 100644 --- a/spec/services/procedure_export_service_spec.rb +++ b/spec/services/procedure_export_service_spec.rb @@ -74,6 +74,7 @@ describe ProcedureExportService do "siret", "carte", "titre_identite", + "iban", "text" ] end @@ -158,6 +159,7 @@ describe ProcedureExportService do "siret", "carte", "titre_identite", + "iban", "text" ] end @@ -238,6 +240,7 @@ describe ProcedureExportService do "siret", "carte", "titre_identite", + "iban", "text" ] end