Skip to content

Commit a5f48da

Browse files
committed
Unify the behavior of :layout and :cache_path options
Previously the :layout option would only work with a Proc or an object that responded to call and wasn't executed in the context of the controller instance whereas :cache_path would also accept a Symbol and was executed in the context of the controller.
1 parent 5d31989 commit a5f48da

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
* The options `:layout` and `:cache_path` now behave the same when
2+
passed a `Symbol`, `Proc` or object that responds to call.
3+
4+
*Andrew White*
5+
16
* Respect `Accept` header when caching actions
27

38
Fixes #18.

lib/action_controller/caching/actions.rb

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -152,16 +152,8 @@ def initialize(options, &block)
152152
end
153153

154154
def around(controller)
155-
cache_layout = @cache_layout.respond_to?(:call) ? @cache_layout.call(controller) : @cache_layout
156-
157-
path_options = if @cache_path.is_a?(Proc) || @cache_path.respond_to?(:to_proc)
158-
controller.instance_exec(controller, &@cache_path)
159-
elsif @cache_path.respond_to?(:call)
160-
@cache_path.call(controller)
161-
else
162-
@cache_path
163-
end
164-
155+
cache_layout = expand_option(controller, @cache_layout)
156+
path_options = expand_option(controller, @cache_path)
165157
cache_path = ActionCachePath.new(controller, path_options || {})
166158

167159
body = controller.read_fragment(cache_path.path, @store_options)
@@ -188,6 +180,17 @@ def render_to_string(controller, body)
188180
controller.render_to_string(html: body, layout: true)
189181
end
190182
end
183+
184+
private
185+
def expand_option(controller, option)
186+
if option.is_a?(Proc) || option.respond_to?(:to_proc)
187+
controller.instance_exec(controller, &option)
188+
elsif option.respond_to?(:call)
189+
option.call(controller)
190+
else
191+
option
192+
end
193+
end
191194
end
192195

193196
class ActionCachePath

0 commit comments

Comments
 (0)