From dbf3666964b8f2a536476cd598492cb3f7aaab47 Mon Sep 17 00:00:00 2001 From: Ary Djmal 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