Support for nested array in API builder.

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@8720 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Jean-Philippe Lang 2012-01-29 18:29:09 +00:00
parent 64b59f1502
commit 0c5af16e14
3 changed files with 31 additions and 1 deletions

View File

@ -43,7 +43,11 @@ module Redmine
end end
else else
if @struct.last.is_a?(Array) if @struct.last.is_a?(Array)
@struct.last << (args.last || {}).merge(:value => args.first) if args.size == 1 && !block_given?
@struct.last << args.first
else
@struct.last << (args.last || {}).merge(:value => args.first)
end
else else
@struct.last[sym] = args.first @struct.last[sym] = args.first
end end

View File

@ -73,6 +73,19 @@ class Redmine::Views::Builders::JsonTest < ActiveSupport::TestCase
end end
end end
def test_nested_arrays
assert_json_output({'books' => [{'authors' => ['B. Smith', 'G. Cooper']}]}) do |b|
b.array :books do |books|
books.book do |book|
book.array :authors do |authors|
authors.author 'B. Smith'
authors.author 'G. Cooper'
end
end
end
end
end
def assert_json_output(expected, &block) def assert_json_output(expected, &block)
builder = Redmine::Views::Builders::Json.new builder = Redmine::Views::Builders::Json.new
block.call(builder) block.call(builder)

View File

@ -46,6 +46,19 @@ class Redmine::Views::Builders::XmlTest < ActiveSupport::TestCase
end end
end end
def test_nested_arrays
assert_xml_output('<books type="array"><book><authors type="array"><author>B. Smith</author><author>G. Cooper</author></authors></book></books>') do |b|
b.array :books do |books|
books.book do |book|
book.array :authors do |authors|
authors.author 'B. Smith'
authors.author 'G. Cooper'
end
end
end
end
end
def assert_xml_output(expected, &block) def assert_xml_output(expected, &block)
builder = Redmine::Views::Builders::Xml.new builder = Redmine::Views::Builders::Xml.new
block.call(builder) block.call(builder)