mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
Database structure to support sub categories
This commit is contained in:
parent
bf06014a16
commit
c814fc16a3
@ -33,6 +33,7 @@ class Category < ActiveRecord::Base
|
|||||||
validates :user_id, presence: true
|
validates :user_id, presence: true
|
||||||
validates :name, presence: true, uniqueness: true, length: { in: 1..50 }
|
validates :name, presence: true, uniqueness: true, length: { in: 1..50 }
|
||||||
validate :uncategorized_validator
|
validate :uncategorized_validator
|
||||||
|
validate :parent_category_validator
|
||||||
|
|
||||||
before_validation :ensure_slug
|
before_validation :ensure_slug
|
||||||
after_save :invalidate_site_cache
|
after_save :invalidate_site_cache
|
||||||
@ -43,6 +44,7 @@ class Category < ActiveRecord::Base
|
|||||||
after_destroy :publish_categories_list
|
after_destroy :publish_categories_list
|
||||||
|
|
||||||
has_one :category_search_data
|
has_one :category_search_data
|
||||||
|
belongs_to :parent_category, class_name: 'Category'
|
||||||
|
|
||||||
scope :latest, ->{ order('topic_count desc') }
|
scope :latest, ->{ order('topic_count desc') }
|
||||||
|
|
||||||
@ -188,6 +190,15 @@ SQL
|
|||||||
errors.add(:slug, I18n.t(:is_reserved)) if slug == SiteSetting.uncategorized_name
|
errors.add(:slug, I18n.t(:is_reserved)) if slug == SiteSetting.uncategorized_name
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def parent_category_validator
|
||||||
|
if parent_category_id
|
||||||
|
errors.add(:parent_category_id, "You can't link a category to itself") if parent_category_id == id
|
||||||
|
|
||||||
|
grandfather_id = Category.where(id: parent_category_id).pluck(:parent_category_id).first
|
||||||
|
errors.add(:parent_category_id, "You can't have more than one level of subcategory") if grandfather_id
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def group_names=(names)
|
def group_names=(names)
|
||||||
# this line bothers me, destroying in AR can not seem to be queued, thinking of extending it
|
# this line bothers me, destroying in AR can not seem to be queued, thinking of extending it
|
||||||
category_groups.destroy_all unless new_record?
|
category_groups.destroy_all unless new_record?
|
||||||
|
@ -0,0 +1,6 @@
|
|||||||
|
class AddParentCategoryIdToCategories < ActiveRecord::Migration
|
||||||
|
def change
|
||||||
|
add_column :categories, :parent_category_id, :integer
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
@ -18,7 +18,7 @@ describe Category do
|
|||||||
it { should have_many :topics }
|
it { should have_many :topics }
|
||||||
it { should have_many :category_featured_topics }
|
it { should have_many :category_featured_topics }
|
||||||
it { should have_many :featured_topics }
|
it { should have_many :featured_topics }
|
||||||
|
it { should belong_to :parent_category}
|
||||||
|
|
||||||
describe "resolve_permissions" do
|
describe "resolve_permissions" do
|
||||||
it "can determine read_restricted" do
|
it "can determine read_restricted" do
|
||||||
@ -314,4 +314,31 @@ describe Category do
|
|||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
describe "parent categories" do
|
||||||
|
let(:user) { Fabricate(:user) }
|
||||||
|
let(:parent_category) { Fabricate(:category, user: user) }
|
||||||
|
|
||||||
|
it "can be associated with a parent category" do
|
||||||
|
sub_category = Fabricate.build(:category, parent_category_id: parent_category.id, user: user)
|
||||||
|
sub_category.should be_valid
|
||||||
|
sub_category.parent_category.should == parent_category
|
||||||
|
end
|
||||||
|
|
||||||
|
it "cannot associate a category with itself" do
|
||||||
|
category = Fabricate(:category, user: user)
|
||||||
|
category.parent_category_id = category.id
|
||||||
|
category.should_not be_valid
|
||||||
|
end
|
||||||
|
|
||||||
|
it "cannot have a category two levels deep" do
|
||||||
|
sub_category = Fabricate(:category, parent_category_id: parent_category.id, user: user)
|
||||||
|
nested_sub_category = Fabricate.build(:category, parent_category_id: sub_category.id, user: user)
|
||||||
|
nested_sub_category.should_not be_valid
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user