Add UUID support to validates_uniqueness_of matcher
This commit is contained in:
parent
18b789a3cf
commit
5b951e7628
2
NEWS.md
2
NEWS.md
|
@ -1,5 +1,7 @@
|
||||||
# HEAD
|
# HEAD
|
||||||
|
|
||||||
|
* Add support for PostgreSQL UUID columns to `validates_uniqueness_of` (#334).
|
||||||
|
|
||||||
* Fix `validates_numericality_of` so that `is_equal_to` submatcher works
|
* Fix `validates_numericality_of` so that `is_equal_to` submatcher works
|
||||||
correctly (#326).
|
correctly (#326).
|
||||||
|
|
||||||
|
|
|
@ -197,6 +197,8 @@ module Shoulda # :nodoc:
|
||||||
'0'
|
'0'
|
||||||
elsif column.type == :datetime
|
elsif column.type == :datetime
|
||||||
DateTime.now
|
DateTime.now
|
||||||
|
elsif column.type == :uuid
|
||||||
|
SecureRandom.uuid
|
||||||
else
|
else
|
||||||
0
|
0
|
||||||
end
|
end
|
||||||
|
|
|
@ -168,6 +168,47 @@ describe Shoulda::Matchers::ActiveModel::ValidateUniquenessOfMatcher do
|
||||||
end
|
end
|
||||||
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 = {})
|
def create_existing_record(attributes = {})
|
||||||
@existing ||= create_record(attributes)
|
@existing ||= create_record(attributes)
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue