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.

Closes #678.

[1]: rails/rails#51494
  • Loading branch information
freesteph committed Apr 4, 2024
1 parent daef6af commit e091ee3
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
14 changes: 14 additions & 0 deletions config/initializers/types.rb
Original file line number Diff line number Diff line change
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
11 changes: 7 additions & 4 deletions lib/asp/entities/pers_physique.rb
Original file line number Diff line number Diff line change
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
attribute :codeinseepaysnai, :string
attribute :codeinseecommune, :string

Expand Down
4 changes: 4 additions & 0 deletions spec/lib/asp/entities/pers_physique_spec.rb
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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 e091ee3

Please sign in to comment.