Skip to content

Commit

Permalink
[engine] Adds root_object method accessor
Browse files Browse the repository at this point in the history
  • Loading branch information
nesquena committed May 20, 2012
1 parent fe3fa33 commit d518956
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 4 additions & 1 deletion lib/rabl/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 8 additions & 5 deletions test/engine_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit d518956

Please sign in to comment.