Add root in DSL

This commit is contained in:
ccocchi 2012-09-19 17:36:57 +02:00
parent 9f0a92ecd7
commit a97a8cd24f
3 changed files with 17 additions and 2 deletions

View File

@ -1,6 +1,7 @@
# CHANGELOG # CHANGELOG
## 0.2.0 (unrelased) ## 0.2.0 (unrelased)
* Add `root` in DSL to set root without changing the source
* Add XML renderer * Add XML renderer
* Use MultiJson's preferred JSON engine as default (shmeltex) * Use MultiJson's preferred JSON engine as default (shmeltex)
* Default template to render with responder can be set per controller * Default template to render with responder can be set per controller

View File

@ -30,6 +30,10 @@ module RablRails
end end
alias_method :collection, :object alias_method :collection, :object
def root(name)
@template.root_name = name
end
# #
# Includes the attribute or method in the output # Includes the attribute or method in the output
# Example: # Example:

View File

@ -24,6 +24,16 @@ class CompilerTest < ActiveSupport::TestCase
assert_equal({}, t.source) assert_equal({}, t.source)
end end
test "root can be defined via keyword" do
t = @compiler.compile_source(%{ root :author })
assert_equal :author, t.root_name
end
test "root keyword override object root" do
t = @compiler.compile_source(%{ object :@user ; root :author })
assert_equal :author, t.root_name
end
test "collection set the data for the template" do test "collection set the data for the template" do
t = @compiler.compile_source(%{ collection :@user }) t = @compiler.compile_source(%{ collection :@user })
assert_equal :@user, t.data assert_equal :@user, t.data
@ -42,7 +52,7 @@ class CompilerTest < ActiveSupport::TestCase
assert_equal :@user, t.data assert_equal :@user, t.data
assert_equal :users, t.root_name assert_equal :users, t.root_name
end end
test "root can be set to false via options" do test "root can be set to false via options" do
t = @compiler.compile_source(%( object :@user, root: false)) t = @compiler.compile_source(%( object :@user, root: false))
assert_equal false, t.root_name assert_equal false, t.root_name
@ -154,7 +164,7 @@ class CompilerTest < ActiveSupport::TestCase
assert_equal({ :user => { :_data => :@user, :id => :id } }, t.source) assert_equal({ :user => { :_data => :@user, :id => :id } }, t.source)
assert_equal false, t.data assert_equal false, t.data
end end
test "name extraction from argument" do test "name extraction from argument" do
assert_equal [:@users, 'users'], @compiler.send(:extract_data_and_name, :@users) assert_equal [:@users, 'users'], @compiler.send(:extract_data_and_name, :@users)
assert_equal [:users, :users], @compiler.send(:extract_data_and_name, :users) assert_equal [:users, :users], @compiler.send(:extract_data_and_name, :users)