diff --git a/lib/rabl-rails/compiler.rb b/lib/rabl-rails/compiler.rb index de7a6eb..ea291bb 100644 --- a/lib/rabl-rails/compiler.rb +++ b/lib/rabl-rails/compiler.rb @@ -62,7 +62,7 @@ module RablRails data, name = extract_data_and_name(name_or_data) name = options[:root] if options[:root] if options[:partial] - template = Library.instance.get(options[:partial]) + template = Library.instance.compile_template_from_path(options[:partial]) @template[name] = template.merge!(:_data => data) elsif block_given? @template[name] = sub_compile(data) { yield } @@ -110,7 +110,7 @@ module RablRails # extends 'users/base' # def extends(path) - t = Library.instance.get(path) + t = Library.instance.compile_template_from_path(path) @template.merge!(t.source) end diff --git a/lib/rabl-rails/library.rb b/lib/rabl-rails/library.rb index a727e7f..0324369 100644 --- a/lib/rabl-rails/library.rb +++ b/lib/rabl-rails/library.rb @@ -12,13 +12,13 @@ module RablRails path = context.instance_variable_get(:@virtual_path) @lookup_context = context.lookup_context - compiled_template = get_compiled_template(path, source) + compiled_template = compile_template_from_source(source, path) format = context.params[:format] || 'json' Renderers.const_get(format.upcase!).new(context).render(compiled_template) end - def get_compiled_template(path, source) + def compile_template_from_source(source, path = nil) if path && RablRails.cache_templates? @cached_templates[path] ||= Compiler.new.compile_source(source) @cached_templates[path].dup @@ -27,11 +27,11 @@ module RablRails end end - def get(path) + def compile_template_from_path(path) template = @cached_templates[path] return template if template t = @lookup_context.find_template(path, [], false) - get_compiled_template(path, t.source) + compile_template_from_source(t.source, path) end end end \ No newline at end of file diff --git a/lib/rabl-rails/renderers/base.rb b/lib/rabl-rails/renderers/base.rb index e89b342..2d04abd 100644 --- a/lib/rabl-rails/renderers/base.rb +++ b/lib/rabl-rails/renderers/base.rb @@ -88,7 +88,7 @@ module RablRails return [] if object.respond_to?(:empty?) && object.empty? - template = Library.instance.get(template_path) + template = Library.instance.compile_template_from_path(template_path) object.respond_to?(:each) ? render_collection(object, template.source) : render_resource(object, template.source) end diff --git a/test/cache_templates_test.rb b/test/cache_templates_test.rb index 7a7797c..06aa5e9 100644 --- a/test/cache_templates_test.rb +++ b/test/cache_templates_test.rb @@ -10,24 +10,24 @@ class CacheTemplatesTest < ActiveSupport::TestCase test "cache templates if perform_caching is active and cache_templates is enabled" do ActionController::Base.stub(:perform_caching).and_return(true) - @library.get_compiled_template('some/path', "") - t = @library.get_compiled_template('some/path', "attribute :id") + @library.compile_template_from_source('', 'some/path') + t = @library.compile_template_from_source("attribute :id", 'some/path') assert_equal({}, t.source) end test "cached templates should not be modifiable in place" do ActionController::Base.stub(:perform_caching).and_return(true) - @library.get_compiled_template('some/path', "") - t = @library.get_compiled_template('some/path', "attribute :id") + @library.compile_template_from_source('', 'some/path') + t = @library.compile_template_from_source("attribute :id", 'some/path') assert_equal({}, t.source) end test "don't cache templates cache_templates is enabled but perform_caching is not active" do ActionController::Base.stub(:perform_caching).and_return(false) - @library.get_compiled_template('some/path', "") - t = @library.get_compiled_template('some/path', "attribute :id") + @library.compile_template_from_source('', 'some/path') + t = @library.compile_template_from_source("attribute :id", 'some/path') assert_equal({ :id => :id }, t.source) end diff --git a/test/compiler_test.rb b/test/compiler_test.rb index 277504a..493e4e6 100644 --- a/test/compiler_test.rb +++ b/test/compiler_test.rb @@ -94,7 +94,7 @@ class CompilerTest < ActiveSupport::TestCase mock_template = RablRails::CompiledTemplate.new mock_template.source = { :id => :id } RablRails::Library.reset_instance - RablRails::Library.instance.stub(:get).with('users/base').and_return(mock_template) + RablRails::Library.instance.stub(:compile_template_from_path).with('users/base').and_return(mock_template) t = @compiler.compile_source(%{child(:user, :partial => 'users/base') }) assert_equal( {:user => { :_data => :user, :id => :id } }, t.source) @@ -120,7 +120,7 @@ class CompilerTest < ActiveSupport::TestCase test "extends use other template source as itself" do template = mock('template', :source => { :id => :id }) RablRails::Library.reset_instance - RablRails::Library.instance.stub(:get).with('users/base').and_return(template) + RablRails::Library.instance.stub(:compile_template_from_path).with('users/base').and_return(template) t = @compiler.compile_source(%{ extends 'users/base' }) assert_equal({ :id => :id }, t.source) end diff --git a/test/renderers/json_renderer_test.rb b/test/renderers/json_renderer_test.rb index aa62713..8b376d5 100644 --- a/test/renderers/json_renderer_test.rb +++ b/test/renderers/json_renderer_test.rb @@ -94,7 +94,7 @@ class TestJsonRenderer < ActiveSupport::TestCase t = RablRails::CompiledTemplate.new t.source = { :name => :name } RablRails::Library.reset_instance - RablRails::Library.instance.should_receive(:get).with('users/base').and_return(t) + RablRails::Library.instance.should_receive(:compile_template_from_path).with('users/base').and_return(t) @template.data = false @template.source = { :user => ->(s) { partial('users/base', :object => @user) } }