From d518956f8c143289e08d3398926defa821554877 Mon Sep 17 00:00:00 2001 From: Nathan Esquenazi Date: Sun, 20 May 2012 16:44:03 -0700 Subject: [PATCH] [engine] Adds root_object method accessor --- CHANGELOG.md | 1 + lib/rabl/engine.rb | 5 ++++- test/engine_test.rb | 13 ++++++++----- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6084b31a..e2526b40 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ * Fix view_path options for renderer (Thanks @ivanvanderbyl and @route) * Only escape if data exists * Fix default object recognition for Rails 2.3.2 + * Adds `root_object` method on engine (Thanks @OliverLetterer) ## 0.6.11 diff --git a/lib/rabl/engine.rb b/lib/rabl/engine.rb index 4dac92f0..3235c34c 100644 --- a/lib/rabl/engine.rb +++ b/lib/rabl/engine.rb @@ -109,7 +109,10 @@ def to_bson(options={}) def object(data) @_data = data unless @_locals[:object] end - + + # Returns the current object that is the topic of this template + # Can be the collection or the object depending on topic assigned + # root_object => @user def root_object @_data end diff --git a/test/engine_test.rb b/test/engine_test.rb index 8e198f88..a7bf9f4b 100644 --- a/test/engine_test.rb +++ b/test/engine_test.rb @@ -303,7 +303,7 @@ scope.instance_variable_set :@user, User.new template.render(scope).split end.equals "{\"name\":\"rabl\"}".split - + asserts "that it sets data source" do template = rabl %q{ object @user @@ -312,7 +312,7 @@ scope.instance_variable_set :@user, User.new template.render(scope) end.matches "{}" - + asserts "that it can set root node" do template = rabl %q{ object @user => :person @@ -371,7 +371,7 @@ scope.instance_variable_set :@user, User.new(:name => 'irvine') template.render(scope) end.equals "{\"name\":\"irvine\"}" - + asserts "that it can add attribute under a different key name through :as" do template = rabl %{ object @user @@ -381,13 +381,16 @@ scope.instance_variable_set :@user, User.new(:name => 'irvine') template.render(scope) end.equals "{\"city\":\"irvine\"}" - + asserts "that it exposes root_object" do template = rabl %q{ object @user - attribute :name, :as => root_object.city.to_sym + attribute :name, :as => root_object.city } + scope = Object.new + scope.instance_variable_set :@user, User.new(:name => 'irvine') + template.render(scope) end.equals "{\"irvine\":\"irvine\"}" asserts "that it can add attribute under a different key name through hash" do