Ticket #5: cache.rb

File cache.rb, 0.8 kB (added by murphy@…, 3 years ago)

Caching for Markaby

Line 
1module ActionController::Caching::Fragments
2  # Called by Markaby::Builder#cache. Imitate cache_erb_template.
3  def cache_markaby_fragment(block, name = {}, options = nil)
4    if perform_caching
5      # check if already cached, otherwise calculate and write
6      cache = read_fragment(name, options) ||
7        write_fragment(name, block.call, options)
8    else # don't cache
9      cache = block.call
10    end
11    options[:builder] << cache
12  end
13end
14
15class Markaby::Builder
16  # Use this method like you would do in a rhtml template.
17  #
18  # See Rails' ActionController::Caching::Fragments for documentation:
19  # http://api.rubyonrails.com/classes/ActionController/Caching/Fragments.html
20  def cache name, &block
21    @helpers.controller.cache_markaby_fragment(block, name, :builder => self)
22  end
23end