FIX: Handle sub-sub-category paths without an id

This commit is contained in:
Daniel Waterworth
2020-04-22 12:12:33 +01:00
parent ceee855d00
commit c8fed90e4e
3 changed files with 20 additions and 17 deletions

View File

@@ -319,9 +319,7 @@ class ListController < ApplicationController
if id.present? if id.present?
@category = Category.find_by_id(id) @category = Category.find_by_id(id)
elsif slug_path.present? elsif slug_path.present?
if (1..2).include?(slug_path.size) @category = Category.find_by_slug_path(slug_path)
@category = Category.find_by_slug(*slug_path.reverse)
end
# Legacy paths # Legacy paths
if @category.nil? && parts.last =~ /\A\d+-category/ if @category.nil? && parts.last =~ /\A\d+-category/

View File

@@ -359,9 +359,7 @@ class TagsController < ::ApplicationController
if id.present? if id.present?
@filter_on_category = Category.find_by_id(id) @filter_on_category = Category.find_by_id(id)
elsif slug_path.present? elsif slug_path.present?
if (1..2).include?(slug_path.size) @filter_on_category = Category.find_by_slug_path(slug_path)
@filter_on_category = Category.find_by_slug(*slug_path.reverse)
end
# Legacy paths # Legacy paths
if @filter_on_category.nil? && parts.last =~ /\A\d+-category/ if @filter_on_category.nil? && parts.last =~ /\A\d+-category/

View File

@@ -777,22 +777,29 @@ class Category < ActiveRecord::Base
end end
end end
def self.find_by_slug(category_slug, parent_category_slug = nil) def self.find_by_slug_path(slug_path)
return nil if slug_path.empty?
return nil if category_slug.nil? return nil if slug_path.size > SiteSetting.max_category_nesting
if SiteSetting.slug_generation_method == "encoded" if SiteSetting.slug_generation_method == "encoded"
parent_category_slug = CGI.escape(parent_category_slug) unless parent_category_slug.nil? slug_path.map! { |slug| CGI.escape(slug) }
category_slug = CGI.escape(category_slug)
end end
if parent_category_slug query =
parent_category_id = self.where(slug: parent_category_slug, parent_category_id: nil).select(:id) slug_path.inject(nil) do |parent_id, slug|
Category.where(
slug: slug,
parent_category_id: parent_id,
).select(:id)
end
self.where(slug: category_slug, parent_category_id: parent_category_id).first Category.find_by_id(query)
else end
self.where(slug: category_slug, parent_category_id: nil).first
end def self.find_by_slug(category_slug, parent_category_slug = nil)
return nil if category_slug.nil?
find_by_slug_path([parent_category_slug, category_slug].compact)
end end
def subcategory_list_includes_topics? def subcategory_list_includes_topics?