From dfbe769a910dc5058e0b0b07caae451c6cf18557 Mon Sep 17 00:00:00 2001 From: ccocchi Date: Thu, 15 Mar 2012 15:12:52 +0100 Subject: [PATCH] Enable non restful template --- lib/rabl-fast-json/compiler.rb | 3 ++- lib/rabl-fast-json/template.rb | 17 ++++++++++----- test/compiled_template_test.rb | 1 + test/compiler_test.rb | 12 +++++++++++ test/deep_nesting_test.rb | 1 + test/non_restful_response_test.rb | 35 +++++++++++++++++++++++++++++++ 6 files changed, 63 insertions(+), 6 deletions(-) create mode 100644 test/non_restful_response_test.rb diff --git a/lib/rabl-fast-json/compiler.rb b/lib/rabl-fast-json/compiler.rb index 3b614df..e935e40 100644 --- a/lib/rabl-fast-json/compiler.rb +++ b/lib/rabl-fast-json/compiler.rb @@ -6,7 +6,7 @@ module RablFastJson @context = context @glue_count = 0 end - + def compile_source(source) @template = CompiledTemplate.new instance_eval(source) @@ -72,6 +72,7 @@ module RablFastJson end def object(data) + return if data === false data, name = extract_data_and_name(data) @template.data, @template.root_name = data, name end diff --git a/lib/rabl-fast-json/template.rb b/lib/rabl-fast-json/template.rb index 5081e8e..b302862 100644 --- a/lib/rabl-fast-json/template.rb +++ b/lib/rabl-fast-json/template.rb @@ -9,12 +9,19 @@ module RablFastJson @source = {} end - def get_object_from_assigns - @object = @context.instance_variable_get(@data) + def get_object_from_context + @object = @context.instance_variable_get(@data) if @data + end + + def get_assigns_from_context + @context.instance_variable_get(:@_assigns).each_pair { |k, v| + instance_variable_set("@#{k}", v) unless k.start_with?('_') || k == @data + } end def render - get_object_from_assigns + get_object_from_context + get_assigns_from_context @object.respond_to?(:each) ? render_collection : render_resource end @@ -29,10 +36,10 @@ module RablFastJson when Symbol data.send(value) # attributes when Proc - value.call(data) # node + instance_exec data, &value # node when Array # node with condition next output if !value.first.call(data) - value.last.call(data) + instance_exec data, &(value.last) when Hash current_value = value.dup data_symbol = current_value.delete(:_data) diff --git a/test/compiled_template_test.rb b/test/compiled_template_test.rb index 2555651..983c363 100644 --- a/test/compiled_template_test.rb +++ b/test/compiled_template_test.rb @@ -7,6 +7,7 @@ class TestCompiledTemplate < ActiveSupport::TestCase @data = User.new(1, 'foobar', 'male') @data.stub(:respond_to?).with(:each).and_return(false) @context.stub(:instance_variable_get).with(:@data).and_return(@data) + @context.stub(:instance_variable_get).with(:@_assigns).and_return({}) @template = RablFastJson::CompiledTemplate.new @template.context = @context @template.data = :@data diff --git a/test/compiler_test.rb b/test/compiler_test.rb index fe73da9..cd35e7f 100644 --- a/test/compiler_test.rb +++ b/test/compiler_test.rb @@ -129,4 +129,16 @@ class CompilerTest < ActiveSupport::TestCase assert_instance_of Array, t.source[:foo] assert_equal 2, t.source[:foo].size end + + test "compile with no object" do + t = @compiler.compile_source(%{ + object false + child(:@user => :user) do + attribute :id + end + }) + + assert_equal({ :user => { :_data => :@user, :id => :id } }, t.source) + assert_nil t.data + end end \ No newline at end of file diff --git a/test/deep_nesting_test.rb b/test/deep_nesting_test.rb index 607ba98..3c3e87c 100644 --- a/test/deep_nesting_test.rb +++ b/test/deep_nesting_test.rb @@ -29,6 +29,7 @@ class DeepNestingTest < ActiveSupport::TestCase @context.stub(:instance_variable_get).with(:@user).and_return(@user) @context.stub(:instance_variable_get).with(:@view_renderer).and_return(@view_renderer) @context.stub(:instance_variable_get).with(:@virtual_path).and_return('users/show') + @context.stub(:instance_variable_get).with(:@_assigns).and_return({}) end test "compile and render deep nesting template" do diff --git a/test/non_restful_response_test.rb b/test/non_restful_response_test.rb new file mode 100644 index 0000000..8eadfcd --- /dev/null +++ b/test/non_restful_response_test.rb @@ -0,0 +1,35 @@ +require 'test_helper' + +class NonRestfulResponseTest < ActiveSupport::TestCase + setup do + RablFastJson::Library.reset_instance + + @user = User.new(1, 'foo', 'male') + @user.stub_chain(:posts, :count).and_return(10) + @user.stub(:respond_to?).with(:each).and_return(false) + + @context = Context.new + @context.stub(:instance_variable_get).with(:@user).and_return(@user) + @context.stub(:instance_variable_get).with(:@view_renderer).and_return(mock()) + @context.stub(:instance_variable_get).with(:@virtual_path).and_return('user/show') + @context.stub(:instance_variable_get).with(:@_assigns).and_return({'user' => @user}) + end + + test "compile and render non restful resource" do + source = %{ + object false + node(:post_count) { @user.posts.count } + child(:@user => :user) do + attributes :id, :name + end + } + + assert_equal(ActiveSupport::JSON.encode({ + :post_count => 10, + :user => { + :id => 1, + :name => 'foo' + } + }), RablFastJson::Library.instance.get_rendered_template(source, @context)) + end +end \ No newline at end of file