lib/curly/template_handler.rb in curly-templates-0.11.0 vs lib/curly/template_handler.rb in curly-templates-0.12.0

- old
+ new

@@ -17,10 +17,30 @@ instrument(template) do compile(template) end end + def cache_if_key_is_not_nil(context, presenter) + if key = presenter.cache_key + if presenter.class.respond_to?(:cache_key) + presenter_key = presenter.class.cache_key + else + presenter_key = nil + end + + options = { + expires_in: presenter.cache_duration + } + + context.cache([key, presenter_key].compact, options) do + yield + end + else + yield + end + end + private def compile(template) # Template is empty, so there's no need to initialize a presenter. return %("") if template.source.empty? @@ -38,37 +58,19 @@ else options = local_assigns end presenter = #{presenter_class}.new(self, options.with_indifferent_access) - - view_function = lambda do - #{source} - end - presenter.setup! - if key = presenter.cache_key - @output_buffer = ActiveSupport::SafeBuffer.new + @output_buffer = output_buffer || ActiveSupport::SafeBuffer.new - if #{presenter_class}.respond_to?(:cache_key) - presenter_key = #{presenter_class}.cache_key - else - presenter_key = nil - end - - options = { - expires_in: presenter.cache_duration - } - - cache([key, presenter_key].compact, options) do - safe_concat(view_function.call) - end - - @output_buffer - else - view_function.call.html_safe + Curly::TemplateHandler.cache_if_key_is_not_nil(self, presenter) do + result = #{source} + safe_concat(result) end + + @output_buffer RUBY end def instrument(template, &block) payload = { path: template.virtual_path }