Method: CacheReplace#render_cached

Defined in:
lib/cache_replace.rb

#render_cached(static_partial, options = {}) ⇒ Object

Supports 4 options:

  1. Single partial to replace

render_cached "container", replace: "inner"
  1. Array of partials to replace

render_cached "container", replace: ["inner"]
  1. Map of keys to replace with values

render_cached "container", replace: {special_link: special_link(object)}
  1. Yield to a hash of keys to replace with values

render_cached "container" do
  {special_link: special_link(object)}
end


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/cache_replace.rb', line 24

def render_cached(static_partial, options={})
  replace = options.delete(:replace)
  fragment = render(static_partial, options)

  case replace
  when Hash
    replace_from_hash fragment, replace
  when NilClass
    replace_from_hash fragment, yield
  else
    replace = *replace
    replace.each do |key|
      fragment.sub! cache_replace_key(key), render(key, options)
    end
  end

  raw fragment
end