diff --git a/app/models/etablissement.rb b/app/models/etablissement.rb index 811a0c0a9..d57bc08b2 100644 --- a/app/models/etablissement.rb +++ b/app/models/etablissement.rb @@ -17,6 +17,8 @@ class Etablissement < ApplicationRecord fermé: "fermé" }, _prefix: true + after_commit -> { dossier&.debounce_update_search_terms } + def entreprise_raison_sociale read_attribute(:entreprise_raison_sociale).presence || raison_sociale_for_ei end diff --git a/app/models/individual.rb b/app/models/individual.rb index c3d92b9eb..6e52045b4 100644 --- a/app/models/individual.rb +++ b/app/models/individual.rb @@ -17,6 +17,8 @@ class Individual < ApplicationRecord validates :email, presence: true, if: -> { dossier.for_tiers? && self.email? }, on: :update + after_commit -> { dossier.debounce_update_search_terms }, if: -> { nom_previously_changed? || prenom_previously_changed? } + GENDER_MALE = "M." GENDER_FEMALE = 'Mme' diff --git a/spec/models/etablissement_spec.rb b/spec/models/etablissement_spec.rb index a7f38d6cd..7653c79cd 100644 --- a/spec/models/etablissement_spec.rb +++ b/spec/models/etablissement_spec.rb @@ -115,6 +115,16 @@ describe Etablissement do end end + describe 'update search terms' do + let(:etablissement) { create(:etablissement, dossier: build(:dossier)) } + + it "schedule update search terms" do + assert_enqueued_jobs(1, only: DossierUpdateSearchTermsJob) do + etablissement.update(entreprise_nom: "nom") + end + end + end + private def csv_to_array_of_hash(lines) diff --git a/spec/models/individual_spec.rb b/spec/models/individual_spec.rb index 64820f03d..a37d1e15b 100644 --- a/spec/models/individual_spec.rb +++ b/spec/models/individual_spec.rb @@ -7,41 +7,49 @@ describe Individual do describe "#save" do let(:individual) { build(:individual) } - subject { individual.save } + subject do + individual.save + individual + end context "with birthdate" do before do individual.birthdate = birthdate_from_user - subject end context "and the format is dd/mm/yyy " do let(:birthdate_from_user) { "12/11/1980" } - it { expect(individual.birthdate).to eq(Date.new(1980, 11, 12)) } + it { expect(subject.birthdate).to eq(Date.new(1980, 11, 12)) } end context "and the format is ISO" do let(:birthdate_from_user) { "1980-11-12" } - it { expect(individual.birthdate).to eq(Date.new(1980, 11, 12)) } + it { expect(subject.birthdate).to eq(Date.new(1980, 11, 12)) } end context "and the format is WTF" do let(:birthdate_from_user) { "1980 1 12" } - it { expect(individual.birthdate).to be_nil } + it { expect(subject.birthdate).to be_nil } end end - context 'when an individual has an invalid notification_method' do - let(:invalid_individual) { build(:individual, notification_method: 'invalid_method') } - - it 'raises an ArgumentError' do - expect { - invalid_individual.valid? - }.to raise_error(ArgumentError, "'invalid_method' is not a valid notification_method") + it "schedule update search terms" do + assert_enqueued_jobs(1, only: DossierUpdateSearchTermsJob) do + subject end end end + + context 'when an individual has an invalid notification_method' do + let(:invalid_individual) { build(:individual, notification_method: 'invalid_method') } + + it 'raises an ArgumentError' do + expect { + invalid_individual.valid? + }.to raise_error(ArgumentError, "'invalid_method' is not a valid notification_method") + end + end end