Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/curly/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,16 @@ def compile_collection(block)
items.each_with_index do |item, index|
options = options.merge("#{name}" => item, "#{counter}" => index + 1)
presenter = ::#{item_presenter_class}.new(self, options)

Curly::TemplateHandler.cache_if_key_is_not_nil(self, presenter) do
RUBY

@presenter_classes.push(item_presenter_class)
compile(block.nodes)
@presenter_classes.pop

output <<-RUBY
end
end
options = options_stack.pop
presenter = presenters.pop
Expand Down Expand Up @@ -151,14 +154,17 @@ def compile_context(block)
options = options.merge("#{name}" => item)
buffer = ActiveSupport::SafeBuffer.new
presenter = ::#{item_presenter_class}.new(self, options)

Curly::TemplateHandler.cache_if_key_is_not_nil(self, presenter) do
RUBY

@presenter_classes.push(item_presenter_class)
compile(block.nodes)
@presenter_classes.pop

output <<-RUBY
buffer
buffer
end
end
buffer = buffers.pop
presenter = presenters.pop
Expand Down
10 changes: 8 additions & 2 deletions lib/curly/template_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,16 @@ def cache_if_key_is_not_nil(context, presenter)

cache_options = presenter.cache_options || {}
cache_options[:expires_in] ||= presenter.cache_duration
controller = context.controller
fragment_name = context.cache_fragment_name([key, presenter_key].compact, cache_options)
fragment = controller.read_fragment(fragment_name, cache_options)

context.cache([key, presenter_key].compact, cache_options) do
yield
if fragment.nil?
fragment = yield
controller.write_fragment(fragment_name, fragment, cache_options)
end

fragment
else
yield
end
Expand Down
31 changes: 31 additions & 0 deletions spec/compiler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,37 @@ def square?(width:, height:)
render("{{#square? width=2 height=2}}yeah!{{/square?}}").should == "yeah!"
end

it "caches context blocks" do
define_presenter do
presents :author

def author(&block)
block.call(@author)
end
end

define_presenter "AuthorPresenter" do
presents :author

def name
@author.name
end

def cache_key
"static"
end
end

author = double("author", name: "john")
template = "{{@author}}{{name}}{{/author}}"

expect(render(template, locals: { author: author })).to eq "john"

author.stub(:name) { "jane" }

expect(render(template, locals: { author: author })).to eq "john"
end

it "gives an error on incomplete blocks" do
expect do
render("{{#hello?}}")
Expand Down
7 changes: 7 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

RSpec.configure do |config|
config.infer_spec_type_from_file_location!

config.before do
Rails.cache.clear
end
end

module CompilationSupport
Expand Down Expand Up @@ -35,6 +39,9 @@ def render(source, options = {}, &block)
template = ActionView::Template.new(source, identifier, handler, details)
view = ActionView::Base.new
view.lookup_context.stub(:find_template) { source }
view.stub(:view_cache_dependencies) { [] }
view.controller = ActionView::TestCase::TestController.new
view.controller.perform_caching = true

begin
template.render(view, options.fetch(:locals, {}), &block)
Expand Down