diff --git a/app/models/category.rb b/app/models/category.rb index 00f4e1803b7..bb4a8d52192 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -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 diff --git a/spec/models/category_spec.rb b/spec/models/category_spec.rb index 68f037bf572..b2da4115047 100644 --- a/spec/models/category_spec.rb +++ b/spec/models/category_spec.rb @@ -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}")