Skip to content

Commit

Permalink
Remove deprecated old nesting style
Browse files Browse the repository at this point in the history
  • Loading branch information
rwz committed Oct 12, 2013
1 parent 1ea286a commit 07a35ee
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 71 deletions.
22 changes: 2 additions & 20 deletions lib/jbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,7 @@ def child!
#
# [1,2,3]
def array!(collection = [], *attributes, &block)
@attributes = if block && block.arity == 2
_two_arguments_map_collection(collection, &block)
elsif block
@attributes = if block
_map_collection(collection, &block)
elsif attributes.any?
_map_collection(collection) { |element| extract! element, *attributes }
Expand Down Expand Up @@ -278,7 +276,7 @@ def attributes!

# Encodes the current builder as JSON.
def target!
::MultiJson.dump @attributes
::MultiJson.dump(@attributes)
end

private
Expand Down Expand Up @@ -332,22 +330,6 @@ def _merge(hash_or_array)
end
end

def _two_arguments_map_collection(collection, &block)
message = "Passing jbuilder object to block is " \
"deprecated and will be removed soon."

if block.respond_to?(:parameters)
arguments = block.parameters.map(&:last)
actual = "|#{arguments.drop(1) * ', '}|"
deprecated = "|#{arguments * ', '}|"
message += "\nUse #{actual} instead of #{deprecated} as block arguments"
end

::ActiveSupport::Deprecation.warn message, ::Kernel.caller(5)

_map_collection(collection){ |element| block[self, element] }
end

def _mapable_arguments?(value, *args)
value.respond_to?(:map)
end
Expand Down
51 changes: 0 additions & 51 deletions test/jbuilder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -241,23 +241,6 @@ def initialize(name, age)
assert_equal 'world', parsed['comments'].second['content']
end

test 'nesting multiple children from array with inline loop with old api' do
comments = [ Comment.new('hello', 1), Comment.new('world', 2) ]

json = Jbuilder.encode do |json|
::ActiveSupport::Deprecation.silence do
json.comments comments do |json, comment|
json.content comment.content
end
end
end

parsed = MultiJson.load(json)
assert_equal ['content'], parsed['comments'].first.keys
assert_equal 'hello', parsed['comments'].first['content']
assert_equal 'world', parsed['comments'].second['content']
end

test 'nesting multiple children from array with inline loop on root' do
comments = [ Comment.new('hello', 1), Comment.new('world', 2) ]

Expand All @@ -272,22 +255,6 @@ def initialize(name, age)
assert_equal 'world', parsed.second['content']
end

test 'nesting multiple children from array with inline loop on root with old api' do
comments = [ Comment.new('hello', 1), Comment.new('world', 2) ]

json = Jbuilder.encode do |json|
::ActiveSupport::Deprecation.silence do
json.call(comments) do |json, comment|
json.content comment.content
end
end
end

parsed = MultiJson.load(json)
assert_equal 'hello', parsed.first['content']
assert_equal 'world', parsed.second['content']
end

test 'array nested inside nested hash' do
json = Jbuilder.encode do |json|
json.author do
Expand Down Expand Up @@ -337,24 +304,6 @@ def initialize(name, age)
assert_not_equal 'hello', MultiJson.load(json)[0]['not_in_json']
end

test 'directly set an array nested in another array with old api' do
data = [ { :department => 'QA', :not_in_json => 'hello', :names => ['John', 'David'] } ]
json = Jbuilder.encode do |json|
::ActiveSupport::Deprecation.silence do
json.array! data do |json, object|
json.department object[:department]
json.names do
json.array! object[:names]
end
end
end
end

parsed = MultiJson.load(json)
assert_equal 'David', parsed.first['names'].last
assert_not_equal 'hello', parsed.first['not_in_json']
end

test 'nested jbuilder objects' do
to_nest = Jbuilder.new
to_nest.nested_value 'Nested Test'
Expand Down

0 comments on commit 07a35ee

Please sign in to comment.