From d9e93b2c530c2e53e1d62c29cd7e1afc5a1399c2 Mon Sep 17 00:00:00 2001 From: ccocchi Date: Tue, 27 Mar 2012 17:07:30 +0200 Subject: [PATCH] Fix some errors --- lib/rabl-fast-json/compiler.rb | 6 +++--- lib/rabl-fast-json/template.rb | 17 +++++++++++++---- test/compiler_test.rb | 2 +- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/lib/rabl-fast-json/compiler.rb b/lib/rabl-fast-json/compiler.rb index acbfb22..016a887 100644 --- a/lib/rabl-fast-json/compiler.rb +++ b/lib/rabl-fast-json/compiler.rb @@ -71,10 +71,10 @@ module RablFastJson @template.merge!(t.source) end - def object(data) - return if data === false + def object(data, options = {}) data, name = extract_data_and_name(data) - @template.data, @template.root_name = data, name + @template.data = data + @template.root_name = options[:root] || name end protected diff --git a/lib/rabl-fast-json/template.rb b/lib/rabl-fast-json/template.rb index be89e67..b79f703 100644 --- a/lib/rabl-fast-json/template.rb +++ b/lib/rabl-fast-json/template.rb @@ -9,6 +9,13 @@ module RablFastJson @source = {} end + # + # def data=(symbol) + # raise "Data passed directly to template should be a symbol" if !symbol.is_a?(Symbol) + # @data = symbol + # end + # + def get_object_from_context @object = @context.instance_variable_get(@data) if @data end @@ -29,8 +36,9 @@ module RablFastJson def partial(template_path, options = {}) raise "No object was given to partial" if options[:object].blank? - template = Library.instance.get(template_path, @context) - template.render_resource(options[:object]) + object = options[:object] + template = Library.instance.get(template_path, @context) + object.respond_to?(:each) ? template.render_collection(object) : template.render_resource(object) end def render @@ -38,7 +46,7 @@ module RablFastJson get_assigns_from_context @object.respond_to?(:each) ? render_collection : render_resource end - + def render_resource(data = nil, source = nil) data ||= @object source ||= @source @@ -52,7 +60,7 @@ module RablFastJson when Proc instance_exec data, &value # node when Array # node with condition - next output if !value.first.call(data) + next output if !instance_exec data, &(value.first) #value.first.call(data) instance_exec data, &(value.last) when Hash current_value = value.dup @@ -77,5 +85,6 @@ module RablFastJson collection ||= @object collection.inject([]) { |output, o| output << render_resource(o, source) } end + end end \ No newline at end of file diff --git a/test/compiler_test.rb b/test/compiler_test.rb index cd35e7f..139211d 100644 --- a/test/compiler_test.rb +++ b/test/compiler_test.rb @@ -139,6 +139,6 @@ class CompilerTest < ActiveSupport::TestCase }) assert_equal({ :user => { :_data => :@user, :id => :id } }, t.source) - assert_nil t.data + assert_equal false, t.data end end \ No newline at end of file