mirror of
https://github.com/discourse/discourse.git
synced 2025-02-18 01:05:00 +00:00
FIX: Category.find_by_slug
find_by_slug should ensure that the parent actually exists when its looking for a parent.
This commit is contained in:
parent
e83c2488a2
commit
5f5b232cde
@ -206,6 +206,9 @@ class CategoriesController < ApplicationController
|
|||||||
def find_by_slug
|
def find_by_slug
|
||||||
params.require(:category_slug)
|
params.require(:category_slug)
|
||||||
@category = Category.find_by_slug(params[:category_slug], params[:parent_category_slug])
|
@category = Category.find_by_slug(params[:category_slug], params[:parent_category_slug])
|
||||||
|
|
||||||
|
raise Discourse::NotFound unless @category.present?
|
||||||
|
|
||||||
if !guardian.can_see?(@category)
|
if !guardian.can_see?(@category)
|
||||||
if SiteSetting.detailed_404 && group = @category.access_category_via_group
|
if SiteSetting.detailed_404 && group = @category.access_category_via_group
|
||||||
raise Discourse::InvalidAccess.new(
|
raise Discourse::InvalidAccess.new(
|
||||||
|
@ -709,7 +709,8 @@ class Category < ActiveRecord::Base
|
|||||||
|
|
||||||
def self.find_by_slug(category_slug, parent_category_slug = nil)
|
def self.find_by_slug(category_slug, parent_category_slug = nil)
|
||||||
if parent_category_slug
|
if parent_category_slug
|
||||||
parent_category_id = self.where(slug: parent_category_slug, parent_category_id: nil).pluck(:id).first
|
parent_category_id = self.where(slug: parent_category_slug, parent_category_id: nil).select(:id)
|
||||||
|
|
||||||
self.where(slug: category_slug, parent_category_id: parent_category_id).first
|
self.where(slug: category_slug, parent_category_id: parent_category_id).first
|
||||||
else
|
else
|
||||||
self.where(slug: category_slug, parent_category_id: nil).first
|
self.where(slug: category_slug, parent_category_id: nil).first
|
||||||
|
@ -742,12 +742,36 @@ describe Category do
|
|||||||
end
|
end
|
||||||
|
|
||||||
describe "find_by_slug" do
|
describe "find_by_slug" do
|
||||||
it "finds with category and sub category" do
|
fab!(:category) do
|
||||||
category = Fabricate(:category_with_definition, slug: 'awesome-category')
|
Fabricate(:category_with_definition, slug: 'awesome-category')
|
||||||
sub_category = Fabricate(:category_with_definition, parent_category_id: category.id, slug: 'awesome-sub-category')
|
end
|
||||||
|
|
||||||
|
fab!(:subcategory) do
|
||||||
|
Fabricate(
|
||||||
|
:category_with_definition,
|
||||||
|
parent_category_id: category.id,
|
||||||
|
slug: 'awesome-sub-category'
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "finds a category that exists" do
|
||||||
expect(Category.find_by_slug('awesome-category')).to eq(category)
|
expect(Category.find_by_slug('awesome-category')).to eq(category)
|
||||||
expect(Category.find_by_slug('awesome-sub-category', 'awesome-category')).to eq(sub_category)
|
end
|
||||||
|
|
||||||
|
it "finds a subcategory that exists" do
|
||||||
|
expect(Category.find_by_slug('awesome-sub-category', 'awesome-category')).to eq(subcategory)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "produces nil if the parent doesn't exist" do
|
||||||
|
expect(Category.find_by_slug('awesome-sub-category', 'no-such-category')).to eq(nil)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "produces nil if the parent doesn't exist and the requested category is a root category" do
|
||||||
|
expect(Category.find_by_slug('awesome-category', 'no-such-category')).to eq(nil)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "produces nil if the subcategory doesn't exist" do
|
||||||
|
expect(Category.find_by_slug('no-such-category', 'awesome-category')).to eq(nil)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user