FIX: Include entire slug path in permalinks

This is a temporary fix since these URLs should contain the id as well.
This commit is contained in:
Daniel Waterworth 2020-03-19 14:56:28 +00:00
parent 145c90419d
commit 1b24a7b993
2 changed files with 52 additions and 25 deletions

View File

@ -682,10 +682,9 @@ class Category < ActiveRecord::Base
def url def url
url = @@url_cache[self.id] url = @@url_cache[self.id]
unless url unless url
url = +"#{Discourse.base_uri}/c" url = "#{Discourse.base_uri}/c/#{slug_path.join('/')}"
url << "/#{parent_category.slug_for_url}" if parent_category_id
url << "/#{slug_for_url}" @@url_cache[self.id] = url
@@url_cache[self.id] = -url
end end
url url
@ -708,7 +707,7 @@ class Category < ActiveRecord::Base
def create_category_permalink def create_category_permalink
old_slug = saved_changes.transform_values(&:first)["slug"] old_slug = saved_changes.transform_values(&:first)["slug"]
url = +"#{Discourse.base_uri}/c" url = +"#{Discourse.base_uri}/c"
url << "/#{parent_category.slug}" if parent_category_id url << "/#{parent_category.slug_path.join('/')}" if parent_category_id
url << "/#{old_slug}" url << "/#{old_slug}"
url = Permalink.normalize_url(url) url = Permalink.normalize_url(url)
@ -720,11 +719,7 @@ class Category < ActiveRecord::Base
end end
def delete_category_permalink def delete_category_permalink
if self.parent_category permalink = Permalink.find_by_url("c/#{slug_path.join('/')}")
permalink = Permalink.find_by_url("c/#{self.parent_category.slug}/#{slug}")
else
permalink = Permalink.find_by_url("c/#{slug}")
end
permalink.destroy if permalink permalink.destroy if permalink
end end

View File

@ -661,36 +661,68 @@ describe Category do
end end
describe "#url" do describe "#url" do
it "builds a url for normal categories" do before_all do
category = Fabricate(:category_with_definition, name: "cats") SiteSetting.max_category_nesting = 3
expect(category.url).to eq "/c/cats" end
fab!(:category) { Fabricate(:category, name: "root") }
fab!(:sub_category) do
Fabricate(
:category,
name: "child",
parent_category_id: category.id,
)
end
fab!(:sub_sub_category) do
Fabricate(
:category,
name: "child_of_child",
parent_category_id: sub_category.id,
)
end
describe "for normal categories" do
it "builds a url" do
expect(category.url).to eq("/c/root")
end
end end
describe "for subcategories" do describe "for subcategories" do
it "includes the parent category" do it "builds a url" do
parent_category = Fabricate(:category_with_definition, name: "parent") expect(sub_category.url).to eq("/c/root/child")
end
end
subcategory = describe "for sub-sub-categories" do
Fabricate( it "builds a url" do
:category_with_definition, expect(sub_sub_category.url)
name: "child", .to eq("/c/root/child/child-of-child")
parent_category_id: parent_category.id
)
expect(subcategory.url).to eq "/c/parent/child"
end end
end end
end end
describe "#url_with_id" do describe "#url_with_id" do
fab!(:category) { Fabricate(:category_with_definition, name: 'cats') } fab!(:category) do
Fabricate(
:category_with_definition,
name: 'cats',
)
end
it "includes the id in the URL" do it "includes the id in the URL" do
expect(category.url_with_id).to eq("/c/cats/#{category.id}") expect(category.url_with_id).to eq("/c/cats/#{category.id}")
end end
context "child category" do context "child category" do
fab!(:child_category) { Fabricate(:category_with_definition, parent_category_id: category.id, name: 'dogs') } fab!(:child_category) do
Fabricate(
:category,
parent_category_id: category.id,
name: 'dogs',
)
end
it "includes the id in the URL" do it "includes the id in the URL" do
expect(child_category.url_with_id).to eq("/c/cats/dogs/#{child_category.id}") expect(child_category.url_with_id).to eq("/c/cats/dogs/#{child_category.id}")