Skip to content
This repository
Browse code

Merge pull request #10225 from kennyj/fix_bit_string_problem

Fix bit string problem
  • Loading branch information...
commit a80f04c00e3606a85745cdf07f545175c62bd817 2 parents 9f290e6 + 9339005
Rafael Mendonça França rafaelfranca authored
7 activerecord/lib/active_record/connection_adapters/postgresql/cast.rb
@@ -26,6 +26,13 @@ def string_to_time(string)
26 26 end
27 27 end
28 28
  29 + def string_to_bit(value)
  30 + case value
  31 + when /^[01]*$/ then value # Bit-string notation
  32 + when /^[0-9A-F]*$/i then value.hex.to_s(2) # Hexadecimal notation
  33 + end
  34 + end
  35 +
29 36 def hstore_to_string(object)
30 37 if Hash === object
31 38 object.map { |k,v|
14 activerecord/lib/active_record/connection_adapters/postgresql/oid.rb
@@ -18,6 +18,16 @@ def type_cast(value)
18 18 end
19 19 end
20 20
  21 + class Bit < Type
  22 + def type_cast(value)
  23 + if String === value
  24 + ConnectionAdapters::PostgreSQLColumn.string_to_bit value
  25 + else
  26 + value
  27 + end
  28 + end
  29 + end
  30 +
21 31 class Bytea < Type
22 32 def type_cast(value)
23 33 return if value.nil?
@@ -323,14 +333,14 @@ def self.registered_type?(name)
323 333 # FIXME: why are we keeping these types as strings?
324 334 alias_type 'tsvector', 'text'
325 335 alias_type 'interval', 'text'
326   - alias_type 'bit', 'text'
327   - alias_type 'varbit', 'text'
328 336 alias_type 'macaddr', 'text'
329 337 alias_type 'uuid', 'text'
330 338
331 339 register_type 'money', OID::Money.new
332 340 register_type 'bytea', OID::Bytea.new
333 341 register_type 'bool', OID::Boolean.new
  342 + register_type 'bit', OID::Bit.new
  343 + register_type 'varbit', OID::Bit.new
334 344
335 345 register_type 'float4', OID::Float.new
336 346 alias_type 'float8', 'float4'
6 activerecord/test/cases/adapters/postgresql/datatype_test.rb
@@ -545,13 +545,13 @@ def test_update_network_address
545 545
546 546 def test_update_bit_string
547 547 new_bit_string = '11111111'
548   - new_bit_string_varying = '11111110'
  548 + new_bit_string_varying = 'FF'
549 549 assert @first_bit_string.bit_string = new_bit_string
550 550 assert @first_bit_string.bit_string_varying = new_bit_string_varying
551 551 assert @first_bit_string.save
552 552 assert @first_bit_string.reload
553   - assert_equal new_bit_string, @first_bit_string.bit_string
554   - assert_equal new_bit_string_varying, @first_bit_string.bit_string_varying
  553 + assert_equal @first_bit_string.bit_string, new_bit_string
  554 + assert_equal @first_bit_string.bit_string, @first_bit_string.bit_string_varying
555 555 end
556 556
557 557 def test_update_oid

0 comments on commit a80f04c

Please sign in to comment.
Something went wrong with that request. Please try again.