Skip to content

Commit e5be60e

Browse files
Fix key format propagation to children in template.
1 parent 646c49b commit e5be60e

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

lib/jbuilder_template.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ def self.encode(context)
33
new(context)._tap { |jbuilder| yield jbuilder }.target!
44
end
55

6-
def initialize(context)
6+
def initialize(context, *args)
77
@context = context
8-
super()
8+
super(*args)
99
end
1010

1111
def partial!(options, locals = {})
@@ -21,7 +21,7 @@ def partial!(options, locals = {})
2121

2222
private
2323
def _new_instance
24-
__class__.new(@context)
24+
__class__.new(@context, @key_formatter)
2525
end
2626
end
2727

test/jbuilder_template_test.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,25 @@ class JbuilderTemplateTest < ActiveSupport::TestCase
1515

1616
assert_equal "hello", JSON.parse(json)["content"]
1717
end
18+
19+
test "key_format! with parameter" do
20+
json = JbuilderTemplate.new(binding)
21+
json.key_format! :camelize => [:lower]
22+
json.camel_style "for JS"
23+
24+
assert_equal ['camelStyle'], json.attributes!.keys
25+
end
26+
27+
test "key_format! propagates to child elements" do
28+
json = JbuilderTemplate.new(binding)
29+
json.key_format! :upcase
30+
json.level1 "one"
31+
json.level2 do |json|
32+
json.value "two"
33+
end
34+
35+
result = json.attributes!
36+
assert_equal "one", result["LEVEL1"]
37+
assert_equal "two", result["LEVEL2"]["VALUE"]
38+
end
1839
end

0 commit comments

Comments
 (0)