Merge pull request #7866 from tchak/fix-iban-validation

fix(iban): format iban after validation
This commit is contained in:
Paul Chavard 2022-10-11 11:35:42 +02:00 committed by GitHub
commit 205dece4d5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 53 additions and 2 deletions

View file

@ -20,5 +20,20 @@
# type_de_champ_id :integer
#
class Champs::IbanChamp < Champ
validates_with IbanValidator
validates_with IbanValidator, if: -> { validation_context != :brouillon }
after_validation :format_iban
def for_api
to_s.gsub(/\s+/, '')
end
def for_api_v2
for_api
end
private
def format_iban
self.value = value&.gsub(/\s+/, '')&.gsub(/(.{4})/, '\0 ')
end
end

View file

@ -0,0 +1,7 @@
class AddIndexOnTypeToChamps < ActiveRecord::Migration[6.1]
include Database::MigrationHelpers
disable_ddl_transaction!
def up
add_concurrent_index :champs, [:type]
end
end

View file

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2022_10_06_193737) do
ActiveRecord::Schema.define(version: 2022_10_07_113737) do
# These are extensions that must be enabled in order to support this database
enable_extension "pgcrypto"
@ -203,6 +203,7 @@ ActiveRecord::Schema.define(version: 2022_10_06_193737) do
t.index ["parent_id"], name: "index_champs_on_parent_id"
t.index ["private"], name: "index_champs_on_private"
t.index ["row"], name: "index_champs_on_row"
t.index ["type"], name: "index_champs_on_type"
t.index ["type_de_champ_id", "dossier_id", "row"], name: "index_champs_on_type_de_champ_id_and_dossier_id_and_row", unique: true
t.index ["type_de_champ_id"], name: "index_champs_on_type_de_champ_id"
end

View file

@ -0,0 +1,21 @@
namespace :after_party do
desc 'Deployment task: format_iban_champ_values'
task format_iban_champ_values: :environment do
puts "Running deploy task 'format_iban_champ_values'"
champs = Champs::IbanChamp.where.not(value: nil)
progress = ProgressReport.new(champs.count)
champs.find_each do |champ|
# format IBAN value
champ.validate
champ.update_column(:value, champ.value)
progress.inc
end
progress.finish
# Update task as completed. If you remove the line below, the task will
# run with every deploy (or every time you call after_party:run).
AfterParty::TaskRecord
.create version: AfterParty::TaskRecorder.new(__FILE__).timestamp
end
end

View file

@ -6,6 +6,13 @@ describe Champs::IbanChamp do
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
expect(build(:champ_iban, value: "FR76 3000 6000 0112 3456 7890 189")).to be_valid
end
it 'format value after validation' do
champ = build(:champ_iban, value: "FR76 3000 6000 0112 3456 7890 189")
champ.valid?
expect(champ.value).to eq("FR76 3000 6000 0112 3456 7890 189")
end
end
end