Skip to content

nil-collection should be treated as empty array in template #148

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 9, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/jbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def set!(key, value = BLANK, *args, &block)
# { "age": 32 }
value
end
elsif value.respond_to?(:map)
elsif value.respond_to?(:map) || ((opts = args.last).respond_to?(:map) && opts[:as])
# json.comments @post.comments, :content, :created_at
# { "comments": [ { "content": "hello", "created_at": "..." }, { "content": "world", "created_at": "..." } ] }
_scope{ array! value, *args }
Expand Down Expand Up @@ -221,7 +221,7 @@ def child!
# json.array! [1, 2, 3]
#
# [1,2,3]
def array!(collection, *attributes, &block)
def array!(collection = [], *attributes, &block)
@attributes = if block && block.arity == 2
_two_arguments_map_collection(collection, &block)
elsif block
Expand Down
6 changes: 4 additions & 2 deletions lib/jbuilder/jbuilder_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ def partial!(name_or_options, locals = {})
options = { :partial => name_or_options, :locals => locals }
as = locals.delete(:as)
options[:as] = as if as.present?
options[:collection] = locals[:collection]
options[:collection] = locals[:collection] if locals.key?(:collection)
end

options[:collection] ||= [] if options.key?(:collection)

_handle_partial_options options
end

def array!(collection, *attributes, &block)
def array!(collection = [], *attributes, &block)
options = attributes.extract_options!

if options.key?(:partial)
Expand Down
32 changes: 32 additions & 0 deletions test/jbuilder_template_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ def assert_collection_rendered(json, context = nil)
assert_collection_rendered json
end

test 'partial! renders as empty array for nil-collection' do
json = render_jbuilder <<-JBUILDER
json.partial! 'blog_post', :collection => nil, :as => :blog_post
JBUILDER

assert_equal '[]', json
end

test 'partial! renders collection (alt. syntax)' do
json = render_jbuilder <<-JBUILDER
json.partial! :partial => 'blog_post', :collection => BLOG_POST_COLLECTION, :as => :blog_post
Expand All @@ -118,6 +126,14 @@ def assert_collection_rendered(json, context = nil)
assert_collection_rendered json
end

test 'partial! renders as empty array for nil-collection (alt. syntax)' do
json = render_jbuilder <<-JBUILDER
json.partial! :partial => 'blog_post', :collection => nil, :as => :blog_post
JBUILDER

assert_equal '[]', json
end

test 'render array of partials' do
json = render_jbuilder <<-JBUILDER
json.array! BLOG_POST_COLLECTION, :partial => 'blog_post', :as => :blog_post
Expand All @@ -126,6 +142,14 @@ def assert_collection_rendered(json, context = nil)
assert_collection_rendered json
end

test 'render array of partials as empty array with nil-collection' do
json = render_jbuilder <<-JBUILDER
json.array! nil, :partial => 'blog_post', :as => :blog_post
JBUILDER

assert_equal '[]', json
end

test 'render array if partials as a value' do
json = render_jbuilder <<-JBUILDER
json.posts BLOG_POST_COLLECTION, :partial => 'blog_post', :as => :blog_post
Expand All @@ -134,6 +158,14 @@ def assert_collection_rendered(json, context = nil)
assert_collection_rendered json, 'posts'
end

test 'render as empty array if partials as a nil value' do
json = render_jbuilder <<-JBUILDER
json.posts nil, :partial => 'blog_post', :as => :blog_post
JBUILDER

assert_equal '{"posts":[]}', json
end

test 'fragment caching a JSON object' do
undef_context_methods :fragment_name_with_digest, :cache_fragment_name

Expand Down