diff --git a/app/assets/javascripts/discourse/controllers/preferences.js.es6 b/app/assets/javascripts/discourse/controllers/preferences.js.es6 index 680f4cf43..d8fdd6c10 100644 --- a/app/assets/javascripts/discourse/controllers/preferences.js.es6 +++ b/app/assets/javascripts/discourse/controllers/preferences.js.es6 @@ -32,7 +32,7 @@ export default Discourse.ObjectController.extend({ canEditName: Discourse.computed.setting('enable_names'), canSelectTitle: function() { - return Discourse.SiteSettings.enable_badges && this.get('model.badge_count') > 0; + return Discourse.SiteSettings.enable_badges && this.get('model.has_title_badges'); }.property('model.badge_count'), availableLocales: function() { diff --git a/app/models/user.rb b/app/models/user.rb index 6d8d89f3c..318ce9f12 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -24,6 +24,7 @@ class User < ActiveRecord::Base has_many :user_actions, dependent: :destroy has_many :post_actions, dependent: :destroy has_many :user_badges, dependent: :destroy + has_many :badges, through: :user_badges has_many :email_logs, dependent: :destroy has_many :post_timings has_many :topic_allowed_users, dependent: :destroy diff --git a/app/serializers/user_serializer.rb b/app/serializers/user_serializer.rb index 52fbadbfd..3968dc2b7 100644 --- a/app/serializers/user_serializer.rb +++ b/app/serializers/user_serializer.rb @@ -42,7 +42,8 @@ class UserSerializer < BasicUserSerializer :suspend_reason, :suspended_till, :uploaded_avatar_id, - :badge_count + :badge_count, + :has_title_badges has_one :invited_by, embed: :object, serializer: BasicUserSerializer has_many :custom_groups, embed: :object, serializer: BasicGroupSerializer @@ -75,7 +76,8 @@ class UserSerializer < BasicUserSerializer :disable_jump_reply, :gravatar_avatar_upload_id, :custom_avatar_upload_id, - :custom_fields + :custom_fields, + :has_title_badges ### ### ATTRIBUTES @@ -230,4 +232,8 @@ class UserSerializer < BasicUserSerializer object.user_avatar.try(:custom_upload_id) end + def has_title_badges + object.badges.where(allow_title: true).count > 0 + end + end diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb index 8cbe1f84d..42ccc49be 100644 --- a/spec/controllers/users_controller_spec.rb +++ b/spec/controllers/users_controller_spec.rb @@ -11,8 +11,12 @@ describe UsersController do let!(:user) { log_in } it 'returns success' do - xhr :get, :show, username: user.username + xhr :get, :show, username: user.username, format: :json response.should be_success + json = JSON.parse(response.body) + + json["user"]["has_title_badges"].should == false + end it "returns not found when the username doesn't exist" do