A platform for community discussion. Free, open, simple.
FIX: Make category slug validation less strict (#8915)
Dan Ungureanuecaf2c2f4eed
diff --git a/app/assets/javascripts/discourse/models/category.js.es6 b/app/assets/javascripts/discourse/models/category.js.es6index f57cc392b57..bbbe590d0b6 100644--- a/app/assets/javascripts/discourse/models/category.js.es6+++ b/app/assets/javascripts/discourse/models/category.js.es6@@ -274,8 +274,12 @@ Category.reopenClass({result = Category.slugFor(parentCategory) + separator;}- const slug = get(category, "slug");- return !slug || slug.trim().length === 0 ? `${result}-` : result + slug;+ const id = get(category, "id"),+ slug = get(category, "slug");++ return !slug || slug.trim().length === 0+ ? `${result}${id}-category`+ : result + slug;},list() {@@ -360,7 +364,7 @@ Category.reopenClass({if (!category &&parts.length > 0 &&- parts[parts.length - 1].match(/^\d+-/)+ parts[parts.length - 1].match(/^\d+-category/)) {const id = parseInt(parts.pop(), 10);diff --git a/app/controllers/list_controller.rb b/app/controllers/list_controller.rbindex 7ee85c7579b..498f3ed86e1 100644--- a/app/controllers/list_controller.rb+++ b/app/controllers/list_controller.rb@@ -324,7 +324,7 @@ class ListController < ApplicationControllerend# Legacy paths- if @category.nil? && parts.last =~ /\A\d+-/+ if @category.nil? && parts.last =~ /\A\d+-category/@category = Category.find_by_id(parts.last.to_i)endenddiff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rbindex 5cc8b8121cb..25d8a15aaec 100644--- a/app/controllers/tags_controller.rb+++ b/app/controllers/tags_controller.rb@@ -364,7 +364,7 @@ class TagsController < ::ApplicationControllerend# Legacy paths- if @filter_on_category.nil? && parts.last =~ /\A\d+-/+ if @filter_on_category.nil? && parts.last =~ /\A\d+-category/@filter_on_category = Category.find_by_id(parts.last.to_i)endenddiff --git a/app/models/category.rb b/app/models/category.rbindex 025512d0fbc..83a842f1892 100644--- a/app/models/category.rb+++ b/app/models/category.rb@@ -316,7 +316,7 @@ class Category < ActiveRecord::Baseend# only allow to use category itself id.- match_id = /^(\d+)-/.match(self.slug)+ match_id = /^(\d+)-category/.match(self.slug)if match_id.present?errors.add(:slug, :invalid) if new_record? || (match_id[1] != self.id.to_s)enddiff --git a/spec/requests/list_controller_spec.rb b/spec/requests/list_controller_spec.rbindex 5172969289f..9702231805f 100644--- a/spec/requests/list_controller_spec.rb+++ b/spec/requests/list_controller_spec.rb@@ -350,7 +350,7 @@ RSpec.describe ListController docontext 'with a link that includes an id' doit "succeeds" do- get "/c/#{category.id}-#{category.slug}/l/latest"+ get "/c/#{category.id}-category/l/latest"expect(response.status).to eq(200)endenddiff --git a/test/javascripts/models/category-test.js.es6 b/test/javascripts/models/category-test.js.es6index d31c7f84f6d..cb58081417e 100644--- a/test/javascripts/models/category-test.js.es6+++ b/test/javascripts/models/category-test.js.es6@@ -17,13 +17,13 @@ QUnit.test("slugFor", assert => {);slugFor(store.createRecord("category", { id: 123, slug: "" }),- "-",- "It returns - for empty slugs"+ "123-category",+ "It returns id-category for empty strings");slugFor(store.createRecord("category", { id: 456 }),- "-",- "It returns - for undefined slugs"+ "456-category",+ "It returns id-category for undefined slugs");slugFor(store.createRecord("category", { slug: "熱帶風暴畫眉" }),@@ -46,14 +46,14 @@ QUnit.test("slugFor", assert => {slugFor(store.createRecord("category", { id: 555, parentCategory: parentCategory }),- "darth/-",+ "darth/555-category","it uses the parent slug before the child and then uses id");parentCategory.set("slug", null);slugFor(store.createRecord("category", { id: 555, parentCategory: parentCategory }),- "-/-",+ "345-category/555-category","it uses the parent before the child and uses ids for both");});