diff --git activerecord lib active_record connection_adapters abstract

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
index 520f3c8..7bba2a5 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
@@ -67,8 +67,8 @@ module ActiveRecord
def type_cast(value)
return nil if value.nil?
case type
- when :string then value
- when :text then value
+ when :string then self.class.transcode_text(value)
+ when :text then self.class.transocde_text(value)
when :integer then value.to_i rescue value ? 1 : 0
when :float then value.to_f
when :decimal then self.class.value_to_decimal(value)
@@ -84,8 +84,8 @@ module ActiveRecord
def type_cast_code(var_name)
case type
- when :string then nil
- when :text then nil
+ when :string then "#{self.class.name}.transcode_text(#{var_name})"
+ when :text then "#{self.class.name}.transcode_text(#{var_name})"
when :integer then "(#{var_name}.to_i rescue #{var_name} ? 1 : 0)"
when :float then "#{var_name}.to_f"
when :decimal then "#{self.class.name}.value_to_decimal(#{var_name})"
@@ -166,6 +166,12 @@ module ActiveRecord
end
end
+ # Transcodes +:string+ or +:text+ to a correct encoding.
+ def transcode_text(value)
+ condition = defined?(Encoding.default_internal) && !Encoding.default_internal.nil?
+ condition ? value.force_encoding(Encoding.default_internal) : value
+ end
+
protected
# '0.123456' -> 123456
# '1.123456' -> 123456