mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-11-23 15:48:43 -05:00
Allow multiple subcategories with the same name
This commit is contained in:
parent
919ae73709
commit
7900c7bd2f
3 changed files with 9 additions and 3 deletions
|
@ -23,7 +23,7 @@ class Category < ActiveRecord::Base
|
||||||
has_many :groups, through: :category_groups
|
has_many :groups, through: :category_groups
|
||||||
|
|
||||||
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: { scope: :parent_category_id }, length: { in: 1..50 }
|
||||||
validate :parent_category_validator
|
validate :parent_category_validator
|
||||||
|
|
||||||
before_validation :ensure_slug
|
before_validation :ensure_slug
|
||||||
|
@ -161,7 +161,7 @@ SQL
|
||||||
t = Topic.new(title: I18n.t("category.topic_prefix", category: name), user: user, pinned_at: Time.now, category_id: id)
|
t = Topic.new(title: I18n.t("category.topic_prefix", category: name), user: user, pinned_at: Time.now, category_id: id)
|
||||||
t.skip_callbacks = true
|
t.skip_callbacks = true
|
||||||
t.auto_close_hours = nil
|
t.auto_close_hours = nil
|
||||||
t.save!
|
t.save!(validate: false)
|
||||||
update_column(:topic_id, t.id)
|
update_column(:topic_id, t.id)
|
||||||
t.posts.create(raw: post_template, user: user)
|
t.posts.create(raw: post_template, user: user)
|
||||||
end
|
end
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
class ChangeCategoryUniqunessContstraint < ActiveRecord::Migration
|
||||||
|
def change
|
||||||
|
remove_index :categories, name: 'index_categories_on_name'
|
||||||
|
add_index :categories, [:parent_category_id, :name], unique: true
|
||||||
|
end
|
||||||
|
end
|
|
@ -9,7 +9,7 @@ describe Category do
|
||||||
|
|
||||||
it 'validates uniqueness of name' do
|
it 'validates uniqueness of name' do
|
||||||
Fabricate(:category)
|
Fabricate(:category)
|
||||||
should validate_uniqueness_of(:name)
|
should validate_uniqueness_of(:name).scoped_to(:parent_category_id)
|
||||||
end
|
end
|
||||||
|
|
||||||
it { should belong_to :topic }
|
it { should belong_to :topic }
|
||||||
|
|
Loading…
Reference in a new issue