Allow badges to be marked as "titleable".

This commit is contained in:
Vikhyat Korrapati 2014-04-25 23:55:29 +05:30
parent 535965263a
commit b4e037dfb2
15 changed files with 51 additions and 26 deletions

View file

@ -50,6 +50,13 @@
{{/if}} {{/if}}
</div> </div>
<div>
<span>
{{input type="checkbox" checked=allow_title}}
{{i18n admin.badges.allow_title}}
</span>
</div>
<div class='buttons'> <div class='buttons'>
<button {{action save}} {{bind-attr disabled=controller.disableSave}} class='btn btn-primary'>{{i18n admin.badges.save}}</button> <button {{action save}} {{bind-attr disabled=controller.disableSave}} class='btn btn-primary'>{{i18n admin.badges.save}}</button>
<span class='saving'>{{savingStatus}}</span> <span class='saving'>{{savingStatus}}</span>

View file

@ -18,10 +18,7 @@ Discourse.PreferencesBadgeTitleController = Ember.ArrayController.extend({
} }
}.property('saving'), }.property('saving'),
selectableUserBadges: Em.computed.filter('model', function(userBadge) { selectableUserBadges: Em.computed.filterBy('model', 'badge.allow_title', true),
var badgeType = userBadge.get('badge.badge_type.name');
return (badgeType === "Gold" || badgeType === "Silver");
}),
selectedUserBadge: function() { selectedUserBadge: function() {
var selectedUserBadgeId = parseInt(this.get('selectedUserBadgeId')); var selectedUserBadgeId = parseInt(this.get('selectedUserBadgeId'));
@ -34,9 +31,7 @@ Discourse.PreferencesBadgeTitleController = Ember.ArrayController.extend({
return selectedUserBadge; return selectedUserBadge;
}.property('selectedUserBadgeId'), }.property('selectedUserBadgeId'),
titleNotChanged: function() { titleNotChanged: Discourse.computed.propertyEqual('user.title', 'selectedUserBadge.badge.name'),
return this.get('user.title') === this.get('selectedUserBadge.badge.name');
}.property('selectedUserBadge', 'user.title'),
disableSave: Em.computed.or('saving', 'titleNotChanged'), disableSave: Em.computed.or('saving', 'titleNotChanged'),

View file

@ -33,15 +33,8 @@ Discourse.PreferencesController = Discourse.ObjectController.extend({
canEditName: Discourse.computed.setting('enable_names'), canEditName: Discourse.computed.setting('enable_names'),
canSelectTitle: function() { canSelectTitle: function() {
if (!Discourse.SiteSettings.enable_badges || this.get('model.badge_count') === 0) { return Discourse.SiteSettings.enable_badges && this.get('model.badge_count') > 0;
return false; }.property('model.badge_count'),
}
// 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'),
availableLocales: function() { availableLocales: function() {
return Discourse.SiteSettings.available_locales.split('|').map( function(s) { return Discourse.SiteSettings.available_locales.split('|').map( function(s) {

View file

@ -101,7 +101,8 @@ Discourse.Badge = Discourse.Model.extend({
data: { data: {
name: this.get('name'), name: this.get('name'),
description: this.get('description'), 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) { }).then(function(json) {
self.updateFromJson(json); self.updateFromJson(json);

View file

@ -194,7 +194,7 @@ Discourse.PreferencesBadgeTitleRoute = Discourse.RestrictedUserRoute.extend({
controller.set('selectedUserBadgeId', userBadge.get('id')); 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')); controller.set('selectedUserBadgeId', controller.get('selectableUserBadges')[0].get('id'));
} }
} }

View file

@ -348,6 +348,10 @@ section.details {
width: 350px; width: 350px;
} }
input[type="checkbox"] {
width: 20px;
}
textarea { textarea {
height: 200px; height: 200px;
} }

View file

@ -30,10 +30,11 @@ class Admin::BadgesController < Admin::AdminController
end end
def update_badge_from_params(badge) 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.name = params[:name]
badge.description = params[:description] badge.description = params[:description]
badge.badge_type = BadgeType.find(params[:badge_type_id]) badge.badge_type = BadgeType.find(params[:badge_type_id])
badge.allow_title = params[:allow_title]
badge badge
end end
end end

View file

@ -68,7 +68,7 @@ class UsersController < ApplicationController
guardian.ensure_can_edit!(user) guardian.ensure_can_edit!(user)
user_badge = UserBadge.find(params[:user_badge_id]) 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.title = user_badge.badge.name
user.save! user.save!
end end

View file

@ -4,6 +4,7 @@ class Badge < ActiveRecord::Base
validates :name, presence: true, uniqueness: true validates :name, presence: true, uniqueness: true
validates :badge_type, presence: true validates :badge_type, presence: true
validates :allow_title, inclusion: [true, false]
end end
# == Schema Information # == Schema Information
@ -17,8 +18,10 @@ end
# grant_count :integer default(0), not null # grant_count :integer default(0), not null
# created_at :datetime # created_at :datetime
# updated_at :datetime # updated_at :datetime
# allow_title :boolean default(FALSE), not null
# #
# Indexes # Indexes
# #
# index_badges_on_badge_type_id (badge_type_id)
# index_badges_on_name (name) UNIQUE # index_badges_on_name (name) UNIQUE
# #

View file

@ -1,5 +1,5 @@
class BadgeSerializer < ApplicationSerializer class BadgeSerializer < ApplicationSerializer
attributes :id, :name, :description, :grant_count attributes :id, :name, :description, :grant_count, :allow_title
has_one :badge_type has_one :badge_type
end end

View file

@ -1749,6 +1749,7 @@ en:
grant: Grant grant: Grant
no_user_badges: "%{name} has not been granted any badges." no_user_badges: "%{name} has not been granted any badges."
no_badges: There are no badges that can be granted. no_badges: There are no badges that can be granted.
allow_title: Allow badge to be used as a title
lightbox: lightbox:
download: "download" download: "download"

View file

@ -0,0 +1,5 @@
class AddTitleableToBadges < ActiveRecord::Migration
def change
add_column :badges, :allow_title, :boolean, null: false, default: false
end
end

View file

@ -35,12 +35,12 @@ describe Admin::BadgesController do
context '.update' do context '.update' do
it 'returns success' 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 response.should be_success
end end
it 'updates the badge' do 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') badge.reload.name.should eq('123456')
end end
end end

View file

@ -5,7 +5,7 @@ describe BadgesController do
context 'index' do context 'index' do
it 'should return a list of all badges' do it 'should return a list of all badges' do
xhr :get, :index get :index, format: :json
response.status.should == 200 response.status.should == 200
parsed = JSON.parse(response.body) parsed = JSON.parse(response.body)
@ -15,7 +15,7 @@ describe BadgesController do
context 'show' do context 'show' do
it "should return a badge" do it "should return a badge" do
xhr :get, :show, id: badge.id get :show, id: badge.id, format: :json
response.status.should == 200 response.status.should == 200
parsed = JSON.parse(response.body) parsed = JSON.parse(response.body)
parsed["badge"].should be_present parsed["badge"].should be_present

View file

@ -996,6 +996,21 @@ describe UsersController do
end end
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 describe "search_users" do
let(:topic) { Fabricate :topic } let(:topic) { Fabricate :topic }