FIX: More encoded slug fixes (#8191)

* FIX: Do not encode the URL twice

Now that we encode slugs in the server we don't need this anymore.

Reverts fe5na33

* FIX: More places do deal with encoded slugs

* the param is a string now, not a hash

* FIX: Handle the nil slug on /categories

* DEV: Add seeded? method to identity default categories

* DEV: Use SiteSetting to keep track of seeded categories
This commit is contained in:
Rafael dos Santos Silva
2019-10-16 17:08:43 -03:00
committed by GitHub
parent 7a0c06691c
commit 6e9c8fe854
8 changed files with 56 additions and 7 deletions

View File

@@ -612,7 +612,8 @@ class Category < ActiveRecord::Base
end
def self.query_category(slug_or_id, parent_category_id)
self.where(slug: slug_or_id, parent_category_id: parent_category_id).first ||
encoded_slug_or_id = CGI.escape(slug_or_id) if SiteSetting.slug_generation_method == 'encoded'
self.where(slug: (encoded_slug_or_id || slug_or_id), parent_category_id: parent_category_id).first ||
self.where(id: slug_or_id.to_i, parent_category_id: parent_category_id).first
end
@@ -629,6 +630,15 @@ class Category < ActiveRecord::Base
id == SiteSetting.uncategorized_category_id
end
def seeded?
[
SiteSetting.lounge_category_id,
SiteSetting.meta_category_id,
SiteSetting.staff_category_id,
SiteSetting.uncategorized_category_id,
].include? id
end
@@url_cache = DistributedCache.new('category_url')
def clear_url_cache
@@ -708,6 +718,14 @@ class Category < ActiveRecord::Base
end
def self.find_by_slug(category_slug, parent_category_slug = nil)
return nil if category_slug.nil?
if SiteSetting.slug_generation_method == "encoded"
parent_category_slug = CGI.escape(parent_category_slug) unless parent_category_slug.nil?
category_slug = CGI.escape(category_slug)
end
if parent_category_slug
parent_category_id = self.where(slug: parent_category_slug, parent_category_id: nil).select(:id)

View File

@@ -988,6 +988,15 @@ class Topic < ActiveRecord::Base
slug
end
def self.find_by_slug(slug)
if SiteSetting.slug_generation_method != "encoded"
Topic.find_by(slug: slug.downcase)
else
encoded_slug = CGI.escape(slug)
Topic.find_by(slug: encoded_slug)
end
end
def title=(t)
slug = Slug.for(t.to_s)
write_attribute(:slug, slug)