From bb7b5bbc84a69a46cbb40b60a130dfd38d7d3d87 Mon Sep 17 00:00:00 2001 From: Jan Berdajs Date: Wed, 19 Jun 2013 12:08:57 +0200 Subject: [PATCH] Do not use a collection as the default object. Fixes #142 --- lib/rabl/engine.rb | 3 ++- test/engine_test.rb | 8 ++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/rabl/engine.rb b/lib/rabl/engine.rb index 004cb42f..5142c100 100644 --- a/lib/rabl/engine.rb +++ b/lib/rabl/engine.rb @@ -209,7 +209,8 @@ def default_object if context_scope.respond_to?(:controller) controller_name = context_scope.controller.controller_name stripped_name = controller_name.split(%r{::|\/}).last - instance_variable_get("@#{stripped_name}") + object = instance_variable_get("@#{stripped_name}") + is_object?(object) ? object : nil end end diff --git a/test/engine_test.rb b/test/engine_test.rb index 2b10af86..de46c434 100644 --- a/test/engine_test.rb +++ b/test/engine_test.rb @@ -373,6 +373,14 @@ template.render(scope).split end.equals "{\"name\":\"rabl\"}".split + asserts "that it does not set a collection as default object" do + template = rabl %{ + attribute :name + } + scope = context_scope('user', []) + template.render(scope).split + end.equals "{}".split + asserts "that it sets data source" do template = rabl %q{ object @user