Skip to content

Commit

Permalink
Allow Jbuilder instance in merge! (#485)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterzhu2118 committed Jan 22, 2021
1 parent 5c82764 commit 469a530
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/jbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,15 @@ def attributes!
@attributes
end

# Merges hash or array into current builder.
def merge!(hash_or_array)
# Merges hash, array, or Jbuilder instance into current builder.
def merge!(obj)
hash_or_array =
if ::Jbuilder === obj
obj.attributes!
else
obj
end

@attributes = _merge_values(@attributes, hash_or_array)
end

Expand Down
12 changes: 12 additions & 0 deletions test/jbuilder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,18 @@ class JbuilderTest < ActiveSupport::TestCase
assert_equal 'Pavel', result['author']['name']
end

test 'support merge! method with Jbuilder instance' do
obj = jbuild do |json|
json.foo 'bar'
end

result = jbuild do |json|
json.merge! obj
end

assert_equal 'bar', result['foo']
end

test 'blocks are additive via extract syntax' do
person = Person.new('Pavel', 27)

Expand Down

0 comments on commit 469a530

Please sign in to comment.