Skip to content

Commit 469a530

Browse files
authored
Allow Jbuilder instance in merge! (#485)
1 parent 5c82764 commit 469a530

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

lib/jbuilder.rb

+9-2
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,15 @@ def attributes!
241241
@attributes
242242
end
243243

244-
# Merges hash or array into current builder.
245-
def merge!(hash_or_array)
244+
# Merges hash, array, or Jbuilder instance into current builder.
245+
def merge!(obj)
246+
hash_or_array =
247+
if ::Jbuilder === obj
248+
obj.attributes!
249+
else
250+
obj
251+
end
252+
246253
@attributes = _merge_values(@attributes, hash_or_array)
247254
end
248255

test/jbuilder_test.rb

+12
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,18 @@ class JbuilderTest < ActiveSupport::TestCase
196196
assert_equal 'Pavel', result['author']['name']
197197
end
198198

199+
test 'support merge! method with Jbuilder instance' do
200+
obj = jbuild do |json|
201+
json.foo 'bar'
202+
end
203+
204+
result = jbuild do |json|
205+
json.merge! obj
206+
end
207+
208+
assert_equal 'bar', result['foo']
209+
end
210+
199211
test 'blocks are additive via extract syntax' do
200212
person = Person.new('Pavel', 27)
201213

0 commit comments

Comments
 (0)