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

View File

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