1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Treat blank UUID values as nil

This commit is contained in:
Dmitry Lavrov 2014-03-27 15:29:20 +03:00 committed by Yves Senn
parent 79f06a9134
commit 1f432c5465
3 changed files with 21 additions and 1 deletions

View file

@ -1,3 +1,11 @@
* Treat blank UUID values as `nil`.
Example:
Sample.new(uuid_field: '') #=> <Sample id: nil, uuid_field: nil>
*Dmitry Lavrov*
* Enable support for materialized views on PostgreSQL >= 9.3.
*Dave Lee*

View file

@ -330,6 +330,13 @@ This is not reliable and will be removed in the future.
end
end
class Uuid < Type
def type; :uuid end
def type_cast(value)
value.presence
end
end
class TypeMap
def initialize
@mapping = {}
@ -418,10 +425,10 @@ This is not reliable and will be removed in the future.
register_type 'json', OID::Json.new
register_type 'cidr', OID::Cidr.new
register_type 'inet', OID::Inet.new
register_type 'uuid', OID::Uuid.new
register_type 'xml', SpecializedString.new(:xml)
register_type 'tsvector', SpecializedString.new(:tsvector)
register_type 'macaddr', SpecializedString.new(:macaddr)
register_type 'uuid', SpecializedString.new(:uuid)
register_type 'citext', SpecializedString.new(:citext)
register_type 'ltree', SpecializedString.new(:ltree)

View file

@ -50,6 +50,11 @@ class PostgresqlUUIDTest < ActiveRecord::TestCase
assert_not column.array
end
def test_treat_blank_uuid_as_nil
UUIDType.create! guid: ''
assert_equal(nil, UUIDType.last.guid)
end
def test_uuid_formats
["A0EEBC99-9C0B-4EF8-BB6D-6BB9BD380A11",
"{a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11}",