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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 977b17be8a840c956c4043e24671792174502c57de951e215873b80b66b1031d
4
- data.tar.gz: 847725cdbcfca6ba030942fa9d63b35b0fffd729587774c2dd020eaf75b0b786
3
+ metadata.gz: 8e18daf81b639ef039951269f0df24a2ffb0a4d6e93c05861eebef18c85a9039
4
+ data.tar.gz: db3ff93fbd28f3a67273b0afbae0962e5e2f49842901b3b2ade5bb3831b74d9b
5
5
  SHA512:
6
- metadata.gz: fcf001b86052df2bf45e8040630a6c3bcb9cb5ee5fee7910a636706fc7c05350a3a86feab542541f5b8290435fd8be61c8ca9cc17c1b1a3b87fb6626aeb9b495
7
- data.tar.gz: 44d59c94049d3f76c9b3111408aabe16631deb5ef80fbde72a6e43a2a5807c2da3e47725dc8974384212cba178a48b76bb61cd114433593ccfd0dc3311e92b07
6
+ metadata.gz: b74be66e7ba0559d14c7032bfff001e9c9a49c657edab95de06c289eb3600945dea14c3efd9595b0c7da524d31f21c407c8f2b4541da308be8921951f94103e3
7
+ data.tar.gz: 8ae3e52031fd58c5de35fae022c9224c116d13a93c85990f5385511a8a914ce488f41b69676f94b54d3442942f3a30f79e460fdc78d84633261d239654505547
@@ -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
@@ -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 Github](https://github.com/rails/jbuilder) and check out your copy.
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
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'jbuilder'
3
- s.version = '2.10.2'
3
+ s.version = '2.11.0'
4
4
  s.authors = 'David Heinemeier Hansson'
5
5
  s.email = 'david@basecamp.com'
6
6
  s.summary = 'Create JSON structures via a Builder-style DSL'
@@ -50,6 +50,10 @@ module Rails
50
50
 
51
51
  attributes.map { |a| ":#{a}"} * ', '
52
52
  end
53
+
54
+ def virtual_attributes
55
+ attributes.select {|name| name.respond_to?(:virtual?) && name.virtual? }
56
+ end
53
57
  end
54
58
  end
55
59
  end
@@ -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 -%>
@@ -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 array into current builder.
245
- def merge!(hash_or_array)
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
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jbuilder
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.10.2
4
+ version: 2.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Heinemeier Hansson