Module: CacheRocket
- Includes:
- Key
- Defined in:
- lib/cache_rocket.rb,
lib/cache_rocket/key.rb,
lib/cache_rocket/version.rb,
lib/cache_rocket/fragment.rb
Defined Under Namespace
Modules: Key Classes: Fragment
Constant Summary collapse
- ERROR_MISSING_KEY_OR_BLOCK =
"You must either pass a `replace` key or a block to render_cached."
- ERROR_MISSING_KEY =
"You must pass a `replace` key to cache_replace."
- VERSION =
"0.7.0"
Instance Method Summary collapse
-
#cache_replace(name = {}, options = {}, &block) ⇒ Object
This method works like Rails’ CacheHelper#cache, plus it replaces content in the replace hash.
-
#render_cached(partial, options = {}) ⇒ Object
Supports 5 options:.
Methods included from Key
Instance Method Details
#cache_replace(name = {}, options = {}, &block) ⇒ Object
This method works like Rails’ CacheHelper#cache, plus it replaces content in the replace hash. It must have a ‘replace` option.
-
cache_replace([“key1”, “key2”], replace: { name: “x” }) do .htmls
= cache_replace_key :name
github.com/rails/rails/blob/master/actionview/lib/action_view/helpers/cache_helper.rb
69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/cache_rocket.rb', line 69 def cache_replace(name = {}, = {}, &block) replace_hash = .delete(:replace) raise(ArgumentError, ERROR_MISSING_KEY) unless replace_hash = .slice(:skip_digest, :virtual_path) safe_concat \ Fragment.new( fragment_for(cache_fragment_name(name, ), , &block) ).replace(replace_hash).to_s.html_safe nil end |
#render_cached(partial, options = {}) ⇒ Object
Supports 5 options:
-
Single partial to replace. “inner” is the key name and “_inner.*” is the partial file name.
render_cached "container", replace: "inner"
-
Array of partials to replace
render_cached "container", replace: ["inner"]
-
Map of keys to replace with values
render_cached "container", replace: {key_name: a_helper_method(object)}
-
Yield to a hash of keys to replace with values
render_cached "container" do
{key_name: a_helper_method(object)}
end
-
Render a collection with Procs for replace values.
render_cached "partial", collection: objects, replace: { key_name: ->(object){a_method(object)} }
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/cache_rocket.rb', line 39 def render_cached(partial, = {}) replace_hash = .delete(:replace) collection = .delete(:collection) fragment = Fragment.new(render(partial, )) case replace_hash when Hash fragment.replace replace_hash, collection when NilClass raise(ArgumentError, ERROR_MISSING_KEY_OR_BLOCK) unless block_given? fragment.replace yield, collection else [*replace_hash].each do |key| fragment.gsub! cache_replace_key(key), render(key, ) end end fragment.to_s.html_safe end |