jbuilder 2.10.2 → 2.11.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +10 -3
- data/CONTRIBUTING.md +1 -1
- data/jbuilder.gemspec +1 -1
- data/lib/generators/rails/jbuilder_generator.rb +4 -0
- data/lib/generators/rails/templates/partial.json.jbuilder +14 -0
- data/lib/jbuilder.rb +18 -7
- data/test/jbuilder_generator_test.rb +12 -0
- data/test/jbuilder_test.rb +64 -0
- data/test/scaffold_api_controller_generator_test.rb +11 -0
- data/test/scaffold_controller_generator_test.rb +10 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8e18daf81b639ef039951269f0df24a2ffb0a4d6e93c05861eebef18c85a9039
|
4
|
+
data.tar.gz: db3ff93fbd28f3a67273b0afbae0962e5e2f49842901b3b2ade5bb3831b74d9b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b74be66e7ba0559d14c7032bfff001e9c9a49c657edab95de06c289eb3600945dea14c3efd9595b0c7da524d31f21c407c8f2b4541da308be8921951f94103e3
|
7
|
+
data.tar.gz: 8ae3e52031fd58c5de35fae022c9224c116d13a93c85990f5385511a8a914ce488f41b69676f94b54d3442942f3a30f79e460fdc78d84633261d239654505547
|
data/CHANGELOG.md
CHANGED
@@ -1,17 +1,24 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
2.11.0
|
4
|
+
------
|
5
|
+
|
6
|
+
* [Allow jbuilder instance to be passed to #merge!](https://github.com/rails/jbuilder/pull/485)
|
7
|
+
* [Fix for key_format! when using nested hashes](https://github.com/rails/jbuilder/pull/486)
|
8
|
+
* [Include rich_text, attachment, and attachments fields in json partial](https://github.com/rails/jbuilder/pull/459)
|
9
|
+
|
3
10
|
2.10.2
|
4
|
-
|
11
|
+
------
|
5
12
|
|
6
13
|
* Update scaffold generator to use double quotes, 422 form error responds, and modern string-of-arrays syntax [DHH]
|
7
14
|
|
8
15
|
2.10.1
|
9
|
-
|
16
|
+
------
|
10
17
|
|
11
18
|
* Fix keyword arguments warning on Ruby 2.7
|
12
19
|
|
13
20
|
2.10.0
|
14
|
-
|
21
|
+
------
|
15
22
|
|
16
23
|
* Requires Rails 5+ and Ruby 2.2+
|
17
24
|
* Nested hashes are deep-merged
|
data/CONTRIBUTING.md
CHANGED
@@ -13,7 +13,7 @@ Jbuilder is work of [many contributors](https://github.com/rails/jbuilder/graphs
|
|
13
13
|
|
14
14
|
#### Fork the Project
|
15
15
|
|
16
|
-
Fork the [project on
|
16
|
+
Fork the [project on GitHub](https://github.com/rails/jbuilder) and check out your copy.
|
17
17
|
|
18
18
|
```
|
19
19
|
git clone https://github.com/contributor/jbuilder.git
|
data/jbuilder.gemspec
CHANGED
@@ -1,2 +1,16 @@
|
|
1
1
|
json.extract! <%= singular_table_name %>, <%= full_attributes_list %>
|
2
2
|
json.url <%= singular_table_name %>_url(<%= singular_table_name %>, format: :json)
|
3
|
+
<%- virtual_attributes.each do |attribute| -%>
|
4
|
+
<%- if attribute.type == :rich_text -%>
|
5
|
+
json.<%= attribute.name %> <%= singular_table_name %>.<%= attribute.name %>.to_s
|
6
|
+
<%- elsif attribute.type == :attachment -%>
|
7
|
+
json.<%= attribute.name %> url_for(<%= singular_table_name %>.<%= attribute.name %>)
|
8
|
+
<%- elsif attribute.type == :attachments -%>
|
9
|
+
json.<%= attribute.name %> do
|
10
|
+
json.array!(<%= singular_table_name %>.<%= attribute.name %>) do |<%= attribute.singular_name %>|
|
11
|
+
json.id <%= attribute.singular_name %>.id
|
12
|
+
json.url url_for(<%= attribute.singular_name %>)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
<%- end -%>
|
16
|
+
<%- end -%>
|
data/lib/jbuilder.rb
CHANGED
@@ -43,11 +43,11 @@ class Jbuilder
|
|
43
43
|
# json.age 32
|
44
44
|
# json.person another_jbuilder
|
45
45
|
# { "age": 32, "person": { ... }
|
46
|
-
value.attributes!
|
46
|
+
_format_keys(value.attributes!)
|
47
47
|
else
|
48
48
|
# json.age 32
|
49
49
|
# { "age": 32 }
|
50
|
-
value
|
50
|
+
_format_keys(value)
|
51
51
|
end
|
52
52
|
elsif _is_collection?(value)
|
53
53
|
# json.comments @post.comments, :content, :created_at
|
@@ -241,8 +241,9 @@ class Jbuilder
|
|
241
241
|
@attributes
|
242
242
|
end
|
243
243
|
|
244
|
-
# Merges hash or
|
245
|
-
def merge!(
|
244
|
+
# Merges hash, array, or Jbuilder instance into current builder.
|
245
|
+
def merge!(object)
|
246
|
+
hash_or_array = ::Jbuilder === object ? object.attributes! : object
|
246
247
|
@attributes = _merge_values(@attributes, hash_or_array)
|
247
248
|
end
|
248
249
|
|
@@ -272,11 +273,11 @@ class Jbuilder
|
|
272
273
|
if _blank?(updates)
|
273
274
|
current_value
|
274
275
|
elsif _blank?(current_value) || updates.nil? || current_value.empty? && ::Array === updates
|
275
|
-
updates
|
276
|
+
_format_keys(updates)
|
276
277
|
elsif ::Array === current_value && ::Array === updates
|
277
|
-
current_value + updates
|
278
|
+
current_value + _format_keys(updates)
|
278
279
|
elsif ::Hash === current_value && ::Hash === updates
|
279
|
-
current_value.deep_merge(updates)
|
280
|
+
current_value.deep_merge(_format_keys(updates))
|
280
281
|
else
|
281
282
|
raise MergeError.build(current_value, updates)
|
282
283
|
end
|
@@ -286,6 +287,16 @@ class Jbuilder
|
|
286
287
|
@key_formatter ? @key_formatter.format(key) : key.to_s
|
287
288
|
end
|
288
289
|
|
290
|
+
def _format_keys(hash_or_array)
|
291
|
+
if ::Array === hash_or_array
|
292
|
+
hash_or_array.map { |value| _format_keys(value) }
|
293
|
+
elsif ::Hash === hash_or_array
|
294
|
+
::Hash[hash_or_array.collect { |k, v| [_key(k), _format_keys(v)] }]
|
295
|
+
else
|
296
|
+
hash_or_array
|
297
|
+
end
|
298
|
+
end
|
299
|
+
|
289
300
|
def _set_value(key, value)
|
290
301
|
raise NullError.build(key) if @attributes.nil?
|
291
302
|
raise ArrayError.build(key) if ::Array === @attributes
|
@@ -43,4 +43,16 @@ class JbuilderGeneratorTest < Rails::Generators::TestCase
|
|
43
43
|
assert_no_match %r{:created_at, :updated_at}, content
|
44
44
|
end
|
45
45
|
end
|
46
|
+
|
47
|
+
if Rails::VERSION::MAJOR >= 6
|
48
|
+
test 'handles virtual attributes' do
|
49
|
+
run_generator %w(Message content:rich_text video:attachment photos:attachments)
|
50
|
+
|
51
|
+
assert_file 'app/views/messages/_message.json.jbuilder' do |content|
|
52
|
+
assert_match %r{json\.content message\.content\.to_s}, content
|
53
|
+
assert_match %r{json\.video url_for\(message\.video\)}, content
|
54
|
+
assert_match %r{json\.photos do\n json\.array!\(message\.photos\) do \|photo\|\n json\.id photo\.id\n json\.url url_for\(photo\)\n end\nend}, content
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
46
58
|
end
|
data/test/jbuilder_test.rb
CHANGED
@@ -196,6 +196,18 @@ class JbuilderTest < ActiveSupport::TestCase
|
|
196
196
|
assert_equal 'Pavel', result['author']['name']
|
197
197
|
end
|
198
198
|
|
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
|
+
|
199
211
|
test 'blocks are additive via extract syntax' do
|
200
212
|
person = Person.new('Pavel', 27)
|
201
213
|
|
@@ -581,6 +593,58 @@ class JbuilderTest < ActiveSupport::TestCase
|
|
581
593
|
assert_equal ['oats and friends'], result.keys
|
582
594
|
end
|
583
595
|
|
596
|
+
test 'key_format! with merge!' do
|
597
|
+
hash = { camel_style: 'for JS' }
|
598
|
+
result = jbuild do |json|
|
599
|
+
json.key_format! camelize: :lower
|
600
|
+
json.merge! hash
|
601
|
+
end
|
602
|
+
|
603
|
+
assert_equal ['camelStyle'], result.keys
|
604
|
+
end
|
605
|
+
|
606
|
+
test 'key_format! with merge! deep' do
|
607
|
+
hash = { camel_style: { sub_attr: 'for JS' } }
|
608
|
+
result = jbuild do |json|
|
609
|
+
json.key_format! camelize: :lower
|
610
|
+
json.merge! hash
|
611
|
+
end
|
612
|
+
|
613
|
+
assert_equal ['subAttr'], result['camelStyle'].keys
|
614
|
+
end
|
615
|
+
|
616
|
+
test 'key_format! with set! array of hashes' do
|
617
|
+
names = [{ first_name: 'camel', last_name: 'case' }]
|
618
|
+
result = jbuild do |json|
|
619
|
+
json.key_format! camelize: :lower
|
620
|
+
json.set! :names, names
|
621
|
+
end
|
622
|
+
|
623
|
+
assert_equal %w[firstName lastName], result['names'][0].keys
|
624
|
+
end
|
625
|
+
|
626
|
+
test 'key_format! with array! of hashes' do
|
627
|
+
names = [{ first_name: 'camel', last_name: 'case' }]
|
628
|
+
result = jbuild do |json|
|
629
|
+
json.key_format! camelize: :lower
|
630
|
+
json.array! names
|
631
|
+
end
|
632
|
+
|
633
|
+
assert_equal %w[firstName lastName], result[0].keys
|
634
|
+
end
|
635
|
+
|
636
|
+
test 'key_format! with merge! array of hashes' do
|
637
|
+
names = [{ first_name: 'camel', last_name: 'case' }]
|
638
|
+
new_names = [{ first_name: 'snake', last_name: 'case' }]
|
639
|
+
result = jbuild do |json|
|
640
|
+
json.key_format! camelize: :lower
|
641
|
+
json.array! names
|
642
|
+
json.merge! new_names
|
643
|
+
end
|
644
|
+
|
645
|
+
assert_equal %w[firstName lastName], result[1].keys
|
646
|
+
end
|
647
|
+
|
584
648
|
test 'default key_format!' do
|
585
649
|
Jbuilder.key_format camelize: :lower
|
586
650
|
result = jbuild{ |json| json.camel_style 'for JS' }
|
@@ -55,5 +55,16 @@ if Rails::VERSION::MAJOR > 4
|
|
55
55
|
assert_match %r{params\.fetch\(:post, \{\}\)}, content
|
56
56
|
end
|
57
57
|
end
|
58
|
+
|
59
|
+
|
60
|
+
if Rails::VERSION::MAJOR >= 6
|
61
|
+
test 'handles virtual attributes' do
|
62
|
+
run_generator ["Message", "content:rich_text", "video:attachment", "photos:attachments"]
|
63
|
+
|
64
|
+
assert_file 'app/controllers/messages_controller.rb' do |content|
|
65
|
+
assert_match %r{params\.require\(:message\)\.permit\(:content, :video, photos: \[\]\)}, content
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
58
69
|
end
|
59
70
|
end
|
@@ -67,4 +67,14 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase
|
|
67
67
|
assert_match %r{params\.fetch\(:post, \{\}\)}, content
|
68
68
|
end
|
69
69
|
end
|
70
|
+
|
71
|
+
if Rails::VERSION::MAJOR >= 6
|
72
|
+
test 'handles virtual attributes' do
|
73
|
+
run_generator %w(Message content:rich_text video:attachment photos:attachments)
|
74
|
+
|
75
|
+
assert_file 'app/controllers/messages_controller.rb' do |content|
|
76
|
+
assert_match %r{params\.require\(:message\)\.permit\(:content, :video, photos: \[\]\)}, content
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
70
80
|
end
|