Skip to content

Commit e571c6a

Browse files
authored
Merge pull request #22 from estum/cache-path-symbol
Support symbols in :cache_path option
2 parents fc82145 + e955b32 commit e571c6a

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'
@@ -97,6 +98,7 @@ def simple_runtime_error
9798
alias_method :edit, :index
9899
alias_method :destroy, :index
99100
alias_method :custom_cache_path, :index
101+
alias_method :symbol_cache_path, :index
100102
alias_method :layout_false, :with_layout
101103
alias_method :with_layout_proc_param, :with_layout
102104

@@ -126,6 +128,12 @@ def invalid
126128
format.json{ render json: @cache_this }
127129
end
128130
end
131+
132+
protected
133+
134+
def cache_path_protected_method
135+
['controller', params[:id]].compact.join('-')
136+
end
129137
end
130138

131139
class MockTime < Time
@@ -339,6 +347,16 @@ def test_action_cache_with_custom_cache_path_with_custom_object
339347
assert fragment_exist?('controller-1')
340348
end
341349

350+
def test_action_cache_with_symbol_cache_path
351+
get :symbol_cache_path
352+
assert_response :success
353+
assert fragment_exist?('controller')
354+
355+
get :symbol_cache_path, id: 1
356+
assert_response :success
357+
assert fragment_exist?('controller-1')
358+
end
359+
342360
def test_cache_expiration
343361
get :index
344362
assert_response :success

0 commit comments

Comments
 (0)