Skip to content

Commit

Permalink
Refactor: move category slug helper to Category model
Browse files Browse the repository at this point in the history
  • Loading branch information
eviltrout committed May 29, 2013
1 parent fe3ac50 commit 3d0587d
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 27 deletions.
10 changes: 1 addition & 9 deletions app/assets/javascripts/discourse/components/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 "";
Expand All @@ -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) + "\" ";
Expand Down
8 changes: 8 additions & 0 deletions app/assets/javascripts/discourse/models/category.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down
16 changes: 0 additions & 16 deletions spec/javascripts/components/utilities_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
21 changes: 21 additions & 0 deletions spec/javascripts/models/category_spec.js
Original file line number Diff line number Diff line change
@@ -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");
});

});

});

0 comments on commit 3d0587d

Please sign in to comment.