Dup the template to avoid in place modification across users

This commit is contained in:
ccocchi 2012-04-03 18:37:11 +02:00
parent e30e5dd66a
commit 3c7448ebfd
2 changed files with 13 additions and 1 deletions

View File

@ -23,6 +23,7 @@ module RablFastJson
def get_compiled_template(path, source)
if path && RablFastJson.cache_templates?
@cached_templates[path] ||= Compiler.new.compile_source(source)
@cached_templates[path].dup
else
Compiler.new.compile_source(source)
end

View File

@ -10,8 +10,19 @@ class CacheTemplatesTest < ActiveSupport::TestCase
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)
@library.get_compiled_template('some/path', "")
t = @library.get_compiled_template('some/path', "attribute :id")
assert_equal @library.get_compiled_template('some/path', ""), @library.get_compiled_template('some/path', "")
assert_equal({}, t.source)
end
test "cached templates should not be modifiable in place" do
RablFastJson.cache_templates = true
ActionController::Base.stub(:perform_caching).and_return(true)
t = @library.get_compiled_template('some/path', "")
assert_nil t.context
t.context = "foobar"
assert_nil @library.get_compiled_template('some/path', "").context
end
test "don't cache templates cache_templates is enabled but perform_caching is not active" do