diff --git a/app/models/memberships/common_api.rb b/app/models/memberships/common_api.rb index 34a1a5376..fa3285dd6 100644 --- a/app/models/memberships/common_api.rb +++ b/app/models/memberships/common_api.rb @@ -40,7 +40,7 @@ def validate_roles # But this method should not save the roles, so we must roll back after checking the validity. Role.transaction(requires_new: true) do roles_to_destroy, roles_to_update = roles.partition(&:marked_for_destruction?) - roles_to_destroy.each { |role| role.delete } + roles_to_destroy.each { |role| role.really_destroy! } roles_to_update.each { |role| save_role_without_validations(role) } roles_to_update.each { |role| validate_role(role) } raise ActiveRecord::Rollback diff --git a/spec/models/memberships/switch_stammsektion_spec.rb b/spec/models/memberships/switch_stammsektion_spec.rb index 6a41196a7..890f0073e 100644 --- a/spec/models/memberships/switch_stammsektion_spec.rb +++ b/spec/models/memberships/switch_stammsektion_spec.rb @@ -50,6 +50,12 @@ def create_role(key, role, owner: person, **attrs) expect(switch).to be_valid end + it "is valid with membership in different section active since today" do + create_role(:matterhorn_mitglieder, "Mitglied", start_on: Time.zone.today) + expect(switch).to be_valid + expect(switch).to be_valid + end + describe "existing membership in tree" do describe "join section" do it "is invalid if person is join_section member" do @@ -110,6 +116,17 @@ def create_role(key, role, owner: person, **attrs) expect(person.primary_group).to eq matterhorn_mitglieder end + it "creates new role and destroys existing when start_on is today" do + bluemlisalp_mitglied.update_column(:start_on, Time.zone.today) + expect do + expect(switch.save).to eq true + end.not_to(change { person.reload.roles.count }) + expect { bluemlisalp_mitglied.reload }.to raise_error(ActiveRecord::RecordNotFound) + expect(matterhorn_mitglied.start_on).to eq now.to_date + expect(matterhorn_mitglied.end_on).to eq now.end_of_year.to_date + expect(person.primary_group).to eq matterhorn_mitglieder + end + it "creates new role and destroys existing if not yet active" do bluemlisalp_mitglied.update_columns(created_at: 1.minute.ago) expect do