Skip to content

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

FIX: Allow users to change title in locales other than English

Gerhard Schlager6d6ffe513138
diff --git a/lib/guardian.rb b/lib/guardian.rb
index 4405a860234..668b7677ef2 100644
--- a/lib/guardian.rb
+++ b/lib/guardian.rb
@@ -312,7 +312,13 @@ class Guardian
return false if title.nil?
return true if title.empty? # A title set to '(none)' in the UI is an empty string
return false if user != @user
- return true if user.badges.where(name: title, allow_title: true).exists?
+
+ return true if user.badges
+ .where(allow_title: true)
+ .pluck(:name)
+ .map { |name| Badge.display_name(name) }
+ .include?(title)
+
user.groups.where(title: title).exists?
end
diff --git a/spec/requests/users_controller_spec.rb b/spec/requests/users_controller_spec.rb
index f318ca22e6c..c7898ab7e03 100644
--- a/spec/requests/users_controller_spec.rb
+++ b/spec/requests/users_controller_spec.rb
@@ -1746,10 +1746,24 @@ describe UsersController do
end
context 'a locale is chosen that differs from I18n.locale' do
+ before do
+ SiteSetting.allow_user_locale = true
+ end
+
it "updates the user's locale" do
- I18n.stubs(:locale).returns('fr')
+ I18n.locale = :fr
put "/u/#{user.username}.json", params: { locale: :fa_IR }
- expect(User.find_by(username: user.username).locale).to eq('fa_IR')
+ expect(user.reload.locale).to eq('fa_IR')
+ end
+
+ it "updates the title" do
+ user.update!(locale: :fr)
+ user.change_trust_level!(TrustLevel[4])
+ BadgeGranter.process_queue!
+
+ leader_title = I18n.t("badges.leader.name", locale: :fr)
+ put "/u/#{user.username}.json", params: { title: leader_title }
+ expect(user.reload.title).to eq(leader_title)
end
end
@@ -1951,7 +1965,7 @@ describe UsersController do
expect(response.status).to eq(200)
end
- context "with overrided name" do
+ context "with overridden name" do
fab!(:badge) { Fabricate(:badge, name: 'Demogorgon', allow_title: true) }
let(:user_badge) { BadgeGranter.grant(badge, user) }