Allow multiple subcategories with the same name

This commit is contained in:
Robin Ward 2014-05-26 15:33:51 -04:00
parent 919ae73709
commit 7900c7bd2f
3 changed files with 9 additions and 3 deletions

View File

@ -23,7 +23,7 @@ class Category < ActiveRecord::Base
has_many :groups, through: :category_groups
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
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.skip_callbacks = true
t.auto_close_hours = nil
t.save!
t.save!(validate: false)
update_column(:topic_id, t.id)
t.posts.create(raw: post_template, user: user)
end

View File

@ -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

View File

@ -9,7 +9,7 @@ describe Category do
it 'validates uniqueness of name' do
Fabricate(:category)
should validate_uniqueness_of(:name)
should validate_uniqueness_of(:name).scoped_to(:parent_category_id)
end
it { should belong_to :topic }