Skip to content

Commit

Permalink
Merge pull request #19376 from kbrock/rails_52_groups
Browse files Browse the repository at this point in the history
Removing groups changes users current group
  • Loading branch information
jrafanie committed Oct 8, 2019
2 parents 50e7c68 + 981a439 commit 87fa4b7
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
3 changes: 2 additions & 1 deletion app/models/miq_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,8 @@ def ensure_can_be_destroyed
throw :abort unless errors[:base].empty?
end

# tell users that this group is goinga away - and the users should fix their current group
def reset_current_group_for_users
User.where(:id => user_ids, :current_group_id => id).each(&:change_current_group)
User.where(:current_group_id => id).each(&:change_current_group)
end
end
12 changes: 9 additions & 3 deletions spec/models/miq_group_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -534,9 +534,15 @@
testgroup3 = FactoryBot.create(:miq_group)
user1 = FactoryBot.create(:user, :miq_groups => [testgroup1, testgroup2], :current_group => testgroup2)
user2 = FactoryBot.create(:user, :miq_groups => [testgroup1, testgroup3], :current_group => testgroup3)
expect { testgroup2.destroy }.not_to raise_error
expect(User.find_by(:id => user1.id).current_group.id).to eq(testgroup1.id)
expect(User.find_by(:id => user2.id).current_group.id).to eq(testgroup3.id)
user3 = FactoryBot.create(:user, :miq_groups => [testgroup1, testgroup2], :current_group => testgroup1)

testgroup2.destroy
expect(user1.reload.current_group.id).to eq(testgroup1.id)
expect(user1.miq_group_ids).to eq([testgroup1.id])
expect(user2.reload.current_group.id).to eq(testgroup3.id)
expect(user2.miq_group_ids).to match_array([testgroup1.id, testgroup3.id])
expect(user3.reload.current_group.id).to eq(testgroup1.id)
expect(user3.miq_group_ids).to eq([testgroup1.id])
end

it "should not be called if the user does not have the deleted group as the current_group" do
Expand Down
2 changes: 1 addition & 1 deletion spec/models/miq_widget_set_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
context "with a group" do
it "being deleted" do
expect(MiqWidgetSet.count).to eq(1)
user.miq_groups = []
user.destroy
group.destroy
expect(MiqWidgetSet.count).to eq(0)
end
Expand Down

0 comments on commit 87fa4b7

Please sign in to comment.