File tree Expand file tree Collapse file tree 2 files changed +21
-2
lines changed Expand file tree Collapse file tree 2 files changed +21
-2
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments