Merge pull request #10554 from mfo/US/fix-export-decimal

ETQ admin/instructeur, je souhaite récupérer la valeur d'un type de champ decimal même si la valeur est précédé et ou suivi d'espace blanc
This commit is contained in:
mfo 2024-06-26 02:06:39 +00:00 committed by GitHub
commit bc6e2a40ba
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 79 additions and 0 deletions

View file

@ -0,0 +1,20 @@
# frozen_string_literal: true
module Maintenance
class FixDecimalNumberWithSpacesTask < MaintenanceTasks::Task
ANY_SPACES = /[[:space:]]/
def collection
Champs::DecimalNumberChamp.where.not(value: nil)
end
def process(element)
if element.value.present? && ANY_SPACES.match?(element.value)
element.update_column(:value, element.value.gsub(ANY_SPACES, ''))
end
end
def count
# not really interested in counting because it raises PG Statement timeout
end
end
end

View file

@ -51,4 +51,21 @@ describe Champs::DecimalNumberChamp do
it { is_expected.to be_truthy } it { is_expected.to be_truthy }
end end
end end
describe 'for_export' do
let(:champ) { create(:champ_decimal_number, value:) }
subject { champ.for_export }
context 'with nil' do
let(:value) { 0 }
it { is_expected.to eq(0.0) }
end
context 'with simple number' do
let(:value) { "120" }
it { is_expected.to eq(120) }
end
context 'with number having spaces' do
let(:value) { " 120 " }
it { is_expected.to be_nil }
end
end
end end

View file

@ -0,0 +1,42 @@
# frozen_string_literal: true
require "rails_helper"
module Maintenance
RSpec.describe FixDecimalNumberWithSpacesTask do
describe "#process" do
subject(:process) { described_class.process(element) }
let(:champ) { create(:champ_decimal_number, value:) }
let(:element) { champ }
context 'with nil' do
let(:value) { 0 }
it { expect { process }.not_to change { champ.reload.valid_value } }
end
context 'with simple number' do
let(:value) { "120" }
it { expect { process }.not_to change { champ.reload.valid_value } }
end
context 'with number having leading spaces' do
let(:value) { " 120" }
it { expect { process }.to change { champ.reload.valid_value }.from(nil).to("120") }
end
context 'with number having trailing spaces' do
let(:value) { "120 " }
it { expect { process }.to change { champ.reload.valid_value }.from(nil).to("120") }
end
context 'with number having leading and trailing spaces' do
let(:value) { " 120 " }
it { expect { process }.to change { champ.reload.valid_value }.from(nil).to("120") }
end
context 'with number having in between spaces' do
let(:value) { "1 2 0" }
it { expect { process }.to change { champ.reload.valid_value }.from(nil).to("120") }
end
context 'with number having in between tab' do
let(:value) { "\t120\t" }
it { expect { process }.to change { champ.reload.valid_value }.from(nil).to("120") }
end
end
end
end