diff --git a/lib/rabl-fast-json.rb b/lib/rabl-fast-json.rb index 408a864..8aed86f 100644 --- a/lib/rabl-fast-json.rb +++ b/lib/rabl-fast-json.rb @@ -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 diff --git a/lib/rabl-fast-json/library.rb b/lib/rabl-fast-json/library.rb index 7b73e9e..b7f6661 100644 --- a/lib/rabl-fast-json/library.rb +++ b/lib/rabl-fast-json/library.rb @@ -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 diff --git a/test/cache_templates_test.rb b/test/cache_templates_test.rb new file mode 100644 index 0000000..c9e01c2 --- /dev/null +++ b/test/cache_templates_test.rb @@ -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 \ No newline at end of file diff --git a/test/test_helper.rb b/test/test_helper.rb index 33e227d..f315ce5 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -5,6 +5,8 @@ require 'rspec/mocks' require 'minitest/autorun' require 'active_support/test_case' +require 'action_controller' + require 'singleton' class <