Module: Catche::ViewHelpers

Defined in:
lib/catche/view_helpers.rb

Instance Method Summary collapse

Instance Method Details

#catche(model_or_resource, options = {}, &block) ⇒ Object Also known as: catches_fragment

Caches a fragment See ActionView ‘cache` for more information

<% catche @project do %>
  <%= @project.title %>
<% end %>

<% catche @projects, :model => Project do %>
  <% @projects.each do |project| %>
    <%= project.title %>
  <% end %>
<% end %>


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/catche/view_helpers.rb', line 16

def catche(model_or_resource, options={}, &block)
  tags    = []
  name    = Catche::Tag.join 'fragment', model_or_resource.hash
  key     = ActiveSupport::Cache.expand_cache_key name, :views
  object  = options[:model] || model_or_resource

  if object.respond_to?(:catche_tag)
    if object.respond_to?(:new)
      tags = Catche::Tag::Collect.collection(controller, object)[:set]
    else
      tags = Catche::Tag::Collect.resource(object)[:set]
    end
  end

  Catche::Tag.tag_view! key, *tags if tags.any?

  cache name, options, &block
end