Skip to content

Commit a6863b5

Browse files
committed
Ensure that Jbuilder.encode properly forwards arguments to .new
The initializer previously accepted an options hash, but now takes kwargs. So the encode method also needs to accept (and forward) kwargs.
1 parent 30ba7df commit a6863b5

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

lib/jbuilder.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ def initialize(
2828
end
2929

3030
# Yields a builder and automatically turns the result into a JSON string
31-
def self.encode(*args, &block)
32-
new(*args, &block).target!
31+
def self.encode(...)
32+
new(...).target!
3333
end
3434

3535
BLANK = Blank.new

test/jbuilder_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -936,4 +936,11 @@ class JbuilderTest < ActiveSupport::TestCase
936936
result = JSON.load(Jbuilder.encode { |json| json.time Time.parse("2018-05-13 11:51:00.485 -0400") })
937937
assert_equal "2018-05-13T11:51:00.485-04:00", result["time"]
938938
end
939+
940+
test "encode forwards options to new" do
941+
Jbuilder.encode(key_formatter: 1, ignore_nil: 2) do |json|
942+
assert_equal 1, json.instance_eval{ @key_formatter }
943+
assert_equal 2, json.instance_eval{ @ignore_nil }
944+
end
945+
end
939946
end

0 commit comments

Comments
 (0)