Skip to content

Commit

Permalink
asp: truncate the persphysique names attributes
Browse files Browse the repository at this point in the history
Piggyback the `limit` option for ActiveModel's attribute method (which
might be a thing one day[1]) to chop the value of the string directly
on the XML-generating class.

This keeps the mapping classes in charge of translating our data into
the ASP stuff, semantically, but keeps the actual XML-maker in control
of the formatted length, which makes more sense to me: all the XML
tags have an associated length in the ASP documentation, so keeping it
as close as possible to the XML generation code is nicer.

It's also coherent with the custom :asp_date type which we use in a
couple places (ie: prestadoss.rb) and takes care of formatting a date
object in the format dictated by the ASP.

Closes #678.

[1]: rails/rails#51494
  • Loading branch information
freesteph committed Apr 8, 2024
1 parent a6ca080 commit 51452ce
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
14 changes: 14 additions & 0 deletions config/initializers/types.rb
Expand Up @@ -8,4 +8,18 @@ def cast(value)
end
end

module ActiveModel
module Type
class String
def cast(value)
if @limit
super(value.to_s.first(@limit))
else
super
end
end
end
end
end

ActiveModel::Type.register :asp_date, AspDateType
13 changes: 8 additions & 5 deletions lib/asp/entities/pers_physique.rb
Expand Up @@ -3,11 +3,14 @@
module ASP
module Entities
class PersPhysique < Entity
FIRST_NAME_MAX_LENGTH = 20
LAST_NAME_MAX_LENGTH = 50

attribute :titre, :string
attribute :nomusage, :string
attribute :nomnaissance, :string
attribute :prenom, :string
attribute :datenaissance, :date
attribute :nomusage, :string, limit: LAST_NAME_MAX_LENGTH
attribute :nomnaissance, :string, limit: LAST_NAME_MAX_LENGTH
attribute :prenom, :string, limit: FIRST_NAME_MAX_LENGTH
attribute :datenaissance, :asp_date
attribute :codeinseepaysnai, :string
attribute :codeinseecommune, :string

Expand All @@ -27,7 +30,7 @@ def fragment(xml)
xml.prenom(prenom)
xml.nomusage(nomusage)
xml.nomnaissance(nomnaissance)
xml.datenaissance(I18n.l(datenaissance, format: :asp))
xml.datenaissance(datenaissance)
xml.codeinseepaysnai(codeinseepaysnai)
xml.codeinseecommune(codeinseecommune) if born_in_france?
end
Expand Down
4 changes: 4 additions & 0 deletions spec/lib/asp/entities/pers_physique_spec.rb
Expand Up @@ -15,6 +15,10 @@
describe "validation" do
subject(:model) { described_class.from_payment_request(payment_request) }

it_behaves_like "a limited string attribute", attribute: :prenom, length: 20
it_behaves_like "a limited string attribute", attribute: :nomnaissance, length: 50
it_behaves_like "a limited string attribute", attribute: :nomusage, length: 50

context "when the student is born in France" do
let(:student) { create(:student, :with_extra_info, :born_in_france) }

Expand Down
11 changes: 11 additions & 0 deletions spec/support/shared/attribute_limiter.rb
@@ -0,0 +1,11 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.shared_examples "a limited string attribute" do |attribute:, length:|
it "chops the '#{attribute}' attribute to #{length} characters" do
model.send "#{attribute}=", Faker::Alphanumeric.alpha(number: length + 1)

expect(model.send(attribute).to_s).to have(length).characters
end
end

0 comments on commit 51452ce

Please sign in to comment.