Show system badges in the admin interface but don't allow editing them.

This commit is contained in:
Vikhyat Korrapati 2014-05-19 09:59:12 +05:30
parent 9e3c08b5ee
commit 6225b83f4a
5 changed files with 49 additions and 57 deletions
app/assets/javascripts/admin
test/javascripts/admin/controllers

View file

@ -15,5 +15,21 @@ Discourse.AdminBadgeController = Discourse.ObjectController.extend({
@property selected @property selected
@type {Boolean} @type {Boolean}
**/ **/
selected: Discourse.computed.propertyEqual('model.name', 'parentController.selectedItem.name') selected: Discourse.computed.propertyEqual('model.name', 'parentController.selectedItem.name'),
/**
Show the displayName only if it is different from the name.
@property showDisplayName
@type {Boolean}
**/
showDisplayName: Discourse.computed.propertyNotEqual('selectedItem.name', 'selectedItem.displayName'),
/**
Don't allow editing if this is a system badge.
@property readOnly
@type {Boolean}
**/
readOnly: Ember.computed.lt('model.id', 100)
}); });

View file

@ -10,16 +10,8 @@ Discourse.AdminBadgesController = Ember.ArrayController.extend({
itemController: 'adminBadge', itemController: 'adminBadge',
/** /**
Show the displayName only if it is different from the name. We don't allow setting a description if a translation for the given badge
name exists.
@property showDisplayName
@type {Boolean}
**/
showDisplayName: Discourse.computed.propertyNotEqual('selectedItem.name', 'selectedItem.displayName'),
/**
We don't allow setting a description if a translation for the given badge name
exists.
@property canEditDescription @property canEditDescription
@type {Boolean} @type {Boolean}

View file

@ -1,11 +1,7 @@
Discourse.AdminBadgesRoute = Discourse.Route.extend({ Discourse.AdminBadgesRoute = Discourse.Route.extend({
model: function() { model: function() {
return Discourse.Badge.findAll().then(function(badges) { return Discourse.Badge.findAll();
return badges.filter(function(badge) {
return badge.id >= 100;
});
});
}, },
setupController: function(controller, model) { setupController: function(controller, model) {

View file

@ -18,19 +18,19 @@
</div> </div>
{{#if selectedItem}} {{#if selectedItem}}
{{#with selectedItem}} {{#with selectedItem controller='adminBadge'}}
<div class='current-badge span13'> <div class='current-badge span13'>
<form class="form-horizontal"> <form class="form-horizontal">
<div> <div>
<label for="name">{{i18n admin.badges.name}}</label> <label for="name">{{i18n admin.badges.name}}</label>
{{input type="text" name="name" value=name}} {{input type="text" name="name" value=name disabled=readOnly}}
</div> </div>
{{#if controller.showDisplayName}} {{#if showDisplayName}}
<div> <div>
<strong>{{i18n admin.badges.display_name}}</strong> <strong>{{i18n admin.badges.display_name}}</strong>
{{displayName}} {{displayName}}
</div> </div>
{{/if}} {{/if}}
<div> <div>
@ -38,7 +38,8 @@
{{view Ember.Select name="badge_type_id" value=badge_type_id {{view Ember.Select name="badge_type_id" value=badge_type_id
content=controller.badgeTypes content=controller.badgeTypes
optionValuePath="content.id" optionValuePath="content.id"
optionLabelPath="content.name"}} optionLabelPath="content.name"
disabled=readOnly}}
</div> </div>
<div> <div>
@ -50,18 +51,20 @@
{{/if}} {{/if}}
</div> </div>
<div> {{#unless readOnly}}
<span> <div>
{{input type="checkbox" checked=allow_title}} <span>
{{i18n admin.badges.allow_title}} {{input type="checkbox" checked=allow_title}}
</span> {{i18n admin.badges.allow_title}}
</div> </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>
<a {{action destroy}} class='delete-link'>{{i18n admin.badges.delete}}</a> <a {{action destroy}} class='delete-link'>{{i18n admin.badges.delete}}</a>
</div> </div>
{{/unless}}
</form> </form>
</div> </div>
{{/with}} {{/with}}

View file

@ -1,30 +1,15 @@
module("Discourse.AdminBadgesController"); module("Discourse.AdminBadgesController");
test("showDisplayName", function() {
var badge, controller;
badge = Discourse.Badge.create({name: "Test Badge"});
controller = testController(Discourse.AdminBadgesController, [badge]);
controller.send('selectBadge', badge);
ok(!controller.get('showDisplayName'), "does not show displayName when it is the same as the name");
this.stub(I18n, "t").returns("translated string");
badge = Discourse.Badge.create({name: "Test Badge"});
controller = testController(Discourse.AdminBadgesController, [badge]);
controller.send('selectBadge', badge);
ok(controller.get('showDisplayName'), "shows the displayName when it is different from the name");
});
test("canEditDescription", function() { test("canEditDescription", function() {
var badge, controller; var badge, controller;
badge = Discourse.Badge.create({name: "Test Badge"}); badge = Discourse.Badge.create({id: 101, name: "Test Badge"});
controller = testController(Discourse.AdminBadgesController, [badge]); controller = testController(Discourse.AdminBadgesController, [badge]);
controller.send('selectBadge', badge); controller.send('selectBadge', badge);
ok(controller.get('canEditDescription'), "allows editing description when a translation exists for the badge name"); ok(controller.get('canEditDescription'), "allows editing description when a translation exists for the badge name");
this.stub(I18n, "t").returns("translated string"); this.stub(I18n, "t").returns("translated string");
badge = Discourse.Badge.create({name: "Test Badge"}); badge = Discourse.Badge.create({id: 102, name: "Test Badge"});
controller = testController(Discourse.AdminBadgesController, [badge]); controller = testController(Discourse.AdminBadgesController, [badge]);
controller.send('selectBadge', badge); controller.send('selectBadge', badge);
ok(!controller.get('canEditDescription'), "shows the displayName when it is different from the name"); ok(!controller.get('canEditDescription'), "shows the displayName when it is different from the name");
@ -38,7 +23,7 @@ test("newBadge", function() {
}); });
test("selectBadge", function() { test("selectBadge", function() {
var badge = Discourse.Badge.create({name: "Test Badge"}), var badge = Discourse.Badge.create({id: 101, name: "Test Badge"}),
controller = testController(Discourse.AdminBadgesController, [badge]); controller = testController(Discourse.AdminBadgesController, [badge]);
controller.send('selectBadge', badge); controller.send('selectBadge', badge);
@ -46,8 +31,8 @@ test("selectBadge", function() {
}); });
test("save", function() { test("save", function() {
var badge = Discourse.Badge.create({name: "Test Badge"}), var badge = Discourse.Badge.create({id: 101, name: "Test Badge"}),
otherBadge = Discourse.Badge.create({name: "Other Badge"}), otherBadge = Discourse.Badge.create({id: 102, name: "Other Badge"}),
controller = testController(Discourse.AdminBadgesController, [badge, otherBadge]); controller = testController(Discourse.AdminBadgesController, [badge, otherBadge]);
controller.send('selectBadge', badge); controller.send('selectBadge', badge);
@ -57,8 +42,8 @@ test("save", function() {
}); });
test("destroy", function() { test("destroy", function() {
var badge = Discourse.Badge.create({name: "Test Badge"}), var badge = Discourse.Badge.create({id: 101, name: "Test Badge"}),
otherBadge = Discourse.Badge.create({name: "Other Badge"}), otherBadge = Discourse.Badge.create({id: 102, name: "Other Badge"}),
controller = testController(Discourse.AdminBadgesController, [badge, otherBadge]); controller = testController(Discourse.AdminBadgesController, [badge, otherBadge]);
this.stub(badge, 'destroy').returns(Ember.RSVP.resolve({})); this.stub(badge, 'destroy').returns(Ember.RSVP.resolve({}));