Skip to content

Commit a696169

Browse files
committed
Support proc options with zero args
Fixes #40.
1 parent a5f48da commit a696169

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
* Support proc options with zero arguments
2+
3+
Fixes #40.
4+
5+
*Andrew White*
6+
17
* The options `:layout` and `:cache_path` now behave the same when
28
passed a `Symbol`, `Proc` or object that responds to call.
39

lib/action_controller/caching/actions.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,17 @@ def render_to_string(controller, body)
183183

184184
private
185185
def expand_option(controller, option)
186-
if option.is_a?(Proc) || option.respond_to?(:to_proc)
187-
controller.instance_exec(controller, &option)
186+
option = option.to_proc if option.respond_to?(:to_proc)
187+
188+
if option.is_a?(Proc)
189+
case option.arity
190+
when -2, -1, 1
191+
controller.instance_exec(controller, &option)
192+
when 0
193+
controller.instance_exec(&option)
194+
else
195+
raise ArgumentError, "Invalid proc arity of #{option.arity} - proc options should have an arity of 0 or 1"
196+
end
188197
elsif option.respond_to?(:call)
189198
option.call(controller)
190199
else

test/caching_test.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ class ActionCachingTestController < CachingController
4040
caches_action :with_format_and_http_param, cache_path: ->(c) { { key: "value" } }
4141
caches_action :with_symbol_format, cache_path: "http://test.host/action_caching_test/with_symbol_format"
4242
caches_action :not_url_cache_path, cache_path: ->(c) { "#{c.params[:action]}_key" }
43+
caches_action :not_url_cache_path_no_args, cache_path: -> { "#{params[:action]}_key" }
4344
caches_action :layout_false, layout: false
4445
caches_action :with_layout_proc_param, layout: ->(c) { c.params[:layout] != "false" }
46+
caches_action :with_layout_proc_param_no_args, layout: -> { params[:layout] != "false" }
4547
caches_action :record_not_found, :four_oh_four, :simple_runtime_error
4648
caches_action :streaming
4749
caches_action :invalid
@@ -81,6 +83,7 @@ def with_symbol_format
8183
def not_url_cache_path
8284
render plain: "cache_this"
8385
end
86+
alias_method :not_url_cache_path_no_args, :not_url_cache_path
8487

8588
def record_not_found
8689
raise ActiveRecord::RecordNotFound, "oops!"
@@ -101,6 +104,7 @@ def simple_runtime_error
101104
alias_method :symbol_cache_path, :index
102105
alias_method :layout_false, :with_layout
103106
alias_method :with_layout_proc_param, :with_layout
107+
alias_method :with_layout_proc_param_no_args, :with_layout
104108

105109
def expire
106110
expire_action controller: "action_caching_test", action: "index"
@@ -807,6 +811,29 @@ def test_explicit_html_format_is_used_for_fragment_path
807811
assert_not_cached cached_time
808812
end
809813

814+
def test_lambda_arity
815+
draw do
816+
get "/action_caching_test/not_url_cache_path_no_args", to: "action_caching_test#not_url_cache_path_no_args"
817+
get "/action_caching_test/with_layout_proc_param_no_args", to: "action_caching_test#with_layout_proc_param_no_args"
818+
end
819+
820+
get :not_url_cache_path_no_args
821+
assert_response :success
822+
assert !fragment_exist?("test.host/action_caching_test/not_url_cache_path_no_args")
823+
assert fragment_exist?("not_url_cache_path_no_args_key")
824+
825+
get :with_layout_proc_param_no_args, params: { title: "Request 1", layout: "false" }
826+
assert_response :success
827+
cached_time = content_to_cache
828+
assert_equal "<title>Request 1</title>\n#{cached_time}", @response.body
829+
assert_equal cached_time, read_fragment("hostname.com/action_caching_test/with_layout_proc_param_no_args")
830+
831+
get :with_layout_proc_param_no_args, params: { title: "Request 2", layout: "false" }
832+
assert_response :success
833+
assert_equal "<title>Request 2</title>\n#{cached_time}", @response.body
834+
assert_equal cached_time, read_fragment("hostname.com/action_caching_test/with_layout_proc_param_no_args")
835+
end
836+
810837
private
811838
def get_html(*args)
812839
@request.accept = "text/html"

0 commit comments

Comments
 (0)