Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Jbuilder instances in merge! #485

Merged
merged 1 commit into from
Jan 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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