Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save AquaGeek/971654 to your computer and use it in GitHub Desktop.
Save AquaGeek/971654 to your computer and use it in GitHub Desktop.
Rails Lighthouse ticket #3677
From dbf3666964b8f2a536476cd598492cb3f7aaab47 Mon Sep 17 00:00:00 2001
From: Ary Djmal <[email protected]>
Date: Mon, 11 Oct 2010 15:14:25 -0400
Subject: [PATCH] Added support to action-cache json requests wrapped in a callback (jsonp)
---
.../lib/action_controller/caching/actions.rb | 7 ++++++
actionpack/test/controller/caching_test.rb | 22 +++++++++++++++++++-
2 files changed, 28 insertions(+), 1 deletions(-)
diff --git a/actionpack/lib/action_controller/caching/actions.rb b/actionpack/lib/action_controller/caching/actions.rb
index d69d96b..a926e6d 100644
--- a/actionpack/lib/action_controller/caching/actions.rb
+++ b/actionpack/lib/action_controller/caching/actions.rb
@@ -133,11 +133,18 @@ module ActionController #:nodoc:
body = controller._save_fragment(cache_path.path, @store_options)
end
+ body = reset_json_callback(body, controller.params[:callback]) if controller.request.format.json?
body = controller.render_to_string(:text => body, :layout => true) unless @cache_layout
controller.response_body = body
controller.content_type = Mime[cache_path.extension || :html]
end
+
+ private
+ def reset_json_callback(body, callback)
+ body = body.sub(/^\w+\((.*)\)$/, '\1')
+ callback.present? ? "#{callback}(#{body})" : body
+ end
end
class ActionCachePath
diff --git a/actionpack/test/controller/caching_test.rb b/actionpack/test/controller/caching_test.rb
index 914ae56..da0d350 100644
--- a/actionpack/test/controller/caching_test.rb
+++ b/actionpack/test/controller/caching_test.rb
@@ -163,7 +163,7 @@ class ActionCachingTestController < CachingController
caches_action :index, :redirected, :forbidden, :if => Proc.new { |c| !c.request.format.json? }, :expires_in => 1.hour
caches_action :show, :cache_path => 'http://test.host/custom/show'
caches_action :edit, :cache_path => Proc.new { |c| c.params[:id] ? "http://test.host/#{c.params[:id]};edit" : "http://test.host/edit" }
- caches_action :with_layout
+ caches_action :with_json_callback, :with_layout
caches_action :layout_false, :layout => false
caches_action :record_not_found, :four_oh_four, :simple_runtime_error
@@ -183,6 +183,11 @@ class ActionCachingTestController < CachingController
response.status = "403 Forbidden"
end
+ def with_json_callback
+ @cache_this = MockTime.now.to_f.to_s
+ render :json => @cache_this, :callback => params[:callback]
+ end
+
def with_layout
@cache_this = MockTime.now.to_f.to_s
@title = nil
@@ -362,6 +367,21 @@ class ActionCacheTest < ActionController::TestCase
assert fragment_exist?('test.host/1;edit')
end
+ def test_action_cache_with_json_callback
+ @request.env['HTTP_ACCEPT'] = 'application/json'
+ get :with_json_callback, :callback => 'jsonp123456789'
+ cached_time = content_to_cache
+ assert_equal "jsonp123456789(#{cached_time})", @response.body
+ assert fragment_exist?('hostname.com/action_caching_test/with_json_callback')
+
+ get :with_json_callback, :callback => 'jsonp987654321'
+ assert_equal "jsonp987654321(#{cached_time})", @response.body
+ assert_equal "jsonp123456789(#{cached_time})", read_fragment('hostname.com/action_caching_test/with_json_callback')
+
+ get :with_json_callback
+ assert_equal cached_time, @response.body
+ end
+
def test_cache_expiration
get :index
assert_response :success
--
1.7.1
From 4aeee9fd1dd8e58ce92ede0fc812f80b583b421f Mon Sep 17 00:00:00 2001
From: Ary Djmal <[email protected]>
Date: Sat, 9 Jan 2010 17:32:01 -0500
Subject: [PATCH] Added support for action-cache json requests wrapped in a callback (jsonp)
---
.../lib/action_controller/caching/actions.rb | 7 +++++
actionpack/test/controller/caching_test.rb | 25 +++++++++++++++++++-
2 files changed, 31 insertions(+), 1 deletions(-)
diff --git a/actionpack/lib/action_controller/caching/actions.rb b/actionpack/lib/action_controller/caching/actions.rb
index 35111a4..6eca128 100644
--- a/actionpack/lib/action_controller/caching/actions.rb
+++ b/actionpack/lib/action_controller/caching/actions.rb
@@ -126,12 +126,19 @@ module ActionController #:nodoc:
cache_path = ActionCachePath.new(controller, path_options || {})
if cache = controller.read_fragment(cache_path.path, @store_options)
+ cache = reset_json_callback(cache, controller.params[:callback]) if controller.request.format.json?
controller._render_cache_fragment(cache, cache_path.extension, @layout == false)
else
yield
controller._save_fragment(cache_path.path, @layout == false, @store_options)
end
end
+
+ private
+ def reset_json_callback(cache, callback)
+ cache = cache.sub(/^\w+\((.*)\)$/, '\1')
+ callback.present? ? "#{callback}(#{cache})" : cache
+ end
end
class ActionCachePath
diff --git a/actionpack/test/controller/caching_test.rb b/actionpack/test/controller/caching_test.rb
index 5a8dc0c..5848403 100644
--- a/actionpack/test/controller/caching_test.rb
+++ b/actionpack/test/controller/caching_test.rb
@@ -156,7 +156,7 @@ class ActionCachingTestController < ActionController::Base
caches_action :index, :redirected, :forbidden, :if => Proc.new { |c| !c.request.format.json? }, :expires_in => 1.hour
caches_action :show, :cache_path => 'http://test.host/custom/show'
caches_action :edit, :cache_path => Proc.new { |c| c.params[:id] ? "http://test.host/#{c.params[:id]};edit" : "http://test.host/edit" }
- caches_action :with_layout
+ caches_action :with_json_callback, :with_layout
caches_action :layout_false, :layout => false
caches_action :record_not_found, :four_oh_four, :simple_runtime_error
@@ -176,6 +176,11 @@ class ActionCachingTestController < ActionController::Base
response.status = "403 Forbidden"
end
+ def with_json_callback
+ @cache_this = MockTime.now.to_f.to_s
+ render :json => @cache_this, :callback => params[:callback]
+ end
+
def with_layout
@cache_this = MockTime.now.to_f.to_s
render :text => @cache_this, :layout => true
@@ -340,6 +345,24 @@ class ActionCacheTest < ActionController::TestCase
assert fragment_exist?('test.host/1;edit')
end
+ def test_action_cache_with_json_callback
+ old_use_accept_header = ActionController::Base.use_accept_header
+ ActionController::Base.use_accept_header = true
+ @request.env['HTTP_ACCEPT'] = 'application/json'
+ get :with_json_callback, :callback => 'jsonp123456789'
+ cached_time = content_to_cache
+ assert_equal "jsonp123456789(#{cached_time})", @response.body
+ assert fragment_exist?('hostname.com/action_caching_test/with_json_callback')
+
+ get :with_json_callback, :callback => 'jsonp987654321'
+ assert_equal "jsonp987654321(#{cached_time})", @response.body
+ assert_equal "jsonp123456789(#{cached_time})", read_fragment('hostname.com/action_caching_test/with_json_callback')
+
+ get :with_json_callback
+ assert_equal cached_time, @response.body
+ ActionController::Base.use_accept_header = old_use_accept_header
+ end
+
def test_cache_expiration
get :index
cached_time = content_to_cache
--
1.6.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment