mirror of
https://github.com/discourse/discourse.git
synced 2025-03-25 09:29:25 +08:00
Merge pull request #1920 from nickborromeo/list-controller
Extract queries to keep logic in the Categories Model
This commit is contained in:
commit
b035c050d2
@ -219,13 +219,12 @@ class ListController < ApplicationController
|
|||||||
|
|
||||||
parent_category_id = nil
|
parent_category_id = nil
|
||||||
if parent_slug_or_id.present?
|
if parent_slug_or_id.present?
|
||||||
parent_category_id = Category.where(slug: parent_slug_or_id).pluck(:id).first ||
|
parent_category_id = Category.query_parent_category(parent_slug_or_id)
|
||||||
Category.where(id: parent_slug_or_id.to_i).pluck(:id).first
|
|
||||||
raise Discourse::NotFound.new if parent_category_id.blank?
|
raise Discourse::NotFound.new if parent_category_id.blank?
|
||||||
end
|
end
|
||||||
|
|
||||||
@category = Category.where(slug: slug_or_id, parent_category_id: parent_category_id).includes(:featured_users).first ||
|
@category = Category.query_category(slug_or_id, parent_category_id)
|
||||||
Category.where(id: slug_or_id.to_i, parent_category_id: parent_category_id).includes(:featured_users).first
|
|
||||||
guardian.ensure_can_see!(@category)
|
guardian.ensure_can_see!(@category)
|
||||||
|
|
||||||
raise Discourse::NotFound.new if @category.blank?
|
raise Discourse::NotFound.new if @category.blank?
|
||||||
|
@ -340,6 +340,16 @@ SQL
|
|||||||
[read_restricted, mapped]
|
[read_restricted, mapped]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.query_parent_category(parent_slug)
|
||||||
|
self.where(slug: parent_slug).pluck(:id).first ||
|
||||||
|
self.where(id: parent_slug.to_i).pluck(:id).first
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.query_category(slug, parent_category_id)
|
||||||
|
self.where(slug: slug, parent_category_id: parent_category_id).includes(:featured_users).first ||
|
||||||
|
self.where(id: slug.to_i, parent_category_id: parent_category_id).includes(:featured_users).first
|
||||||
|
end
|
||||||
|
|
||||||
def uncategorized?
|
def uncategorized?
|
||||||
id == SiteSetting.uncategorized_category_id
|
id == SiteSetting.uncategorized_category_id
|
||||||
end
|
end
|
||||||
|
@ -372,6 +372,21 @@ describe Category do
|
|||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe ".query_parent_category" do
|
||||||
|
it "should return the parent category id given a parent slug" do
|
||||||
|
parent_category.name = "Amazing Category"
|
||||||
|
parent_category.id.should == Category.query_parent_category(parent_category.slug)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe ".query_category" do
|
||||||
|
it "should return the category" do
|
||||||
|
category = Fabricate(:category, name: "Amazing Category", parent_category_id: parent_category.id, user: user)
|
||||||
|
parent_category.name = "Amazing Parent Category"
|
||||||
|
category.should == Category.query_category(category.slug, parent_category.id)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user