Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call really_destroy! in validate_roles, to not throw error when role … #1444

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/models/memberships/common_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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! }
Copy link
Contributor Author

@njaeggi njaeggi Dec 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should call the same as we would in our save method anyways, where we use really_destroy! for all roles marked for destruction...

This still won't destroy the role on validation, because of the transaction

roles_to_update.each { |role| save_role_without_validations(role) }
roles_to_update.each { |role| validate_role(role) }
raise ActiveRecord::Rollback
Expand Down
17 changes: 17 additions & 0 deletions spec/models/memberships/switch_stammsektion_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason calling .delete only fails when calling this method twice? In our request this method is called twice, resulting in this bug, to recreate it in this spec we have to call it twice...

end

describe "existing membership in tree" do
describe "join section" do
it "is invalid if person is join_section member" do
Expand Down Expand Up @@ -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
Expand Down
Loading