File tree 2 files changed +21
-2
lines changed
2 files changed +21
-2
lines changed Original file line number Diff line number Diff line change @@ -241,8 +241,15 @@ def attributes!
241
241
@attributes
242
242
end
243
243
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
+
246
253
@attributes = _merge_values ( @attributes , hash_or_array )
247
254
end
248
255
Original file line number Diff line number Diff line change @@ -196,6 +196,18 @@ class JbuilderTest < ActiveSupport::TestCase
196
196
assert_equal 'Pavel' , result [ 'author' ] [ 'name' ]
197
197
end
198
198
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
+
199
211
test 'blocks are additive via extract syntax' do
200
212
person = Person . new ( 'Pavel' , 27 )
201
213
You can’t perform that action at this time.
0 commit comments