FIX: don't allow same category name with different case

This commit is contained in:
Neil Lalonde 2014-08-11 16:55:26 -04:00
parent d1f6c31382
commit e40e9351f6
3 changed files with 12 additions and 2 deletions

View File

@ -23,7 +23,10 @@ class Category < ActiveRecord::Base
has_many :groups, through: :category_groups
validates :user_id, presence: true
validates :name, presence: true, uniqueness: { scope: :parent_category_id }, length: { in: 1..50 }
validates :name, if: Proc.new { |c| c.new_record? || c.name_changed? },
presence: true,
uniqueness: { scope: :parent_category_id, case_sensitive: false },
length: { in: 1..50 }
validate :parent_category_validator
before_validation :ensure_slug

View File

@ -48,7 +48,7 @@ class Topic < ActiveRecord::Base
rate_limit :limit_topics_per_day
rate_limit :limit_private_messages_per_day
validates :title, :if => Proc.new { |t| t.title_changed? },
validates :title, :if => Proc.new { |t| t.new_record? || t.title_changed? },
:presence => true,
:topic_title_length => true,
:quality_title => { :unless => :private_message? },

View File

@ -12,6 +12,13 @@ describe Category do
should validate_uniqueness_of(:name).scoped_to(:parent_category_id)
end
it 'validates uniqueness in case insensitive way' do
Fabricate(:category, name: "Cats")
c = Fabricate.build(:category, name: "cats")
c.should_not be_valid
c.errors[:name].should be_present
end
it { should belong_to :topic }
it { should belong_to :user }