@@ -19,27 +19,10 @@ def type_cast(value)
1919 end
2020 end
2121
22- if ActiveRecord ::VERSION ::MINOR >= 2 # >= 4.2
23-
24- class String < Type
25- def type ; :string end
26-
27- def type_cast ( value )
28- return if value . nil?
29-
30- value . to_s
31- end
32- end
33-
34- else
35-
36- # String, Text types do not exist in AR 4.0/4.1
37- # AR reports Identity for them - make it similar
38-
39- class String < Identity
40- def type ; :string end
41- end
42-
22+ # String, Text types do not exist in AR 4.0/4.1
23+ # AR reports Identity for them - make it similar
24+ class String < Identity
25+ def type ; :string end
4326 end
4427
4528 class SpecializedString < OID ::String
@@ -66,57 +49,6 @@ def type_cast(value)
6649 end
6750 end
6851
69- class Bit < ActiveRecord ::Type ::Value
70- def type
71- :bit
72- end
73-
74- def type_cast ( value )
75- if ::String === value
76- case value
77- when /^0x/i
78- value [ 2 ..-1 ] . hex . to_s ( 2 ) # Hexadecimal notation
79- else
80- value # Bit-string notation
81- end
82- else
83- value
84- end
85- end
86-
87- def type_cast_for_database ( value )
88- Data . new ( super ) if value
89- end
90-
91- class Data
92- def initialize ( value )
93- @value = value
94- end
95-
96- def to_s
97- value
98- end
99-
100- def binary?
101- /\A [01]*\Z / === value
102- end
103-
104- def hex?
105- /\A [0-9A-F]*\Z /i === value
106- end
107-
108- protected
109-
110- attr_reader :value
111- end
112- end if ActiveRecord ::VERSION . to_s >= '4.2'
113-
114- class BitVarying < Bit
115- def type
116- :bit_varying
117- end
118- end if ActiveRecord ::VERSION . to_s >= '4.2'
119-
12052 class Bytea < Type
12153 def type ; :binary end
12254
@@ -394,43 +326,6 @@ def accessor
394326 end
395327 end
396328
397- class Jsonb < Json
398- def type
399- :jsonb
400- end
401-
402- def changed_in_place? ( raw_old_value , new_value )
403- # Postgres does not preserve insignificant whitespaces when
404- # roundtripping jsonb columns. This causes some false positives for
405- # the comparison here. Therefore, we need to parse and re-dump the
406- # raw value here to ensure the insignificant whitespaces are
407- # consistent with our encoder's output.
408- raw_old_value = type_cast_for_database ( type_cast_from_database ( raw_old_value ) )
409- super ( raw_old_value , new_value )
410- end
411- end if ActiveRecord ::VERSION . to_s >= '4.2'
412-
413- class Xml < ActiveRecord ::Type ::String
414- def type
415- :xml
416- end
417-
418- def type_cast_for_database ( value )
419- return unless value
420- Data . new ( super )
421- end
422-
423- class Data # :nodoc:
424- def initialize ( value )
425- @value = value
426- end
427-
428- def to_s
429- @value
430- end
431- end
432- end if ActiveRecord ::VERSION . to_s >= '4.2'
433-
434329 class Uuid < Type
435330 def type ; :uuid end
436331 def type_cast ( value )
@@ -546,3 +441,113 @@ def self.registered_type?(name)
546441 end
547442 end
548443end
444+
445+ module ActiveRecord
446+ module ConnectionAdapters
447+ module PostgreSQL
448+ module OID
449+
450+ remove_const ( :String ) if const_defined? ( :String )
451+ class String < Type
452+ def type ; :string end
453+
454+ def type_cast ( value )
455+ return if value . nil?
456+
457+ value . to_s
458+ end
459+ end
460+
461+ remove_const ( :Bit ) if const_defined? ( :Bit )
462+ class Bit < ActiveRecord ::Type ::Value
463+ def type
464+ :bit
465+ end
466+
467+ def type_cast ( value )
468+ if ::String === value
469+ case value
470+ when /^0x/i
471+ value [ 2 ..-1 ] . hex . to_s ( 2 ) # Hexadecimal notation
472+ else
473+ value # Bit-string notation
474+ end
475+ else
476+ value
477+ end
478+ end
479+
480+ def type_cast_for_database ( value )
481+ Data . new ( super ) if value
482+ end
483+
484+ class Data
485+ def initialize ( value )
486+ @value = value
487+ end
488+
489+ def to_s
490+ value
491+ end
492+
493+ def binary?
494+ /\A [01]*\Z / === value
495+ end
496+
497+ def hex?
498+ /\A [0-9A-F]*\Z /i === value
499+ end
500+
501+ protected
502+
503+ attr_reader :value
504+ end
505+ end
506+
507+ class BitVarying < Bit
508+ def type
509+ :bit_varying
510+ end
511+ end
512+
513+ class Jsonb < Json
514+ def type
515+ :jsonb
516+ end
517+
518+ def changed_in_place? ( raw_old_value , new_value )
519+ # Postgres does not preserve insignificant whitespaces when
520+ # roundtripping jsonb columns. This causes some false positives for
521+ # the comparison here. Therefore, we need to parse and re-dump the
522+ # raw value here to ensure the insignificant whitespaces are
523+ # consistent with our encoder's output.
524+ raw_old_value = type_cast_for_database ( type_cast_from_database ( raw_old_value ) )
525+ super ( raw_old_value , new_value )
526+ end
527+ end
528+
529+ class Xml < ActiveRecord ::Type ::String
530+ def type
531+ :xml
532+ end
533+
534+ def type_cast_for_database ( value )
535+ return unless value
536+ Data . new ( super )
537+ end
538+
539+ class Data # :nodoc:
540+ def initialize ( value )
541+ @value = value
542+ end
543+
544+ def to_s
545+ @value
546+ end
547+ end
548+ end
549+
550+ end
551+ end
552+ end
553+ end if ActiveRecord ::VERSION ::MAJOR > 4 || ActiveRecord ::VERSION ::MINOR >= 2 # >= 4.2
0 commit comments