Skip to content

Commit

Permalink
Merge branch 'force_utf8_params' into version1
Browse files Browse the repository at this point in the history
  • Loading branch information
jfahrenkrug committed Mar 14, 2012
2 parents 1253fd0 + e3f15d4 commit 667c4dc
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
26 changes: 26 additions & 0 deletions app/controllers/application_controller.rb
Expand Up @@ -11,6 +11,7 @@ class ApplicationController < ActionController::Base
before_filter :set_timezone
before_filter :set_user_locale
before_filter :set_javascripts_and_stylesheets
before_filter :force_utf8_params if RUBY_VERSION =~ /1\.9/
before_filter :set_standard_body_style, :only => [:new, :edit, :update, :create]

attr_accessor :config, :cache
Expand Down Expand Up @@ -84,4 +85,29 @@ def set_standard_body_style
@body_classes.concat(%w(reversed))
end

# When using Radiant with Ruby 1.9, the strings that come in from forms are ASCII-8BIT encoded.
# That causes problems, especially when using special chars and with certain DBs, like DB2
# That's why we force the encoding of the params to UTF-8
# That's what's happening in Rails 3, too: https://github.com/rails/rails/commit/25215d7285db10e2c04d903f251b791342e4dd6a
#
# See http://stackoverflow.com/questions/8268778/rails-2-3-9-encoding-of-query-parameters
# See https://rails.lighthouseapp.com/projects/8994/tickets/4807
# See http://jasoncodes.com/posts/ruby19-rails2-encodings (thanks for the following code, Jason!)
def force_utf8_params
traverse = lambda do |object, block|
if object.kind_of?(Hash)
object.each_value { |o| traverse.call(o, block) }
elsif object.kind_of?(Array)
object.each { |o| traverse.call(o, block) }
else
block.call(object)
end
object
end
force_encoding = lambda do |o|
o.force_encoding(Encoding::UTF_8) if o.respond_to?(:force_encoding)
end
traverse.call(params, force_encoding)
end

end
15 changes: 15 additions & 0 deletions spec/controllers/admin/pages_controller_spec.rb
@@ -1,3 +1,5 @@
#encoding: utf-8

require File.dirname(__FILE__) + '/../../spec_helper'

describe Admin::PagesController do
Expand Down Expand Up @@ -291,6 +293,19 @@
final_updated_at = pages(:home).updated_at
lambda{ next_updated_at <=> final_updated_at }.should be_true
end

it 'should convert form input to UTF-8' do
# When using Radiant with Ruby 1.9, the strings that come in from forms are ASCII-8BIT encoded.
# That causes problems, especially when using special chars and with certain DBs, like DB2
#
# See http://stackoverflow.com/questions/8268778/rails-2-3-9-encoding-of-query-parameters
# See https://rails.lighthouseapp.com/projects/8994/tickets/4807
# See http://jasoncodes.com/posts/ruby19-rails2-encodings

put :update, :id => page_id(:home), :page => {:breadcrumb => 'Homepage', :parts_attributes => {'0' => {:id => pages(:home).parts[0].id, :content => 'Ümlautö'.force_encoding('ASCII-8BIT')}}} and sleep(1)
params['page']['parts_attributes']['0']['content'].encoding.to_s.should == 'UTF-8'
params['page']['parts_attributes']['0']['content'].should == 'Ümlautö'
end
end

it "should initialize meta and buttons_partials in edit action" do
Expand Down

0 comments on commit 667c4dc

Please sign in to comment.