diff --git a/app/models/user.rb b/app/models/user.rb index 27187529a..d25faaa53 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -264,7 +264,8 @@ class User < ApplicationRecord end def discard! - update!(discarded_at: Time.zone.now) + self.discarded_at = Time.zone.now + save!(validate: false) end protected diff --git a/spec/models/organisation_spec.rb b/spec/models/organisation_spec.rb index d00f79039..3964923f0 100644 --- a/spec/models/organisation_spec.rb +++ b/spec/models/organisation_spec.rb @@ -298,4 +298,25 @@ RSpec.describe Organisation, type: :model do expect(organisation.status).to be(:active) end end + + describe "discard" do + let(:organisation) { create(:organisation) } + let!(:user) { create(:user, organisation:) } + let!(:scheme) { create(:scheme, owning_organisation: organisation) } + + context "when merged organisation is discarded" do + before do + organisation.merge_date = Time.zone.yesterday + organisation.absorbing_organisation_id = create(:organisation).id + organisation.save! + end + + it "discards all of the organisation resources" do + organisation.discard! + expect(organisation.status).to eq(:deleted) + expect(user.reload.status).to eq(:deleted) + expect(scheme.reload.status).to eq(:deleted) + end + end + end end