Skip to content

Commit

Permalink
Suppression de l'attribut 'asp_file_reference' de 'Student'
Browse files Browse the repository at this point in the history
  • Loading branch information
tnicolas1 committed Nov 26, 2024
1 parent db3b759 commit e4562a1
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 56 deletions.
20 changes: 0 additions & 20 deletions app/models/student.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ class Student < ApplicationRecord # rubocop:disable Metrics/ClassLength
:first_name,
:last_name,
:birthdate,
:asp_file_reference,
presence: true

validates :asp_file_reference, uniqueness: true
validates :address_city_insee_code, length: { maximum: 5 }, allow_blank: true

enum :biological_sex, { sex_unknown: 0, male: 1, female: 2 }, validate: { allow_nil: true }, default: :sex_unknown
Expand Down Expand Up @@ -85,8 +83,6 @@ def self.asp_ready
.with_valid_address
end

before_validation :check_asp_file_reference

def lives_in_france?
InseeCodes.in_france?(address_country_code)
rescue InseeCountryCodeMapper::UnusableCountryCode
Expand Down Expand Up @@ -159,20 +155,4 @@ def retry_pfmps_payment_requests!(reasons)
def unsyncable?
ine_not_found || current_schooling&.removed? || establishment.blank?
end

private

def check_asp_file_reference
return if asp_file_reference.present?

loop do
self.asp_file_reference = generate_asp_file_reference

break unless Student.exists?(asp_file_reference: asp_file_reference)
end
end

def generate_asp_file_reference
SecureRandom.alphanumeric(10).upcase
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ def migrate_single_attributive_decision!
.merge(Schooling.with_attributive_decisions)
.group(:id)
.having("count(schoolings.id) = 1")
.pluck(:id, :asp_file_reference)
.to_h

Schooling
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class RemoveASPFileReferenceFromStudents < ActiveRecord::Migration[7.2]
def change
remove_column :students, :asp_file_reference, :string
end
end
4 changes: 1 addition & 3 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion spec/models/schooling_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@

before do
schooling.classe.update!(label: "1ERE APEX TEST")
schooling.student.update!(first_name: "Jeanne", last_name: "DUPONT", asp_file_reference: "ref123")
schooling.student.update!(first_name: "Jeanne", last_name: "DUPONT")
end

it "creates a sane filename" do
Expand Down
31 changes: 0 additions & 31 deletions spec/models/student_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
it { is_expected.to validate_presence_of(:last_name) }
it { is_expected.to validate_presence_of(:birthdate) }
it { is_expected.to validate_presence_of(:ine) }
it { is_expected.to validate_uniqueness_of(:asp_file_reference) }

describe "#rib" do
subject(:student) { classe.students.first }
Expand Down Expand Up @@ -52,36 +51,6 @@
end
end

describe "asp_file_reference" do
subject(:student) { build(:student, asp_file_reference: nil) }

it "is generated before_save" do
expect { student.save! }.to change(student, :asp_file_reference)
end

# rubocop:disable RSpec/SubjectStub
context "when there is a collision" do
let(:used_values) { %w[A B C] }

before do
used_values.each { |value| create(:student, asp_file_reference: value) }

allow(student)
.to receive(:generate_asp_file_reference)
.and_return(*used_values, "D")
end

it "tries until it is unique" do
student.save!

expect(student)
.to have_received(:generate_asp_file_reference)
.exactly(4).times
end
end
# rubocop:enable RSpec/SubjectStub
end

describe "current_schooling" do
subject(:current_schooling) { student.reload.current_schooling }

Expand Down

0 comments on commit e4562a1

Please sign in to comment.