From 27e518b20d153fd9e63ee868cf4940261d34d66c Mon Sep 17 00:00:00 2001 From: Louise Crow Date: Tue, 5 May 2015 14:21:57 +0100 Subject: [PATCH 1/2] Make the namespace for memcached dependent on ruby version. Items cached in 1.8 can't safely be retrieved in 1.9 --- config/application.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/config/application.rb b/config/application.rb index 472077f069..46c4eb93b5 100644 --- a/config/application.rb +++ b/config/application.rb @@ -60,7 +60,8 @@ class Application < Rails::Application config.time_zone = ::AlaveteliConfiguration::time_zone # Set the cache to use a memcached backend - config.cache_store = :mem_cache_store, { :namespace => AlaveteliConfiguration::domain } + config.cache_store = :mem_cache_store, + { :namespace => "#{AlaveteliConfiguration::domain}_#{RUBY_VERSION}" } config.action_dispatch.rack_cache = nil config.after_initialize do |app| From f75b1de52432018c19d9fce04b1127ab114df0ea Mon Sep 17 00:00:00 2001 From: Louise Crow Date: Thu, 7 May 2015 11:41:12 +0100 Subject: [PATCH 2/2] Force the encoding of values returned from YAML. This is a workaround for an issue where YAML in ruby 1.8 tags UTF-8 heavy strings as binary, resulting in them being retrieved under 1.9 as ASCII-8BIT which can't be concatenated with UTF-8. Described as "Deep deep YAML oddness" in https://www.zendesk.com/blog/upgrade-the-road-to-1-9/. --- app/models/info_request_event.rb | 8 +++++++- app/models/post_redirect.rb | 6 +++++- spec/models/info_request_event_spec.rb | 6 ++++++ spec/models/post_redirect_spec.rb | 10 +++++++++- 4 files changed, 27 insertions(+), 3 deletions(-) diff --git a/app/models/info_request_event.rb b/app/models/info_request_event.rb index 635ba8f583..0ee82d30cc 100644 --- a/app/models/info_request_event.rb +++ b/app/models/info_request_event.rb @@ -278,9 +278,15 @@ def params=(params) end self.params_yaml = params.to_yaml end + def params - YAML.load(self.params_yaml) + param_hash = YAML.load(params_yaml) + param_hash.each do |key, value| + param_hash[key] = value.force_encoding('UTF-8') if value.respond_to?(:force_encoding) + end + param_hash end + def params_yaml_as_html ret = '' # split out parameters into old/new diffs, and other ones diff --git a/app/models/post_redirect.rb b/app/models/post_redirect.rb index 8049349d06..59160381cf 100644 --- a/app/models/post_redirect.rb +++ b/app/models/post_redirect.rb @@ -71,7 +71,11 @@ def reason_params=(reason_params) end def reason_params - YAML.load(reason_params_yaml) + param_hash = YAML.load(reason_params_yaml) + param_hash.each do |key, value| + param_hash[key] = value.force_encoding('UTF-8') if value.respond_to?(:force_encoding) + end + param_hash end # Extract just local path part, without domain or # diff --git a/spec/models/info_request_event_spec.rb b/spec/models/info_request_event_spec.rb index 53c83bd466..1299dfb63a 100644 --- a/spec/models/info_request_event_spec.rb +++ b/spec/models/info_request_event_spec.rb @@ -30,6 +30,12 @@ ire.params.should == example_params end + it "should restore UTF8-heavy params stored under ruby 1.8 as UTF-8" do + ire = InfoRequestEvent.new + utf8_params = "--- \n:foo: !binary |\n 0KLQvtCz0LDRiCDR\n" + ire.params_yaml = utf8_params + ire.params[:foo].encoding.to_s.should == 'UTF-8' if ire.params[:foo].respond_to?(:encoding) + end end describe 'when deciding if it is indexed by search' do diff --git a/spec/models/post_redirect_spec.rb b/spec/models/post_redirect_spec.rb index 73740e9142..70b221f100 100644 --- a/spec/models/post_redirect_spec.rb +++ b/spec/models/post_redirect_spec.rb @@ -65,11 +65,19 @@ end it "should convert reason parameters into YAML and back successfully" do - pr = PostRedirect.new + pr = PostRedirect.new example_reason_params = { :foo => 'this is stuff', :bar => 83, :humbug => "yikes!!!" } pr.reason_params = example_reason_params pr.reason_params_yaml.should == example_reason_params.to_yaml pr.reason_params.should == example_reason_params end + + it "should restore UTF8-heavy params stored under ruby 1.8 as UTF-8" do + pr = PostRedirect.new + utf8_params = "--- \n:foo: !binary |\n 0KLQvtCz0LDRiCDR\n" + pr.reason_params_yaml = utf8_params + puts pr.reason_params + pr.reason_params[:foo].encoding.to_s.should == 'UTF-8' if pr.reason_params[:foo].respond_to?(:encoding) + end end