Add UUID support to validates_uniqueness_of matcher

This commit is contained in:
Morton Jonuschat 2013-08-06 09:26:02 +02:00 committed by Elliot Winkler
parent 18b789a3cf
commit 5b951e7628
3 changed files with 45 additions and 0 deletions

View File

@ -1,5 +1,7 @@
# HEAD
* Add support for PostgreSQL UUID columns to `validates_uniqueness_of` (#334).
* Fix `validates_numericality_of` so that `is_equal_to` submatcher works
correctly (#326).

View File

@ -197,6 +197,8 @@ module Shoulda # :nodoc:
'0'
elsif column.type == :datetime
DateTime.now
elsif column.type == :uuid
SecureRandom.uuid
else
0
end

View File

@ -168,6 +168,47 @@ describe Shoulda::Matchers::ActiveModel::ValidateUniquenessOfMatcher do
end
end
context 'when the scoped attribute is a uuid' do
it 'accepts' do
validating_scoped_uniqueness([:scope1], :uuid, :scope1 => SecureRandom.uuid).
should matcher.scoped_to(:scope1)
end
context 'with an existing record that conflicts with scope.next' do
it 'accepts' do
validating_scoped_uniqueness_with_conflicting_next(:scope1, :uuid, :scope1 => SecureRandom.uuid).
should matcher.scoped_to(:scope1)
end
end
context 'with a nil value' do
it 'accepts' do
validating_scoped_uniqueness([:scope1], :uuid, :scope1 => nil).
should matcher.scoped_to(:scope1)
end
end
context 'when too narrow of a scope is specified' do
it 'rejects' do
record = validating_scoped_uniqueness([:scope1, :scope2], :uuid,
:scope1 => SecureRandom.uuid,
:scope2 => SecureRandom.uuid
)
record.should_not matcher.scoped_to(:scope1, :scope2, :other)
end
end
context 'when too broad of a scope is specified' do
it 'rejects' do
record = validating_scoped_uniqueness([:scope1, :scope2], :uuid,
:scope1 => SecureRandom.uuid,
:scope2 => SecureRandom.uuid
)
record.should_not matcher.scoped_to(:scope1)
end
end
end
def create_existing_record(attributes = {})
@existing ||= create_record(attributes)
end