A platform for community discussion. Free, open, simple.
DEV: Deprecate Category#url_with_id in favor of Category#url (#9972)
Dan Ungureanud21a08c284b4
diff --git a/app/controllers/category_hashtags_controller.rb b/app/controllers/category_hashtags_controller.rbindex b08d7f23ae9..b5a971fa805 100644--- a/app/controllers/category_hashtags_controller.rb+++ b/app/controllers/category_hashtags_controller.rb@@ -9,7 +9,7 @@ class CategoryHashtagsController < ApplicationControllerids = category_slugs.map { |category_slug| Category.query_from_hashtag_slug(category_slug).try(:id) }valid_categories = Category.secured(guardian).where(id: ids).map do |category|- { slug: category.hashtag_slug, url: category.url_with_id }+ { slug: category.hashtag_slug, url: category.url }end.compactrender json: { valid: valid_categories }diff --git a/app/models/category.rb b/app/models/category.rbindex d6375eb7144..cb51a03cc48 100644--- a/app/models/category.rb+++ b/app/models/category.rb@@ -721,11 +721,13 @@ class Category < ActiveRecord::Baseenddef url- @@url_cache[self.id] ||= "#{Discourse.base_uri}/c/#{slug_path.join('/')}"+ @@url_cache[self.id] ||= "#{Discourse.base_uri}/c/#{slug_path.join('/')}/#{self.id}"enddef url_with_id- self.parent_category ? "#{url}/#{self.id}" : "#{Discourse.base_uri}/c/#{self.slug}/#{self.id}"+ Discourse.deprecate("Category#url_with_id is deprecated. Use `Category#url` instead.", output_in_test: true)++ urlend# If the name changes, try and update the category definition topic too if it's an exact match@@ -739,9 +741,10 @@ class Category < ActiveRecord::Basedef create_category_permalinkold_slug = saved_changes.transform_values(&:first)["slug"]+url = +"#{Discourse.base_uri}/c"url << "/#{parent_category.slug_path.join('/')}" if parent_category_id- url << "/#{old_slug}"+ url << "/#{old_slug}/#{id}"url = Permalink.normalize_url(url)if Permalink.where(url: url).exists?diff --git a/app/models/permalink.rb b/app/models/permalink.rbindex 9e1e2d1a4e2..ea317839e51 100644--- a/app/models/permalink.rb+++ b/app/models/permalink.rb@@ -80,7 +80,7 @@ class Permalink < ActiveRecord::Basereturn external_url if external_urlreturn "#{Discourse::base_uri}#{post.url}" if postreturn topic.relative_url if topic- return "#{category.url}/#{category.id}" if category+ return category.url if categoryreturn tag.full_url if tagnilenddiff --git a/app/models/topic_list.rb b/app/models/topic_list.rbindex d51c0f153f3..52b0ee00708 100644--- a/app/models/topic_list.rb+++ b/app/models/topic_list.rb@@ -67,7 +67,7 @@ class TopicListdef preload_keyif @category- "topic_list_#{@category.url.sub(/^\//, '')}/#{@category.id}/l/#{@filter}"+ "topic_list_#{@category.url.sub(/^\//, '')}/l/#{@filter}"else"topic_list_#{@filter}"enddiff --git a/lib/pretty_text/helpers.rb b/lib/pretty_text/helpers.rbindex d207e01eed6..9633a4e8eaf 100644--- a/lib/pretty_text/helpers.rb+++ b/lib/pretty_text/helpers.rb@@ -41,7 +41,7 @@ module PrettyTextdef category_hashtag_lookup(category_slug)if category = Category.query_from_hashtag_slug(category_slug)- [category.url_with_id, category_slug]+ [category.url, category_slug]elsenilend@@ -106,7 +106,7 @@ module PrettyTextis_tag = text =~ /#{tag_postfix}$/if !is_tag && category = Category.query_from_hashtag_slug(text)- [category.url_with_id, text]+ [category.url, text]elsif (!is_tag && tag = Tag.find_by(name: text)) ||(is_tag && tag = Tag.find_by(name: text.gsub!("#{tag_postfix}", '')))["#{Discourse.base_url}/tag/#{tag.name}", text]diff --git a/spec/components/pretty_text_spec.rb b/spec/components/pretty_text_spec.rbindex 49a3ac90baa..823fb8900c2 100644--- a/spec/components/pretty_text_spec.rb+++ b/spec/components/pretty_text_spec.rb@@ -1106,9 +1106,9 @@ describe PrettyText do["<span class=\"hashtag\">#unknown::tag</span>",- "<a class=\"hashtag\" href=\"#{category2.url_with_id}\">#<span>known</span></a>",+ "<a class=\"hashtag\" href=\"#{category2.url}\">#<span>known</span></a>","<a class=\"hashtag\" href=\"http://test.localhost/tag/known\">#<span>known</span></a>",- "<a class=\"hashtag\" href=\"#{category.url_with_id}\">#<span>testing</span></a>"+ "<a class=\"hashtag\" href=\"#{category.url}\">#<span>testing</span></a>"].each do |element|expect(cooked).to include(element)diff --git a/spec/models/category_spec.rb b/spec/models/category_spec.rbindex 7b550a9c1d3..f122673c0a9 100644--- a/spec/models/category_spec.rb+++ b/spec/models/category_spec.rb@@ -430,7 +430,7 @@ describe Category doendit "reuses existing permalink when category slug is changed" do- permalink = Permalink.create!(url: "c/#{@category.slug}", category_id: 42)+ permalink = Permalink.create!(url: "c/#{@category.slug}/#{@category.id}", category_id: 42)expect { @category.update(slug: 'new-slug') }.to_not change { Permalink.count }expect(permalink.reload.category_id).to eq(@category.id)@@ -695,47 +695,20 @@ describe Category dodescribe "for normal categories" doit "builds a url" do- expect(category.url).to eq("/c/root")+ expect(category.url).to eq("/c/root/#{category.id}")endenddescribe "for subcategories" doit "builds a url" do- expect(sub_category.url).to eq("/c/root/child")+ expect(sub_category.url).to eq("/c/root/child/#{sub_category.id}")endenddescribe "for sub-sub-categories" doit "builds a url" doexpect(sub_sub_category.url)- .to eq("/c/root/child/child-of-child")- end- end- end-- describe "#url_with_id" do- 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) 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}")+ .to eq("/c/root/child/child-of-child/#{sub_sub_category.id}")endendenddiff --git a/spec/models/permalink_spec.rb b/spec/models/permalink_spec.rbindex 869d38fba5f..709768e5d03 100644--- a/spec/models/permalink_spec.rb+++ b/spec/models/permalink_spec.rb@@ -63,7 +63,7 @@ describe Permalink doit "returns a category url when category_id is set" dopermalink.category_id = category.id- expect(target_url).to eq("#{category.url}/#{category.id}")+ expect(target_url).to eq("#{category.url}")endit "returns nil when category_id is set but category is not found" dodiff --git a/spec/requests/categories_controller_spec.rb b/spec/requests/categories_controller_spec.rbindex a4825fb101a..d920723b145 100644--- a/spec/requests/categories_controller_spec.rb+++ b/spec/requests/categories_controller_spec.rb@@ -13,7 +13,7 @@ describe CategoriesController doget '/categories', headers: { 'HTTP_USER_AGENT' => 'Googlebot' }html = Nokogiri::HTML5(response.body)expect(html.css('body.crawler')).to be_present- expect(html.css("a[href=\"/forum/c/#{category.slug}\"]")).to be_present+ expect(html.css("a[href=\"/forum/c/#{category.slug}/#{category.id}\"]")).to be_presentendit "properly preloads topic list" dodiff --git a/spec/requests/category_hashtags_controller_spec.rb b/spec/requests/category_hashtags_controller_spec.rbindex f2f62a82ea1..7676ba56423 100644--- a/spec/requests/category_hashtags_controller_spec.rb+++ b/spec/requests/category_hashtags_controller_spec.rb@@ -18,7 +18,7 @@ describe CategoryHashtagsController doexpect(response.status).to eq(200)expect(response.parsed_body).to eq(- "valid" => [{ "slug" => category.hashtag_slug, "url" => category.url_with_id }]+ "valid" => [{ "slug" => category.hashtag_slug, "url" => category.url }])end@@ -45,7 +45,7 @@ describe CategoryHashtagsController doexpect(response.status).to eq(200)expect(response.parsed_body).to eq(- "valid" => [{ "slug" => private_category.hashtag_slug, "url" => private_category.url_with_id }]+ "valid" => [{ "slug" => private_category.hashtag_slug, "url" => private_category.url }])endend