Commit db2bd738e9d4a7435580ed2c0ba59bc3a470b3cb
1 parent
45d9194b
Exists in
master
and in
27 other branches
Fix encoding for columns serialized with YAML
See a good explanation about the problem in '4. YAML serialized columns': http://blog.rayapps.com/2013/03/11/7-things-that-can-go-wrong-with-ruby-19-string-encodings/
Showing
1 changed file
with
30 additions
and
0 deletions
Show diff stats
@@ -0,0 +1,30 @@ | @@ -0,0 +1,30 @@ | ||
1 | +class FixYamlEncoding < ActiveRecord::Migration | ||
2 | + def self.up | ||
3 | + fix_encoding(Block, 'settings') | ||
4 | + fix_encoding(Product, 'data') | ||
5 | + fix_encoding(Environment, 'settings') | ||
6 | + fix_encoding(Profile, 'data') | ||
7 | + fix_encoding(ActionTracker::Record, 'params') | ||
8 | + fix_encoding(Article, 'setting') | ||
9 | + end | ||
10 | + | ||
11 | + def self.down | ||
12 | + puts "Warning: cannot restore original encoding" | ||
13 | + end | ||
14 | + | ||
15 | + private | ||
16 | + | ||
17 | + def self.fix_encoding(model, param) | ||
18 | + result = model.find(:all, :conditions => "#{param} LIKE '%!binary%'") | ||
19 | + puts "Fixing #{result.count} rows of #{model} (#{param})" | ||
20 | + result.each {|r| r.update_column(param, deep_fix(r.send(param)).to_yaml)} | ||
21 | + end | ||
22 | + | ||
23 | + def self.deep_fix(hash) | ||
24 | + hash.each do |value| | ||
25 | + value.force_encoding('UTF-8') if value.is_a?(String) && !value.frozen? && value.encoding == Encoding::ASCII_8BIT | ||
26 | + deep_fix(value) if value.respond_to?(:each) | ||
27 | + end | ||
28 | + end | ||
29 | + | ||
30 | +end |