FIX: removing a group from a user was not removing primary group
This commit is contained in:
parent
4a5d79ad89
commit
e8323fa534
|
@ -93,9 +93,12 @@ class Admin::GroupsController < Admin::AdminController
|
||||||
|
|
||||||
return can_not_modify_automatic if group.automatic
|
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)
|
group.users.delete(user_id)
|
||||||
|
|
||||||
if group.save
|
if group.save && user.save
|
||||||
render json: success_json
|
render json: success_json
|
||||||
else
|
else
|
||||||
render_json_error(group)
|
render_json_error(group)
|
||||||
|
|
|
@ -279,6 +279,7 @@ class Group < ActiveRecord::Base
|
||||||
|
|
||||||
def remove(user)
|
def remove(user)
|
||||||
self.group_users.where(user: user).each(&:destroy)
|
self.group_users.where(user: user).each(&:destroy)
|
||||||
|
user.update_columns(primary_group_id: nil) if user.primary_group_id == self.id
|
||||||
end
|
end
|
||||||
|
|
||||||
def appoint_manager(user)
|
def appoint_manager(user)
|
||||||
|
|
|
@ -137,11 +137,17 @@ describe Admin::GroupsController do
|
||||||
group.add(user)
|
group.add(user)
|
||||||
group.save
|
group.save
|
||||||
|
|
||||||
|
user.primary_group_id = group.id
|
||||||
|
user.save
|
||||||
|
|
||||||
xhr :delete, :remove_member, id: group.id, user_id: user.id
|
xhr :delete, :remove_member, id: group.id, user_id: user.id
|
||||||
|
|
||||||
expect(response).to be_success
|
expect(response).to be_success
|
||||||
group.reload
|
group.reload
|
||||||
expect(group.users.count).to eq(0)
|
expect(group.users.count).to eq(0)
|
||||||
|
|
||||||
|
user.reload
|
||||||
|
expect(user.primary_group_id).to eq(nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -47,6 +47,24 @@ describe Group do
|
||||||
Group[:staff].user_ids - [-1]
|
Group[:staff].user_ids - [-1]
|
||||||
end
|
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
|
it "Can update moderator/staff/admin groups correctly" do
|
||||||
|
|
||||||
admin = Fabricate(:admin)
|
admin = Fabricate(:admin)
|
||||||
|
|
Loading…
Reference in New Issue