Caching templates is now an option
This commit is contained in:
parent
3e9c903077
commit
e30e5dd66a
@ -13,4 +13,14 @@ require 'rabl-fast-json/handler'
|
|||||||
require 'rabl-fast-json/railtie'
|
require 'rabl-fast-json/railtie'
|
||||||
|
|
||||||
module RablFastJson
|
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
|
end
|
||||||
|
@ -21,13 +21,16 @@ module RablFastJson
|
|||||||
end
|
end
|
||||||
|
|
||||||
def get_compiled_template(path, source)
|
def get_compiled_template(path, source)
|
||||||
# @cached_templates[path] ||=
|
if path && RablFastJson.cache_templates?
|
||||||
Compiler.new.compile_source(source)
|
@cached_templates[path] ||= Compiler.new.compile_source(source)
|
||||||
|
else
|
||||||
|
Compiler.new.compile_source(source)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def get(path)
|
def get(path)
|
||||||
template = @cached_templates[path]
|
template = @cached_templates[path]
|
||||||
return template if !template.nil?
|
return template unless template.nil?
|
||||||
t = @view_renderer.lookup_context.find_template(path, [], false)
|
t = @view_renderer.lookup_context.find_template(path, [], false)
|
||||||
get_compiled_template(path, t.source)
|
get_compiled_template(path, t.source)
|
||||||
end
|
end
|
||||||
|
23
test/cache_templates_test.rb
Normal file
23
test/cache_templates_test.rb
Normal 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
|
@ -5,6 +5,8 @@ require 'rspec/mocks'
|
|||||||
require 'minitest/autorun'
|
require 'minitest/autorun'
|
||||||
require 'active_support/test_case'
|
require 'active_support/test_case'
|
||||||
|
|
||||||
|
require 'action_controller'
|
||||||
|
|
||||||
require 'singleton'
|
require 'singleton'
|
||||||
class <<Singleton
|
class <<Singleton
|
||||||
def included_with_reset(klass)
|
def included_with_reset(klass)
|
||||||
@ -44,5 +46,5 @@ class Context
|
|||||||
@_assigns[key]
|
@_assigns[key]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
User = Struct.new(:id, :name, :sex)
|
User = Struct.new(:id, :name, :sex)
|
Loading…
Reference in New Issue
Block a user