Refactor: move category slug helper to Category model

This commit is contained in:
Robin Ward 2013-05-29 14:18:02 -04:00
parent fe3ac50aae
commit 3d0587d8ce
6 changed files with 32 additions and 27 deletions

View File

@ -29,14 +29,6 @@ Discourse.Utilities = {
}
},
categoryUrlId: function(category) {
if (!category) return "";
var id = Em.get(category, 'id');
var slug = Em.get(category, 'slug');
if ((!slug) || slug.isBlank()) return "" + id + "-category";
return slug;
},
// Create a badge like category link
categoryLink: function(category) {
if (!category) return "";
@ -47,7 +39,7 @@ Discourse.Utilities = {
var description = Em.get(category, 'description');
// Build the HTML link
var result = "<a href=\"" + Discourse.getURL("/category/") + this.categoryUrlId(category) + "\" class=\"badge-category\" ";
var result = "<a href=\"" + Discourse.getURL("/category/") + Discourse.Category.slugFor(category) + "\" class=\"badge-category\" ";
// Add description if we have it
if (description) result += "title=\"" + Handlebars.Utils.escapeExpression(description) + "\" ";

View File

@ -82,6 +82,14 @@ Discourse.Category.reopenClass({
return this.uncategorized;
},
slugFor: function(category) {
if (!category) return "";
var id = Em.get(category, 'id');
var slug = Em.get(category, 'slug');
if ((!slug) || slug.isBlank()) return "" + id + "-category";
return slug;
},
list: function() {
return Discourse.Site.instance().get('categories');
},

View File

@ -36,7 +36,7 @@ Discourse.ListCategoryRoute = Discourse.FilteredListRoute.extend({
}
var listController = this.controllerFor('list');
var urlId = Discourse.Utilities.categoryUrlId(category);
var urlId = Discourse.Category.slugFor(category);
listController.set('filterMode', "category/" + urlId);
var router = this;

View File

@ -152,7 +152,7 @@ Discourse.EditCategoryView = Discourse.ModalBodyView.extend({
this.get('category').save().then(function(result) {
// success
$('#discourse-modal').modal('hide');
Discourse.URL.redirectTo("/category/" + Discourse.Utilities.categoryUrlId(result.category));
Discourse.URL.redirectTo("/category/" + Discourse.Category.slugFor(result.category));
}, function(errors) {
// errors
if(errors.length === 0) errors.push(Em.String.i18n("category.creation_error"));

View File

@ -2,22 +2,6 @@
describe("Discourse.Utilities", function() {
describe("categoryUrlId", function() {
it("returns the slug when it exists", function() {
expect(Discourse.Utilities.categoryUrlId({ slug: 'hello' })).toBe("hello");
});
it("returns id-category when slug is an empty string", function() {
expect(Discourse.Utilities.categoryUrlId({ id: 123, slug: '' })).toBe("123-category");
});
it("returns id-category without a slug", function() {
expect(Discourse.Utilities.categoryUrlId({ id: 456 })).toBe("456-category");
});
});
describe("emailValid", function() {
it("allows upper case in first part of emails", function() {

View File

@ -0,0 +1,21 @@
/*global waitsFor:true expect:true describe:true beforeEach:true it:true spyOn:true */
describe("Discourse.Category", function() {
describe("slugFor", function() {
it("returns the slug when it exists", function() {
expect(Discourse.Category.slugFor({ slug: 'hello' })).toBe("hello");
});
it("returns id-category when slug is an empty string", function() {
expect(Discourse.Category.slugFor({ id: 123, slug: '' })).toBe("123-category");
});
it("returns id-category without a slug", function() {
expect(Discourse.Category.slugFor({ id: 456 })).toBe("456-category");
});
});
});