Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: match user title when primary group changes

Kyle Zhao665941780732
diff --git a/app/models/user.rb b/app/models/user.rb
index fe0e40bf461..3062c9678fc 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -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)
diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb
index 948a315d529..4516ff0c6f8 100644
--- a/spec/models/user_spec.rb
+++ b/spec/models/user_spec.rb
@@ -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
diff --git a/spec/requests/admin/users_controller_spec.rb b/spec/requests/admin/users_controller_spec.rb
index 2c8cc43f5fa..886eeae9fbf 100644
--- a/spec/requests/admin/users_controller_spec.rb
+++ b/spec/requests/admin/users_controller_spec.rb
@@ -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