Permalink
Browse files

Treat blank UUID values as nil

  • Loading branch information...
1 parent 79f06a9 commit 1f432c54658cf54608a6e37b70b8dc8e40521502 @lavrovdv lavrovdv committed with senny Mar 27, 2014
View
8 activerecord/CHANGELOG.md
@@ -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
9 activerecord/lib/active_record/connection_adapters/postgresql/oid.rb
@@ -330,6 +330,13 @@ def accessor
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 @@ def self.registered_type?(name)
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
5 activerecord/test/cases/adapters/postgresql/uuid_test.rb
@@ -50,6 +50,11 @@ def test_data_type_of_uuid_types
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}",

0 comments on commit 1f432c5

Please sign in to comment.