Skip to content

Commit e955b32

Browse files
committed
Support symbols in :cache_path option
This tiny commit allows to use symbols in the `:cache_path` option via the standard behavior of `Symbol#to_proc`. The controller's instance method pointed by the symbol should be in a `protected` scope.
1 parent 30ae10c commit e955b32

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

lib/action_controller/caching/actions.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def initialize(options, &block)
154154
def around(controller)
155155
cache_layout = @cache_layout.respond_to?(:call) ? @cache_layout.call(controller) : @cache_layout
156156

157-
path_options = if @cache_path.is_a?(Proc)
157+
path_options = if @cache_path.is_a?(Proc) || @cache_path.respond_to?(:to_proc)
158158
controller.instance_exec(controller, &@cache_path)
159159
elsif @cache_path.respond_to?(:call)
160160
@cache_path.call(controller)

test/caching_test.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class ActionCachingTestController < CachingController
3535
caches_action :show, cache_path: 'http://test.host/custom/show'
3636
caches_action :edit, cache_path: Proc.new { |c| c.params[:id] ? "http://test.host/#{c.params[:id]};edit" : 'http://test.host/edit' }
3737
caches_action :custom_cache_path, cache_path: CachePath.new
38+
caches_action :symbol_cache_path, cache_path: :cache_path_protected_method
3839
caches_action :with_layout
3940
caches_action :with_format_and_http_param, cache_path: Proc.new { |c| { key: 'value' } }
4041
caches_action :with_symbol_format, cache_path: 'http://test.host/action_caching_test/with_symbol_format'
@@ -92,6 +93,7 @@ def simple_runtime_error
9293
alias_method :edit, :index
9394
alias_method :destroy, :index
9495
alias_method :custom_cache_path, :index
96+
alias_method :symbol_cache_path, :index
9597
alias_method :layout_false, :with_layout
9698
alias_method :with_layout_proc_param, :with_layout
9799

@@ -121,6 +123,12 @@ def invalid
121123
format.json{ render json: @cache_this }
122124
end
123125
end
126+
127+
protected
128+
129+
def cache_path_protected_method
130+
['controller', params[:id]].compact.join('-')
131+
end
124132
end
125133

126134
class MockTime < Time
@@ -327,6 +335,16 @@ def test_action_cache_with_custom_cache_path_with_custom_object
327335
assert fragment_exist?('controller-1')
328336
end
329337

338+
def test_action_cache_with_symbol_cache_path
339+
get :symbol_cache_path
340+
assert_response :success
341+
assert fragment_exist?('controller')
342+
343+
get :symbol_cache_path, id: 1
344+
assert_response :success
345+
assert fragment_exist?('controller-1')
346+
end
347+
330348
def test_cache_expiration
331349
get :index
332350
assert_response :success

0 commit comments

Comments
 (0)