Skip to content

Commit

Permalink
rib: normalises the name and normalise even more the RIB and IBAN
Browse files Browse the repository at this point in the history
We got ASP errors because:

- the INTITDEST (i.e Rib#name) had `\t` in them – they were input with
tabulation as space, which is perfectly legitimate to me. Clean up the
value to adress that.

- the BIC length was over 11, which is because of space around
them (we took care of that) but also sometimes in the middle of the
value. Take care of that too now, and refactor a bit to reduce duplication.

NOTE: bonus points: we can use something like:

```ruby
Rib.find_each do |rib|
  rib.normalize_attribute(:iban)
  rib.normalize_attribute(:bic)
  rib.normalize_attribute(:name)

  rib.save! # this will not trigger an update query if there are no changes^
```

Closes #677.
  • Loading branch information
freesteph committed Apr 4, 2024
1 parent 099802c commit 7cde136
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
8 changes: 4 additions & 4 deletions app/models/rib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ class Rib < ApplicationRecord
scope :reused, -> { Rib.where(iban: Rib.multiple_ibans) }
scope :multiple_ibans, -> { Rib.select(:iban).group(:iban).having("count(iban) > 1") }

normalizes :bic, with: ->(bic) { bic.strip }
normalizes :iban, with: ->(iban) { iban.strip }
normalizes :bic, :iban, with: ->(value) { value.gsub(/\s+/, "").upcase }
normalizes :name, with: ->(name) { name.squish }

before_validation do
self.iban = iban.strip.upcase unless iban.nil?
self.bic = bic.strip.upcase unless bic.nil?
self.iban = Rib.normalize_value_for(:iban, iban) unless iban.nil?
self.bic = Rib.normalize_value_for(:bic, bic) unless bic.nil?
end

# gem 'bank-account' provides the :iban and :rib validation but we
Expand Down
17 changes: 15 additions & 2 deletions spec/models/rib_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,27 @@
end

describe "normalization" do
subject(:spaced) { create(:rib, bic: " #{rib.bic.downcase} ", iban: " #{rib.iban.downcase} ") }
subject(:spaced) do
create(
:rib,
bic: " #{"#{rib.bic.downcase[0..2]} #{rib.bic[3..]}"} ",
iban: " #{"#{rib.iban.downcase[0..2]} #{rib.iban[3..]}"}\t "
)
end

%i[bic iban].each do |attr|
it "strips the #{attr}" do
expect(spaced[attr]).to eq rib[attr].upcase
end
end

describe "name" do
it "squishes the name attribute" do
rib = create(:rib, name: " Marie\t\tCurie Mrs ")

expect(rib.name).to eq "Marie Curie Mrs"
end
end
end

describe "reused?" do
Expand All @@ -57,7 +71,6 @@
it "is included in the unique scope" do
expect(described_class.unique).to include(rib)
end

end

context "with multiple RIBS with the same IBAN" do
Expand Down

0 comments on commit 7cde136

Please sign in to comment.