FEATURE: match user title when primary group changes

When primary group changes and the user's title is the previous primary
group's title, change the title to the new primary group's title
This commit is contained in:
Kyle Zhao 2018-09-17 13:08:39 +08:00 committed by Sam
parent 53d34c69fc
commit 6659417807
3 changed files with 57 additions and 0 deletions

View File

@ -108,6 +108,7 @@ class User < ActiveRecord::Base
before_save :update_username_lower
before_save :ensure_password_is_hashed
before_save :match_title_to_primary_group_changes
after_save :expire_tokens_if_password_changed
after_save :clear_global_notice_if_needed
@ -1273,6 +1274,14 @@ class User < ActiveRecord::Base
end
end
def match_title_to_primary_group_changes
return unless primary_group_id_changed?
if title == Group.where(id: primary_group_id_was).pluck(:title).first
self.title = primary_group&.title
end
end
private
def previous_visit_at_update_required?(timestamp)

View File

@ -1814,4 +1814,16 @@ describe User do
end
describe '#match_title_to_primary_group_changes' do
let(:primary_group_a) { Fabricate(:group, title: 'A', users: [user]) }
let(:primary_group_b) { Fabricate(:group, title: 'B', users: [user]) }
it "updates user's title only when it is blank or matches the previous primary group" do
expect { user.update(primary_group: primary_group_a) }.to change { user.reload.title }.from(nil).to('A')
expect { user.update(primary_group: primary_group_b) }.to change { user.reload.title }.from('A').to('B')
user.update(title: 'Different')
expect { user.update(primary_group: primary_group_a) }.to_not change { user.reload.title }
end
end
end

View File

@ -381,6 +381,7 @@ RSpec.describe Admin::UsersController do
describe '#primary_group' do
let(:group) { Fabricate(:group) }
let(:another_user) { Fabricate(:coding_horror) }
let(:another_group) { Fabricate(:group, title: 'New') }
it "raises an error when the user doesn't have permission" do
sign_in(user)
@ -427,6 +428,41 @@ RSpec.describe Admin::UsersController do
another_user.reload
expect(another_user.primary_group_id).to eq(nil)
end
it "updates user's title when it matches the previous primary group title" do
group.update_columns(primary_group: true, title: 'Previous')
group.add(another_user)
another_group.add(another_user)
expect(another_user.reload.title).to eq('Previous')
put "/admin/users/#{another_user.id}/primary_group.json", params: {
primary_group_id: another_group.id
}
another_user.reload
expect(response.status).to eq(200)
expect(another_user.primary_group_id).to eq(another_group.id)
expect(another_user.title).to eq('New')
end
it "doesn't update user's title when it does not match the previous primary group title" do
another_user.update_columns(title: 'Different')
group.update_columns(primary_group: true, title: 'Previous')
another_group.add(another_user)
group.add(another_user)
expect(another_user.reload.title).to eq('Different')
put "/admin/users/#{another_user.id}/primary_group.json", params: {
primary_group_id: another_group.id
}
another_user.reload
expect(response.status).to eq(200)
expect(another_user.primary_group_id).to eq(another_group.id)
expect(another_user.title).to eq('Different')
end
end
describe '#destroy' do