FIX: removing a group from a user was not removing primary group

This commit is contained in:
Sam 2015-02-09 16:03:09 +11:00
parent 4a5d79ad89
commit e8323fa534
4 changed files with 29 additions and 1 deletions

View File

@ -93,9 +93,12 @@ class Admin::GroupsController < Admin::AdminController
return can_not_modify_automatic if group.automatic
user = User.find(user_id)
user.primary_group_id = nil if user.primary_group_id == group.id
group.users.delete(user_id)
if group.save
if group.save && user.save
render json: success_json
else
render_json_error(group)

View File

@ -279,6 +279,7 @@ class Group < ActiveRecord::Base
def remove(user)
self.group_users.where(user: user).each(&:destroy)
user.update_columns(primary_group_id: nil) if user.primary_group_id == self.id
end
def appoint_manager(user)

View File

@ -137,11 +137,17 @@ describe Admin::GroupsController do
group.add(user)
group.save
user.primary_group_id = group.id
user.save
xhr :delete, :remove_member, id: group.id, user_id: user.id
expect(response).to be_success
group.reload
expect(group.users.count).to eq(0)
user.reload
expect(user.primary_group_id).to eq(nil)
end
end

View File

@ -47,6 +47,24 @@ describe Group do
Group[:staff].user_ids - [-1]
end
it "Correctly handles primary group" do
group = Fabricate(:group)
user = Fabricate(:user)
group.add(user)
group.save
user.primary_group = group
user.save
group.reload
group.remove(user)
group.save
user.reload
expect(user.primary_group).to eq nil
end
it "Can update moderator/staff/admin groups correctly" do
admin = Fabricate(:admin)