Caching templates is now an option

This commit is contained in:
ccocchi 2012-04-02 16:22:43 +02:00
parent 3e9c903077
commit e30e5dd66a
4 changed files with 42 additions and 4 deletions

View File

@ -13,4 +13,14 @@ require 'rabl-fast-json/handler'
require 'rabl-fast-json/railtie'
module RablFastJson
mattr_accessor :cache_templates
@@cache_templates = true
def self.configure
yield self
end
def self.cache_templates?
ActionController::Base.perform_caching && @@cache_templates
end
end

View File

@ -21,13 +21,16 @@ module RablFastJson
end
def get_compiled_template(path, source)
# @cached_templates[path] ||=
Compiler.new.compile_source(source)
if path && RablFastJson.cache_templates?
@cached_templates[path] ||= Compiler.new.compile_source(source)
else
Compiler.new.compile_source(source)
end
end
def get(path)
template = @cached_templates[path]
return template if !template.nil?
return template unless template.nil?
t = @view_renderer.lookup_context.find_template(path, [], false)
get_compiled_template(path, t.source)
end

View File

@ -0,0 +1,23 @@
require 'test_helper'
class CacheTemplatesTest < ActiveSupport::TestCase
setup do
RablFastJson::Library.reset_instance
@library = RablFastJson::Library.instance
end
test "cache templates if perform_caching is active and cache_templates is enabled" do
RablFastJson.cache_templates = true
ActionController::Base.stub(:perform_caching).and_return(true)
assert_equal @library.get_compiled_template('some/path', ""), @library.get_compiled_template('some/path', "")
end
test "don't cache templates cache_templates is enabled but perform_caching is not active" do
RablFastJson.cache_templates = true
ActionController::Base.stub(:perform_caching).and_return(false)
refute_equal @library.get_compiled_template('some/path', ""), @library.get_compiled_template('some/path', "")
end
end

View File

@ -5,6 +5,8 @@ require 'rspec/mocks'
require 'minitest/autorun'
require 'active_support/test_case'
require 'action_controller'
require 'singleton'
class <<Singleton
def included_with_reset(klass)