From b4e037dfb243fd06c5525e463c65c40016ceddbe Mon Sep 17 00:00:00 2001 From: Vikhyat Korrapati Date: Fri, 25 Apr 2014 23:55:29 +0530 Subject: [PATCH] Allow badges to be marked as "titleable". --- .../admin/templates/badges.js.handlebars | 7 +++++++ .../preferences_badge_title_controller.js | 9 ++------- .../controllers/preferences_controller.js | 11 ++--------- app/assets/javascripts/discourse/models/badge.js | 3 ++- .../discourse/routes/preferences_routes.js | 2 +- .../stylesheets/common/admin/admin_base.scss | 4 ++++ app/controllers/admin/badges_controller.rb | 3 ++- app/controllers/users_controller.rb | 2 +- app/models/badge.rb | 5 ++++- app/serializers/badge_serializer.rb | 2 +- config/locales/client.en.yml | 1 + .../20140425172618_add_titleable_to_badges.rb | 5 +++++ spec/controllers/admin/badges_controller_spec.rb | 4 ++-- spec/controllers/badges_controller_spec.rb | 4 ++-- spec/controllers/users_controller_spec.rb | 15 +++++++++++++++ 15 files changed, 51 insertions(+), 26 deletions(-) create mode 100644 db/migrate/20140425172618_add_titleable_to_badges.rb diff --git a/app/assets/javascripts/admin/templates/badges.js.handlebars b/app/assets/javascripts/admin/templates/badges.js.handlebars index e11ba2ff7..dc34984a1 100644 --- a/app/assets/javascripts/admin/templates/badges.js.handlebars +++ b/app/assets/javascripts/admin/templates/badges.js.handlebars @@ -50,6 +50,13 @@ {{/if}} +
+ + {{input type="checkbox" checked=allow_title}} + {{i18n admin.badges.allow_title}} + +
+
{{savingStatus}} diff --git a/app/assets/javascripts/discourse/controllers/preferences_badge_title_controller.js b/app/assets/javascripts/discourse/controllers/preferences_badge_title_controller.js index 85352626d..a7af81ee4 100644 --- a/app/assets/javascripts/discourse/controllers/preferences_badge_title_controller.js +++ b/app/assets/javascripts/discourse/controllers/preferences_badge_title_controller.js @@ -18,10 +18,7 @@ Discourse.PreferencesBadgeTitleController = Ember.ArrayController.extend({ } }.property('saving'), - selectableUserBadges: Em.computed.filter('model', function(userBadge) { - var badgeType = userBadge.get('badge.badge_type.name'); - return (badgeType === "Gold" || badgeType === "Silver"); - }), + selectableUserBadges: Em.computed.filterBy('model', 'badge.allow_title', true), selectedUserBadge: function() { var selectedUserBadgeId = parseInt(this.get('selectedUserBadgeId')); @@ -34,9 +31,7 @@ Discourse.PreferencesBadgeTitleController = Ember.ArrayController.extend({ return selectedUserBadge; }.property('selectedUserBadgeId'), - titleNotChanged: function() { - return this.get('user.title') === this.get('selectedUserBadge.badge.name'); - }.property('selectedUserBadge', 'user.title'), + titleNotChanged: Discourse.computed.propertyEqual('user.title', 'selectedUserBadge.badge.name'), disableSave: Em.computed.or('saving', 'titleNotChanged'), diff --git a/app/assets/javascripts/discourse/controllers/preferences_controller.js b/app/assets/javascripts/discourse/controllers/preferences_controller.js index 851dce9ac..d837e6ac1 100644 --- a/app/assets/javascripts/discourse/controllers/preferences_controller.js +++ b/app/assets/javascripts/discourse/controllers/preferences_controller.js @@ -33,15 +33,8 @@ Discourse.PreferencesController = Discourse.ObjectController.extend({ canEditName: Discourse.computed.setting('enable_names'), canSelectTitle: function() { - if (!Discourse.SiteSettings.enable_badges || this.get('model.badge_count') === 0) { - return false; - } - - // If the first featured badge isn't gold or silver we know the user won't have - // _any_ gold or silver badges. - var badgeType = this.get('model.featured_user_badges')[0].get('badge.badge_type.name'); - return (badgeType === "Gold" || badgeType === "Silver"); - }.property('model.badge_count', 'model.featured_user_badges.@each.badge.badge_type.name'), + return Discourse.SiteSettings.enable_badges && this.get('model.badge_count') > 0; + }.property('model.badge_count'), availableLocales: function() { return Discourse.SiteSettings.available_locales.split('|').map( function(s) { diff --git a/app/assets/javascripts/discourse/models/badge.js b/app/assets/javascripts/discourse/models/badge.js index c802f356d..0d629d175 100644 --- a/app/assets/javascripts/discourse/models/badge.js +++ b/app/assets/javascripts/discourse/models/badge.js @@ -101,7 +101,8 @@ Discourse.Badge = Discourse.Model.extend({ data: { name: this.get('name'), description: this.get('description'), - badge_type_id: this.get('badge_type_id') + badge_type_id: this.get('badge_type_id'), + allow_title: this.get('allow_title') } }).then(function(json) { self.updateFromJson(json); diff --git a/app/assets/javascripts/discourse/routes/preferences_routes.js b/app/assets/javascripts/discourse/routes/preferences_routes.js index ef59b99d4..eae59f72e 100644 --- a/app/assets/javascripts/discourse/routes/preferences_routes.js +++ b/app/assets/javascripts/discourse/routes/preferences_routes.js @@ -194,7 +194,7 @@ Discourse.PreferencesBadgeTitleRoute = Discourse.RestrictedUserRoute.extend({ controller.set('selectedUserBadgeId', userBadge.get('id')); } }); - if (!controller.get('selectedUserBadgeId')) { + if (!controller.get('selectedUserBadgeId') && controller.get('selectableUserBadges.length') > 0) { controller.set('selectedUserBadgeId', controller.get('selectableUserBadges')[0].get('id')); } } diff --git a/app/assets/stylesheets/common/admin/admin_base.scss b/app/assets/stylesheets/common/admin/admin_base.scss index 6412c8369..0ef6d1dbd 100644 --- a/app/assets/stylesheets/common/admin/admin_base.scss +++ b/app/assets/stylesheets/common/admin/admin_base.scss @@ -348,6 +348,10 @@ section.details { width: 350px; } + input[type="checkbox"] { + width: 20px; + } + textarea { height: 200px; } diff --git a/app/controllers/admin/badges_controller.rb b/app/controllers/admin/badges_controller.rb index 92b119af8..08991e8ef 100644 --- a/app/controllers/admin/badges_controller.rb +++ b/app/controllers/admin/badges_controller.rb @@ -30,10 +30,11 @@ class Admin::BadgesController < Admin::AdminController end def update_badge_from_params(badge) - params.permit(:name, :description, :badge_type_id) + params.permit(:name, :description, :badge_type_id, :allow_title) badge.name = params[:name] badge.description = params[:description] badge.badge_type = BadgeType.find(params[:badge_type_id]) + badge.allow_title = params[:allow_title] badge end end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 9ed1fac36..826304797 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -68,7 +68,7 @@ class UsersController < ApplicationController guardian.ensure_can_edit!(user) user_badge = UserBadge.find(params[:user_badge_id]) - if user_badge.user == user && ["Gold", "Silver"].include?(user_badge.badge.badge_type.name) + if user_badge.user == user && user_badge.badge.allow_title? user.title = user_badge.badge.name user.save! end diff --git a/app/models/badge.rb b/app/models/badge.rb index aea3895c4..9f8cee902 100644 --- a/app/models/badge.rb +++ b/app/models/badge.rb @@ -4,6 +4,7 @@ class Badge < ActiveRecord::Base validates :name, presence: true, uniqueness: true validates :badge_type, presence: true + validates :allow_title, inclusion: [true, false] end # == Schema Information @@ -17,8 +18,10 @@ end # grant_count :integer default(0), not null # created_at :datetime # updated_at :datetime +# allow_title :boolean default(FALSE), not null # # Indexes # -# index_badges_on_name (name) UNIQUE +# index_badges_on_badge_type_id (badge_type_id) +# index_badges_on_name (name) UNIQUE # diff --git a/app/serializers/badge_serializer.rb b/app/serializers/badge_serializer.rb index 34b729598..90be747c4 100644 --- a/app/serializers/badge_serializer.rb +++ b/app/serializers/badge_serializer.rb @@ -1,5 +1,5 @@ class BadgeSerializer < ApplicationSerializer - attributes :id, :name, :description, :grant_count + attributes :id, :name, :description, :grant_count, :allow_title has_one :badge_type end diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 89b96f933..db1573c91 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -1749,6 +1749,7 @@ en: grant: Grant no_user_badges: "%{name} has not been granted any badges." no_badges: There are no badges that can be granted. + allow_title: Allow badge to be used as a title lightbox: download: "download" diff --git a/db/migrate/20140425172618_add_titleable_to_badges.rb b/db/migrate/20140425172618_add_titleable_to_badges.rb new file mode 100644 index 000000000..346f3314f --- /dev/null +++ b/db/migrate/20140425172618_add_titleable_to_badges.rb @@ -0,0 +1,5 @@ +class AddTitleableToBadges < ActiveRecord::Migration + def change + add_column :badges, :allow_title, :boolean, null: false, default: false + end +end diff --git a/spec/controllers/admin/badges_controller_spec.rb b/spec/controllers/admin/badges_controller_spec.rb index 62674af34..b507c6a7a 100644 --- a/spec/controllers/admin/badges_controller_spec.rb +++ b/spec/controllers/admin/badges_controller_spec.rb @@ -35,12 +35,12 @@ describe Admin::BadgesController do context '.update' do it 'returns success' do - xhr :put, :update, id: badge.id, name: "123456", badge_type_id: badge.badge_type_id + xhr :put, :update, id: badge.id, name: "123456", badge_type_id: badge.badge_type_id, allow_title: false response.should be_success end it 'updates the badge' do - xhr :put, :update, id: badge.id, name: "123456", badge_type_id: badge.badge_type_id + xhr :put, :update, id: badge.id, name: "123456", badge_type_id: badge.badge_type_id, allow_title: false badge.reload.name.should eq('123456') end end diff --git a/spec/controllers/badges_controller_spec.rb b/spec/controllers/badges_controller_spec.rb index 945d64151..adc3957d0 100644 --- a/spec/controllers/badges_controller_spec.rb +++ b/spec/controllers/badges_controller_spec.rb @@ -5,7 +5,7 @@ describe BadgesController do context 'index' do it 'should return a list of all badges' do - xhr :get, :index + get :index, format: :json response.status.should == 200 parsed = JSON.parse(response.body) @@ -15,7 +15,7 @@ describe BadgesController do context 'show' do it "should return a badge" do - xhr :get, :show, id: badge.id + get :show, id: badge.id, format: :json response.status.should == 200 parsed = JSON.parse(response.body) parsed["badge"].should be_present diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb index 7d25a2da3..c70e4e373 100644 --- a/spec/controllers/users_controller_spec.rb +++ b/spec/controllers/users_controller_spec.rb @@ -996,6 +996,21 @@ describe UsersController do end end + describe "badge_title" do + let(:user) { Fabricate(:user) } + let(:badge) { Fabricate(:badge) } + let(:user_badge) { BadgeGranter.grant(badge, user) } + + it "sets the user's title to the badge name if it is titleable" do + log_in_user user + xhr :put, :badge_title, user_badge_id: user_badge.id, username: user.username + user.reload.title.should_not == badge.name + badge.update_attributes allow_title: true + xhr :put, :badge_title, user_badge_id: user_badge.id, username: user.username + user.reload.title.should == badge.name + end + end + describe "search_users" do let(:topic) { Fabricate :topic }