mirror of
https://github.com/codeninjasllc/discourse.git
synced 2025-02-17 12:11:16 -05:00
Refactor: move category slug helper to Category model
This commit is contained in:
parent
fe3ac50aae
commit
3d0587d8ce
6 changed files with 32 additions and 27 deletions
|
@ -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
|
// Create a badge like category link
|
||||||
categoryLink: function(category) {
|
categoryLink: function(category) {
|
||||||
if (!category) return "";
|
if (!category) return "";
|
||||||
|
@ -47,7 +39,7 @@ Discourse.Utilities = {
|
||||||
var description = Em.get(category, 'description');
|
var description = Em.get(category, 'description');
|
||||||
|
|
||||||
// Build the HTML link
|
// 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
|
// Add description if we have it
|
||||||
if (description) result += "title=\"" + Handlebars.Utils.escapeExpression(description) + "\" ";
|
if (description) result += "title=\"" + Handlebars.Utils.escapeExpression(description) + "\" ";
|
||||||
|
|
|
@ -82,6 +82,14 @@ Discourse.Category.reopenClass({
|
||||||
return this.uncategorized;
|
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() {
|
list: function() {
|
||||||
return Discourse.Site.instance().get('categories');
|
return Discourse.Site.instance().get('categories');
|
||||||
},
|
},
|
||||||
|
|
|
@ -36,7 +36,7 @@ Discourse.ListCategoryRoute = Discourse.FilteredListRoute.extend({
|
||||||
}
|
}
|
||||||
|
|
||||||
var listController = this.controllerFor('list');
|
var listController = this.controllerFor('list');
|
||||||
var urlId = Discourse.Utilities.categoryUrlId(category);
|
var urlId = Discourse.Category.slugFor(category);
|
||||||
listController.set('filterMode', "category/" + urlId);
|
listController.set('filterMode', "category/" + urlId);
|
||||||
|
|
||||||
var router = this;
|
var router = this;
|
||||||
|
|
|
@ -152,7 +152,7 @@ Discourse.EditCategoryView = Discourse.ModalBodyView.extend({
|
||||||
this.get('category').save().then(function(result) {
|
this.get('category').save().then(function(result) {
|
||||||
// success
|
// success
|
||||||
$('#discourse-modal').modal('hide');
|
$('#discourse-modal').modal('hide');
|
||||||
Discourse.URL.redirectTo("/category/" + Discourse.Utilities.categoryUrlId(result.category));
|
Discourse.URL.redirectTo("/category/" + Discourse.Category.slugFor(result.category));
|
||||||
}, function(errors) {
|
}, function(errors) {
|
||||||
// errors
|
// errors
|
||||||
if(errors.length === 0) errors.push(Em.String.i18n("category.creation_error"));
|
if(errors.length === 0) errors.push(Em.String.i18n("category.creation_error"));
|
||||||
|
|
|
@ -2,22 +2,6 @@
|
||||||
|
|
||||||
describe("Discourse.Utilities", function() {
|
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() {
|
describe("emailValid", function() {
|
||||||
|
|
||||||
it("allows upper case in first part of emails", function() {
|
it("allows upper case in first part of emails", function() {
|
||||||
|
|
21
spec/javascripts/models/category_spec.js
Normal file
21
spec/javascripts/models/category_spec.js
Normal 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");
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
Loading…
Reference in a new issue