jbuilder 2.11.0 → 2.11.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ruby.yml +108 -0
- data/Appraisals +5 -1
- data/CHANGELOG.md +11 -0
- data/CONTRIBUTING.md +3 -3
- data/README.md +73 -9
- data/Rakefile +1 -1
- data/gemfiles/rails_6_1.gemfile +10 -0
- data/gemfiles/rails_head.gemfile +1 -1
- data/jbuilder.gemspec +1 -1
- data/lib/generators/rails/templates/api_controller.rb +1 -1
- data/lib/generators/rails/templates/controller.rb +9 -13
- data/lib/jbuilder/collection_renderer.rb +108 -0
- data/lib/jbuilder/jbuilder_template.rb +54 -7
- data/lib/jbuilder.rb +39 -10
- data/test/jbuilder_dependency_tracker_test.rb +1 -1
- data/test/jbuilder_template_test.rb +100 -2
- data/test/jbuilder_test.rb +139 -6
- data/test/scaffold_api_controller_generator_test.rb +1 -1
- data/test/scaffold_controller_generator_test.rb +22 -3
- data/test/test_helper.rb +7 -4
- metadata +10 -8
- data/.travis.yml +0 -52
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 071e52598986c430c4bc79a185a0229eceb11c3f38f12ee5a6bd6f9c16f7008c
|
4
|
+
data.tar.gz: 73e82bf90e7049fb735ba139521daf1e0291fbd8e704a1523f7aa4f97e395185
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fca112fc608e7fee45bc1b90ddb5430205d462955f458d1c5fff0d3d716a40ddd7cfc3383465f2eb727b82bfb3b1b226508df7ae77779c3655661324ee8d4963
|
7
|
+
data.tar.gz: 5a250b8e997d56eeeef8b3630cbdc98a6b88e78c06cd3524fb0f5518939ed10ff38bcb9dcc9c8470925f881c5b0690298af019ff16db4a162bee3207f5e6bfd6
|
@@ -0,0 +1,108 @@
|
|
1
|
+
name: Ruby test
|
2
|
+
|
3
|
+
on: [push, pull_request]
|
4
|
+
|
5
|
+
jobs:
|
6
|
+
test:
|
7
|
+
name: Ruby ${{ matrix.ruby }} (${{ matrix.gemfile }})
|
8
|
+
runs-on: ubuntu-20.04
|
9
|
+
continue-on-error: ${{ matrix.experimental }}
|
10
|
+
env:
|
11
|
+
BUNDLE_GEMFILE: ${{ github.workspace }}/gemfiles/${{ matrix.gemfile }}.gemfile
|
12
|
+
BUNDLE_JOBS: 4
|
13
|
+
BUNDLE_RETRY: 3
|
14
|
+
strategy:
|
15
|
+
fail-fast: false
|
16
|
+
matrix:
|
17
|
+
ruby:
|
18
|
+
- "2.2"
|
19
|
+
- "2.3"
|
20
|
+
- "2.4"
|
21
|
+
- "2.5"
|
22
|
+
- "2.6"
|
23
|
+
- "2.7"
|
24
|
+
- "3.0"
|
25
|
+
|
26
|
+
gemfile:
|
27
|
+
- "rails_5_0"
|
28
|
+
- "rails_5_1"
|
29
|
+
- "rails_5_2"
|
30
|
+
- "rails_6_0"
|
31
|
+
- "rails_6_1"
|
32
|
+
- "rails_head"
|
33
|
+
|
34
|
+
experimental: [false]
|
35
|
+
exclude:
|
36
|
+
- ruby: 2.7
|
37
|
+
gemfile: rails_5_0
|
38
|
+
- ruby: '3.0'
|
39
|
+
gemfile: rails_5_0
|
40
|
+
- ruby: head
|
41
|
+
gemfile: rails_5_0
|
42
|
+
- ruby: 2.7
|
43
|
+
gemfile: rails_5_1
|
44
|
+
- ruby: '3.0'
|
45
|
+
gemfile: rails_5_1
|
46
|
+
- ruby: head
|
47
|
+
gemfile: rails_5_1
|
48
|
+
- ruby: 2.2
|
49
|
+
gemfile: rails_5_2
|
50
|
+
- ruby: 2.7
|
51
|
+
gemfile: rails_5_2
|
52
|
+
- ruby: '3.0'
|
53
|
+
gemfile: rails_5_2
|
54
|
+
- ruby: head
|
55
|
+
gemfile: rails_5_2
|
56
|
+
- ruby: 2.2
|
57
|
+
gemfile: rails_6_0
|
58
|
+
- ruby: 2.3
|
59
|
+
gemfile: rails_6_0
|
60
|
+
- ruby: 2.4
|
61
|
+
gemfile: rails_6_0
|
62
|
+
- ruby: '3.0'
|
63
|
+
gemfile: rails_6_0
|
64
|
+
- ruby: head
|
65
|
+
gemfile: rails_6_0
|
66
|
+
- ruby: 2.2
|
67
|
+
gemfile: rails_6_1
|
68
|
+
- ruby: 2.3
|
69
|
+
gemfile: rails_6_1
|
70
|
+
- ruby: 2.4
|
71
|
+
gemfile: rails_6_1
|
72
|
+
- ruby: 2.2
|
73
|
+
gemfile: rails_head
|
74
|
+
- ruby: 2.3
|
75
|
+
gemfile: rails_head
|
76
|
+
- ruby: 2.4
|
77
|
+
gemfile: rails_head
|
78
|
+
- ruby: 2.5
|
79
|
+
gemfile: rails_head
|
80
|
+
- ruby: 2.6
|
81
|
+
gemfile: rails_head
|
82
|
+
- ruby: 2.7
|
83
|
+
gemfile: rails_head
|
84
|
+
experimental: false
|
85
|
+
- ruby: '3.0'
|
86
|
+
gemfile: rails_head
|
87
|
+
experimental: false
|
88
|
+
include:
|
89
|
+
- ruby: 2.7
|
90
|
+
gemfile: rails_head
|
91
|
+
experimental: true
|
92
|
+
- ruby: '3.0'
|
93
|
+
gemfile: rails_head
|
94
|
+
experimental: true
|
95
|
+
- ruby: head
|
96
|
+
gemfile: rails_head
|
97
|
+
experimental: true
|
98
|
+
|
99
|
+
steps:
|
100
|
+
- uses: actions/checkout@v2
|
101
|
+
|
102
|
+
- uses: ruby/setup-ruby@v1
|
103
|
+
with:
|
104
|
+
ruby-version: ${{ matrix.ruby }}
|
105
|
+
bundler-cache: true
|
106
|
+
|
107
|
+
- name: Ruby test
|
108
|
+
run: bundle exec rake
|
data/Appraisals
CHANGED
@@ -15,7 +15,11 @@ if RUBY_VERSION >= "2.5.0"
|
|
15
15
|
gem "rails", "~> 6.0.0"
|
16
16
|
end
|
17
17
|
|
18
|
+
appraise "rails-6-1" do
|
19
|
+
gem "rails", "~> 6.1.0"
|
20
|
+
end
|
21
|
+
|
18
22
|
appraise "rails-head" do
|
19
|
-
gem "rails", github: "rails/rails"
|
23
|
+
gem "rails", github: "rails/rails", branch: "main"
|
20
24
|
end
|
21
25
|
end
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,16 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
2.11.2
|
4
|
+
------
|
5
|
+
|
6
|
+
* [Improve key formatting for nested hashes and disable by default](https://github.com/rails/jbuilder/pull/497)
|
7
|
+
|
8
|
+
2.11.1
|
9
|
+
------
|
10
|
+
|
11
|
+
* Use symbols instead of strings for before_action filters [DHH]
|
12
|
+
* Slim down comments in generated scaffold code [DHH]
|
13
|
+
|
3
14
|
2.11.0
|
4
15
|
------
|
5
16
|
|
data/CONTRIBUTING.md
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
Contributing to Jbuilder
|
2
2
|
=====================
|
3
3
|
|
4
|
-
[![Build Status](https://
|
4
|
+
[![Build Status](https://github.com/rails/jbuilder/workflows/Ruby%20test/badge.svg)][test]
|
5
5
|
[![Gem Version](https://badge.fury.io/rb/jbuilder.svg)][gem]
|
6
6
|
[![Code Climate](https://codeclimate.com/github/rails/jbuilder/badges/gpa.svg)][codeclimate]
|
7
7
|
|
8
|
-
[
|
8
|
+
[test]: https://github.com/rails/jbuilder/actions?query=branch%3Amaster
|
9
9
|
[gem]: https://rubygems.org/gems/jbuilder
|
10
10
|
[codeclimate]: https://codeclimate.com/github/rails/jbuilder
|
11
11
|
|
@@ -95,7 +95,7 @@ git push origin my-feature-branch -f
|
|
95
95
|
|
96
96
|
#### Check on Your Pull Request
|
97
97
|
|
98
|
-
Go back to your pull request after a few minutes and see whether it passed muster with
|
98
|
+
Go back to your pull request after a few minutes and see whether it passed muster with GitHub Actions. Everything should look green, otherwise fix issues and amend your commit as described above.
|
99
99
|
|
100
100
|
#### Be Patient
|
101
101
|
|
data/README.md
CHANGED
@@ -108,6 +108,33 @@ json.array! @people, :id, :name
|
|
108
108
|
# => [ { "id": 1, "name": "David" }, { "id": 2, "name": "Jamie" } ]
|
109
109
|
```
|
110
110
|
|
111
|
+
To make a plain array without keys, construct and pass in a standard Ruby array.
|
112
|
+
|
113
|
+
```ruby
|
114
|
+
my_array = %w(David Jamie)
|
115
|
+
|
116
|
+
json.people my_array
|
117
|
+
|
118
|
+
# => "people": [ "David", "Jamie" ]
|
119
|
+
```
|
120
|
+
|
121
|
+
You don't always have or need a collection when building an array.
|
122
|
+
|
123
|
+
```ruby
|
124
|
+
json.people do
|
125
|
+
json.child! do
|
126
|
+
json.id 1
|
127
|
+
json.name 'David'
|
128
|
+
end
|
129
|
+
json.child! do
|
130
|
+
json.id 2
|
131
|
+
json.name 'Jamie'
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
# => { "people": [ { "id": 1, "name": "David" }, { "id": 2, "name": "Jamie" } ] }
|
136
|
+
```
|
137
|
+
|
111
138
|
Jbuilder objects can be directly nested inside each other. Useful for composing objects.
|
112
139
|
|
113
140
|
``` ruby
|
@@ -137,7 +164,7 @@ company.to_builder.target!
|
|
137
164
|
```
|
138
165
|
|
139
166
|
You can either use Jbuilder stand-alone or directly as an ActionView template
|
140
|
-
language. When required in Rails, you can create views
|
167
|
+
language. When required in Rails, you can create views à la show.json.jbuilder
|
141
168
|
(the json is already yielded):
|
142
169
|
|
143
170
|
``` ruby
|
@@ -171,19 +198,19 @@ It's also possible to render collections of partials:
|
|
171
198
|
json.array! @posts, partial: 'posts/post', as: :post
|
172
199
|
|
173
200
|
# or
|
174
|
-
|
175
201
|
json.partial! 'posts/post', collection: @posts, as: :post
|
176
202
|
|
177
203
|
# or
|
178
|
-
|
179
204
|
json.partial! partial: 'posts/post', collection: @posts, as: :post
|
180
205
|
|
181
206
|
# or
|
182
|
-
|
183
207
|
json.comments @post.comments, partial: 'comments/comment', as: :comment
|
184
208
|
```
|
185
209
|
|
186
|
-
The `as: :some_symbol` is used with partials. It will take care of mapping the passed in object to a variable for the
|
210
|
+
The `as: :some_symbol` is used with partials. It will take care of mapping the passed in object to a variable for the
|
211
|
+
partial. If the value is a collection either implicitly or explicitly by using the `collection:` option, then each
|
212
|
+
value of the collection is passed to the partial as the variable `some_symbol`. If the value is a singular object,
|
213
|
+
then the object is passed to the partial as the variable `some_symbol`.
|
187
214
|
|
188
215
|
Be sure not to confuse the `as:` option to mean nesting of the partial. For example:
|
189
216
|
|
@@ -193,7 +220,7 @@ Be sure not to confuse the `as:` option to mean nesting of the partial. For exam
|
|
193
220
|
json.partial! @comment, as: :comment
|
194
221
|
```
|
195
222
|
|
196
|
-
is quite different
|
223
|
+
is quite different from:
|
197
224
|
|
198
225
|
```ruby
|
199
226
|
# comment attributes are nested under a "comment" property
|
@@ -236,6 +263,8 @@ json.bar "bar"
|
|
236
263
|
# => { "bar": "bar" }
|
237
264
|
```
|
238
265
|
|
266
|
+
## Caching
|
267
|
+
|
239
268
|
Fragment caching is supported, it uses `Rails.cache` and works like caching in
|
240
269
|
HTML templates:
|
241
270
|
|
@@ -253,9 +282,25 @@ json.cache_if! !admin?, ['v1', @person], expires_in: 10.minutes do
|
|
253
282
|
end
|
254
283
|
```
|
255
284
|
|
256
|
-
|
257
|
-
|
258
|
-
|
285
|
+
Aside from that, the `:cached` options on collection rendering is available on Rails >= 6.0. This will cache the
|
286
|
+
rendered results effectively using the multi fetch feature.
|
287
|
+
|
288
|
+
```ruby
|
289
|
+
json.array! @posts, partial: "posts/post", as: :post, cached: true
|
290
|
+
|
291
|
+
# or:
|
292
|
+
json.comments @post.comments, partial: "comments/comment", as: :comment, cached: true
|
293
|
+
```
|
294
|
+
|
295
|
+
If your collection cache depends on multiple sources (try to avoid this to keep things simple), you can name all these dependencies as part of a block that returns an array:
|
296
|
+
|
297
|
+
```ruby
|
298
|
+
json.array! @posts, partial: "posts/post", as: :post, cached: -> post { [post, current_user] }
|
299
|
+
```
|
300
|
+
|
301
|
+
This will include both records as part of the cache key and updating either of them will expire the cache.
|
302
|
+
|
303
|
+
## Formatting Keys
|
259
304
|
|
260
305
|
Keys can be auto formatted using `key_format!`, this can be used to convert
|
261
306
|
keynames from the standard ruby_format to camelCase:
|
@@ -274,6 +319,25 @@ environment.rb for example):
|
|
274
319
|
Jbuilder.key_format camelize: :lower
|
275
320
|
```
|
276
321
|
|
322
|
+
By default, key format is not applied to keys of hashes that are
|
323
|
+
passed to methods like `set!`, `array!` or `merge!`. You can opt into
|
324
|
+
deeply transforming these as well:
|
325
|
+
|
326
|
+
``` ruby
|
327
|
+
json.key_format! camelize: :lower
|
328
|
+
json.deep_format_keys!
|
329
|
+
json.settings([{some_value: "abc"}])
|
330
|
+
|
331
|
+
# => { "settings": [{ "someValue": "abc" }]}
|
332
|
+
```
|
333
|
+
|
334
|
+
You can set this globally with the class method `deep_format_keys` (from inside your
|
335
|
+
environment.rb for example):
|
336
|
+
|
337
|
+
``` ruby
|
338
|
+
Jbuilder.deep_format_keys true
|
339
|
+
```
|
340
|
+
|
277
341
|
## Contributing to Jbuilder
|
278
342
|
|
279
343
|
Jbuilder is the work of many contributors. You're encouraged to submit pull requests, propose
|
data/Rakefile
CHANGED
data/gemfiles/rails_head.gemfile
CHANGED
data/jbuilder.gemspec
CHANGED
@@ -4,7 +4,7 @@ require_dependency "<%= namespaced_path %>/application_controller"
|
|
4
4
|
<% end -%>
|
5
5
|
<% module_namespacing do -%>
|
6
6
|
class <%= controller_class_name %>Controller < ApplicationController
|
7
|
-
before_action :set_<%= singular_table_name %>, only: %
|
7
|
+
before_action :set_<%= singular_table_name %>, only: %i[ show update destroy ]
|
8
8
|
|
9
9
|
# GET <%= route_url %>
|
10
10
|
# GET <%= route_url %>.json
|
@@ -4,16 +4,14 @@ require_dependency "<%= namespaced_path %>/application_controller"
|
|
4
4
|
<% end -%>
|
5
5
|
<% module_namespacing do -%>
|
6
6
|
class <%= controller_class_name %>Controller < ApplicationController
|
7
|
-
before_action :set_<%= singular_table_name %>, only: %
|
7
|
+
before_action :set_<%= singular_table_name %>, only: %i[ show edit update destroy ]
|
8
8
|
|
9
|
-
# GET <%= route_url %>
|
10
|
-
# GET <%= route_url %>.json
|
9
|
+
# GET <%= route_url %> or <%= route_url %>.json
|
11
10
|
def index
|
12
11
|
@<%= plural_table_name %> = <%= orm_class.all(class_name) %>
|
13
12
|
end
|
14
13
|
|
15
|
-
# GET <%= route_url %>/1
|
16
|
-
# GET <%= route_url %>/1.json
|
14
|
+
# GET <%= route_url %>/1 or <%= route_url %>/1.json
|
17
15
|
def show
|
18
16
|
end
|
19
17
|
|
@@ -26,14 +24,13 @@ class <%= controller_class_name %>Controller < ApplicationController
|
|
26
24
|
def edit
|
27
25
|
end
|
28
26
|
|
29
|
-
# POST <%= route_url %>
|
30
|
-
# POST <%= route_url %>.json
|
27
|
+
# POST <%= route_url %> or <%= route_url %>.json
|
31
28
|
def create
|
32
29
|
@<%= singular_table_name %> = <%= orm_class.build(class_name, "#{singular_table_name}_params") %>
|
33
30
|
|
34
31
|
respond_to do |format|
|
35
32
|
if @<%= orm_instance.save %>
|
36
|
-
format.html { redirect_to
|
33
|
+
format.html { redirect_to <%= show_helper %>, notice: <%= %("#{human_name} was successfully created.") %> }
|
37
34
|
format.json { render :show, status: :created, location: <%= "@#{singular_table_name}" %> }
|
38
35
|
else
|
39
36
|
format.html { render :new, status: :unprocessable_entity }
|
@@ -42,12 +39,11 @@ class <%= controller_class_name %>Controller < ApplicationController
|
|
42
39
|
end
|
43
40
|
end
|
44
41
|
|
45
|
-
# PATCH/PUT <%= route_url %>/1
|
46
|
-
# PATCH/PUT <%= route_url %>/1.json
|
42
|
+
# PATCH/PUT <%= route_url %>/1 or <%= route_url %>/1.json
|
47
43
|
def update
|
48
44
|
respond_to do |format|
|
49
45
|
if @<%= orm_instance.update("#{singular_table_name}_params") %>
|
50
|
-
format.html { redirect_to
|
46
|
+
format.html { redirect_to <%= show_helper %>, notice: <%= %("#{human_name} was successfully updated.") %> }
|
51
47
|
format.json { render :show, status: :ok, location: <%= "@#{singular_table_name}" %> }
|
52
48
|
else
|
53
49
|
format.html { render :edit, status: :unprocessable_entity }
|
@@ -56,10 +52,10 @@ class <%= controller_class_name %>Controller < ApplicationController
|
|
56
52
|
end
|
57
53
|
end
|
58
54
|
|
59
|
-
# DELETE <%= route_url %>/1
|
60
|
-
# DELETE <%= route_url %>/1.json
|
55
|
+
# DELETE <%= route_url %>/1 or <%= route_url %>/1.json
|
61
56
|
def destroy
|
62
57
|
@<%= orm_instance.destroy %>
|
58
|
+
|
63
59
|
respond_to do |format|
|
64
60
|
format.html { redirect_to <%= index_helper %>_url, notice: <%= %("#{human_name} was successfully destroyed.") %> }
|
65
61
|
format.json { head :no_content }
|
@@ -0,0 +1,108 @@
|
|
1
|
+
require 'delegate'
|
2
|
+
require 'active_support/concern'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'action_view/renderer/collection_renderer'
|
6
|
+
rescue LoadError
|
7
|
+
require 'action_view/renderer/partial_renderer'
|
8
|
+
end
|
9
|
+
|
10
|
+
class Jbuilder
|
11
|
+
module CollectionRenderable # :nodoc:
|
12
|
+
extend ActiveSupport::Concern
|
13
|
+
|
14
|
+
class_methods do
|
15
|
+
def supported?
|
16
|
+
superclass.private_method_defined?(:build_rendered_template) && self.superclass.private_method_defined?(:build_rendered_collection)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def build_rendered_template(content, template, layout = nil)
|
23
|
+
super(content || json.attributes!, template)
|
24
|
+
end
|
25
|
+
|
26
|
+
def build_rendered_collection(templates, _spacer)
|
27
|
+
json.merge!(templates.map(&:body))
|
28
|
+
end
|
29
|
+
|
30
|
+
def json
|
31
|
+
@options[:locals].fetch(:json)
|
32
|
+
end
|
33
|
+
|
34
|
+
class ScopedIterator < ::SimpleDelegator # :nodoc:
|
35
|
+
include Enumerable
|
36
|
+
|
37
|
+
def initialize(obj, scope)
|
38
|
+
super(obj)
|
39
|
+
@scope = scope
|
40
|
+
end
|
41
|
+
|
42
|
+
# Rails 6.0 support:
|
43
|
+
def each
|
44
|
+
return enum_for(:each) unless block_given?
|
45
|
+
|
46
|
+
__getobj__.each do |object|
|
47
|
+
@scope.call { yield(object) }
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
# Rails 6.1 support:
|
52
|
+
def each_with_info
|
53
|
+
return enum_for(:each_with_info) unless block_given?
|
54
|
+
|
55
|
+
__getobj__.each_with_info do |object, info|
|
56
|
+
@scope.call { yield(object, info) }
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
private_constant :ScopedIterator
|
62
|
+
end
|
63
|
+
|
64
|
+
if defined?(::ActionView::CollectionRenderer)
|
65
|
+
# Rails 6.1 support:
|
66
|
+
class CollectionRenderer < ::ActionView::CollectionRenderer # :nodoc:
|
67
|
+
include CollectionRenderable
|
68
|
+
|
69
|
+
def initialize(lookup_context, options, &scope)
|
70
|
+
super(lookup_context, options)
|
71
|
+
@scope = scope
|
72
|
+
end
|
73
|
+
|
74
|
+
private
|
75
|
+
def collection_with_template(view, template, layout, collection)
|
76
|
+
super(view, template, layout, ScopedIterator.new(collection, @scope))
|
77
|
+
end
|
78
|
+
end
|
79
|
+
else
|
80
|
+
# Rails 6.0 support:
|
81
|
+
class CollectionRenderer < ::ActionView::PartialRenderer # :nodoc:
|
82
|
+
include CollectionRenderable
|
83
|
+
|
84
|
+
def initialize(lookup_context, options, &scope)
|
85
|
+
super(lookup_context)
|
86
|
+
@options = options
|
87
|
+
@scope = scope
|
88
|
+
end
|
89
|
+
|
90
|
+
def render_collection_with_partial(collection, partial, context, block)
|
91
|
+
render(context, @options.merge(collection: collection, partial: partial), block)
|
92
|
+
end
|
93
|
+
|
94
|
+
private
|
95
|
+
def collection_without_template(view)
|
96
|
+
@collection = ScopedIterator.new(@collection, @scope)
|
97
|
+
|
98
|
+
super(view)
|
99
|
+
end
|
100
|
+
|
101
|
+
def collection_with_template(view, template)
|
102
|
+
@collection = ScopedIterator.new(@collection, @scope)
|
103
|
+
|
104
|
+
super(view, template)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'jbuilder/jbuilder'
|
2
|
+
require 'jbuilder/collection_renderer'
|
2
3
|
require 'action_dispatch/http/mime_type'
|
3
4
|
require 'active_support/cache'
|
4
5
|
|
@@ -15,6 +16,38 @@ class JbuilderTemplate < Jbuilder
|
|
15
16
|
super(*args)
|
16
17
|
end
|
17
18
|
|
19
|
+
# Generates JSON using the template specified with the `:partial` option. For example, the code below will render
|
20
|
+
# the file `views/comments/_comments.json.jbuilder`, and set a local variable comments with all this message's
|
21
|
+
# comments, which can be used inside the partial.
|
22
|
+
#
|
23
|
+
# Example:
|
24
|
+
#
|
25
|
+
# json.partial! 'comments/comments', comments: @message.comments
|
26
|
+
#
|
27
|
+
# There are multiple ways to generate a collection of elements as JSON, as ilustrated below:
|
28
|
+
#
|
29
|
+
# Example:
|
30
|
+
#
|
31
|
+
# json.array! @posts, partial: 'posts/post', as: :post
|
32
|
+
#
|
33
|
+
# # or:
|
34
|
+
# json.partial! 'posts/post', collection: @posts, as: :post
|
35
|
+
#
|
36
|
+
# # or:
|
37
|
+
# json.partial! partial: 'posts/post', collection: @posts, as: :post
|
38
|
+
#
|
39
|
+
# # or:
|
40
|
+
# json.comments @post.comments, partial: 'comments/comment', as: :comment
|
41
|
+
#
|
42
|
+
# Aside from that, the `:cached` options is available on Rails >= 6.0. This will cache the rendered results
|
43
|
+
# effectively using the multi fetch feature.
|
44
|
+
#
|
45
|
+
# Example:
|
46
|
+
#
|
47
|
+
# json.array! @posts, partial: "posts/post", as: :post, cached: true
|
48
|
+
#
|
49
|
+
# json.comments @post.comments, partial: "comments/comment", as: :comment, cached: true
|
50
|
+
#
|
18
51
|
def partial!(*args)
|
19
52
|
if args.one? && _is_active_model?(args.first)
|
20
53
|
_render_active_model_partial args.first
|
@@ -104,11 +137,30 @@ class JbuilderTemplate < Jbuilder
|
|
104
137
|
private
|
105
138
|
|
106
139
|
def _render_partial_with_options(options)
|
107
|
-
options.reverse_merge! locals: options.except(:partial, :as, :collection)
|
140
|
+
options.reverse_merge! locals: options.except(:partial, :as, :collection, :cached)
|
108
141
|
options.reverse_merge! ::JbuilderTemplate.template_lookup_options
|
109
142
|
as = options[:as]
|
110
143
|
|
111
|
-
if as && options.key?(:collection)
|
144
|
+
if as && options.key?(:collection) && CollectionRenderer.supported?
|
145
|
+
collection = options.delete(:collection) || []
|
146
|
+
partial = options.delete(:partial)
|
147
|
+
options[:locals].merge!(json: self)
|
148
|
+
|
149
|
+
if options.has_key?(:layout)
|
150
|
+
raise ::NotImplementedError, "The `:layout' option is not supported in collection rendering."
|
151
|
+
end
|
152
|
+
|
153
|
+
if options.has_key?(:spacer_template)
|
154
|
+
raise ::NotImplementedError, "The `:spacer_template' option is not supported in collection rendering."
|
155
|
+
end
|
156
|
+
|
157
|
+
results = CollectionRenderer
|
158
|
+
.new(@context.lookup_context, options) { |&block| _scope(&block) }
|
159
|
+
.render_collection_with_partial(collection, partial, @context, nil)
|
160
|
+
|
161
|
+
array! if results.respond_to?(:body) && results.body.nil?
|
162
|
+
elsif as && options.key?(:collection) && !CollectionRenderer.supported?
|
163
|
+
# For Rails <= 5.2:
|
112
164
|
as = as.to_sym
|
113
165
|
collection = options.delete(:collection)
|
114
166
|
locals = options.delete(:locals)
|
@@ -162,12 +214,7 @@ class JbuilderTemplate < Jbuilder
|
|
162
214
|
|
163
215
|
def _fragment_name_with_digest(key, options)
|
164
216
|
if @context.respond_to?(:cache_fragment_name)
|
165
|
-
# Current compatibility, fragment_name_with_digest is private again and cache_fragment_name
|
166
|
-
# should be used instead.
|
167
217
|
@context.cache_fragment_name(key, **options)
|
168
|
-
elsif @context.respond_to?(:fragment_name_with_digest)
|
169
|
-
# Backwards compatibility for period of time when fragment_name_with_digest was made public.
|
170
|
-
@context.fragment_name_with_digest(key)
|
171
218
|
else
|
172
219
|
key
|
173
220
|
end
|
data/lib/jbuilder.rb
CHANGED
@@ -9,12 +9,14 @@ require 'active_support/core_ext/hash/deep_merge'
|
|
9
9
|
class Jbuilder
|
10
10
|
@@key_formatter = nil
|
11
11
|
@@ignore_nil = false
|
12
|
+
@@deep_format_keys = false
|
12
13
|
|
13
14
|
def initialize(options = {})
|
14
15
|
@attributes = {}
|
15
16
|
|
16
17
|
@key_formatter = options.fetch(:key_formatter){ @@key_formatter ? @@key_formatter.clone : nil}
|
17
18
|
@ignore_nil = options.fetch(:ignore_nil, @@ignore_nil)
|
19
|
+
@deep_format_keys = options.fetch(:deep_format_keys, @@deep_format_keys)
|
18
20
|
|
19
21
|
yield self if ::Kernel.block_given?
|
20
22
|
end
|
@@ -131,6 +133,31 @@ class Jbuilder
|
|
131
133
|
@@ignore_nil = value
|
132
134
|
end
|
133
135
|
|
136
|
+
# Deeply apply key format to nested hashes and arrays passed to
|
137
|
+
# methods like set!, merge! or array!.
|
138
|
+
#
|
139
|
+
# Example:
|
140
|
+
#
|
141
|
+
# json.key_format! camelize: :lower
|
142
|
+
# json.settings({some_value: "abc"})
|
143
|
+
#
|
144
|
+
# { "settings": { "some_value": "abc" }}
|
145
|
+
#
|
146
|
+
# json.key_format! camelize: :lower
|
147
|
+
# json.deep_format_keys!
|
148
|
+
# json.settings({some_value: "abc"})
|
149
|
+
#
|
150
|
+
# { "settings": { "someValue": "abc" }}
|
151
|
+
#
|
152
|
+
def deep_format_keys!(value = true)
|
153
|
+
@deep_format_keys = value
|
154
|
+
end
|
155
|
+
|
156
|
+
# Same as instance method deep_format_keys! except sets the default.
|
157
|
+
def self.deep_format_keys(value = true)
|
158
|
+
@@deep_format_keys = value
|
159
|
+
end
|
160
|
+
|
134
161
|
# Turns the current element into an array and yields a builder to add a hash.
|
135
162
|
#
|
136
163
|
# Example:
|
@@ -190,10 +217,10 @@ class Jbuilder
|
|
190
217
|
elsif attributes.any?
|
191
218
|
_map_collection(collection) { |element| extract! element, *attributes }
|
192
219
|
else
|
193
|
-
collection.to_a
|
220
|
+
_format_keys(collection.to_a)
|
194
221
|
end
|
195
222
|
|
196
|
-
|
223
|
+
@attributes = _merge_values(@attributes, array)
|
197
224
|
end
|
198
225
|
|
199
226
|
# Extracts the mentioned attributes or hash elements from the passed object and turns them into attributes of the JSON.
|
@@ -244,7 +271,7 @@ class Jbuilder
|
|
244
271
|
# Merges hash, array, or Jbuilder instance into current builder.
|
245
272
|
def merge!(object)
|
246
273
|
hash_or_array = ::Jbuilder === object ? object.attributes! : object
|
247
|
-
@attributes = _merge_values(@attributes, hash_or_array)
|
274
|
+
@attributes = _merge_values(@attributes, _format_keys(hash_or_array))
|
248
275
|
end
|
249
276
|
|
250
277
|
# Encodes the current builder as JSON.
|
@@ -255,11 +282,11 @@ class Jbuilder
|
|
255
282
|
private
|
256
283
|
|
257
284
|
def _extract_hash_values(object, attributes)
|
258
|
-
attributes.each{ |key| _set_value key, object.fetch(key) }
|
285
|
+
attributes.each{ |key| _set_value key, _format_keys(object.fetch(key)) }
|
259
286
|
end
|
260
287
|
|
261
288
|
def _extract_method_values(object, attributes)
|
262
|
-
attributes.each{ |key| _set_value key, object.public_send(key) }
|
289
|
+
attributes.each{ |key| _set_value key, _format_keys(object.public_send(key)) }
|
263
290
|
end
|
264
291
|
|
265
292
|
def _merge_block(key)
|
@@ -273,11 +300,11 @@ class Jbuilder
|
|
273
300
|
if _blank?(updates)
|
274
301
|
current_value
|
275
302
|
elsif _blank?(current_value) || updates.nil? || current_value.empty? && ::Array === updates
|
276
|
-
|
303
|
+
updates
|
277
304
|
elsif ::Array === current_value && ::Array === updates
|
278
|
-
current_value +
|
305
|
+
current_value + updates
|
279
306
|
elsif ::Hash === current_value && ::Hash === updates
|
280
|
-
current_value.deep_merge(
|
307
|
+
current_value.deep_merge(updates)
|
281
308
|
else
|
282
309
|
raise MergeError.build(current_value, updates)
|
283
310
|
end
|
@@ -288,6 +315,8 @@ class Jbuilder
|
|
288
315
|
end
|
289
316
|
|
290
317
|
def _format_keys(hash_or_array)
|
318
|
+
return hash_or_array unless @deep_format_keys
|
319
|
+
|
291
320
|
if ::Array === hash_or_array
|
292
321
|
hash_or_array.map { |value| _format_keys(value) }
|
293
322
|
elsif ::Hash === hash_or_array
|
@@ -312,12 +341,12 @@ class Jbuilder
|
|
312
341
|
end
|
313
342
|
|
314
343
|
def _scope
|
315
|
-
parent_attributes, parent_formatter = @attributes, @key_formatter
|
344
|
+
parent_attributes, parent_formatter, parent_deep_format_keys = @attributes, @key_formatter, @deep_format_keys
|
316
345
|
@attributes = BLANK
|
317
346
|
yield
|
318
347
|
@attributes
|
319
348
|
ensure
|
320
|
-
@attributes, @key_formatter = parent_attributes, parent_formatter
|
349
|
+
@attributes, @key_formatter, @deep_format_keys = parent_attributes, parent_formatter, parent_deep_format_keys
|
321
350
|
end
|
322
351
|
|
323
352
|
def _is_collection?(object)
|
@@ -61,7 +61,7 @@ class JbuilderDependencyTrackerTest < ActiveSupport::TestCase
|
|
61
61
|
assert_equal %w[comments/comment], dependencies
|
62
62
|
end
|
63
63
|
|
64
|
-
test 'detects explicit
|
64
|
+
test 'detects explicit dependency' do
|
65
65
|
dependencies = track_dependencies <<-RUBY
|
66
66
|
# Template Dependency: path/to/partial
|
67
67
|
json.foo 'bar'
|
@@ -159,7 +159,7 @@ class JbuilderTemplateTest < ActiveSupport::TestCase
|
|
159
159
|
end
|
160
160
|
|
161
161
|
test "object fragment caching with expiry" do
|
162
|
-
travel_to "2018-05-
|
162
|
+
travel_to Time.iso8601("2018-05-12T11:29:00-04:00")
|
163
163
|
|
164
164
|
render <<-JBUILDER
|
165
165
|
json.cache! "cache-key", expires_in: 1.minute do
|
@@ -283,6 +283,101 @@ class JbuilderTemplateTest < ActiveSupport::TestCase
|
|
283
283
|
assert_equal "David", result["firstName"]
|
284
284
|
end
|
285
285
|
|
286
|
+
if JbuilderTemplate::CollectionRenderer.supported?
|
287
|
+
test "returns an empty array for an empty collection" do
|
288
|
+
result = render('json.array! @posts, partial: "post", as: :post, cached: true', posts: [])
|
289
|
+
|
290
|
+
# Do not use #assert_empty as it is important to ensure that the type of the JSON result is an array.
|
291
|
+
assert_equal [], result
|
292
|
+
end
|
293
|
+
|
294
|
+
test "works with an enumerable object" do
|
295
|
+
enumerable_class = Class.new do
|
296
|
+
include Enumerable
|
297
|
+
alias length count # Rails 6.1 requires this.
|
298
|
+
|
299
|
+
def each(&block)
|
300
|
+
[].each(&block)
|
301
|
+
end
|
302
|
+
end
|
303
|
+
|
304
|
+
result = render('json.array! @posts, partial: "post", as: :post, cached: true', posts: enumerable_class.new)
|
305
|
+
|
306
|
+
# Do not use #assert_empty as it is important to ensure that the type of the JSON result is an array.
|
307
|
+
assert_equal [], result
|
308
|
+
end
|
309
|
+
|
310
|
+
test "supports the cached: true option" do
|
311
|
+
result = render('json.array! @posts, partial: "post", as: :post, cached: true', posts: POSTS)
|
312
|
+
|
313
|
+
assert_equal 10, result.count
|
314
|
+
assert_equal "Post #5", result[4]["body"]
|
315
|
+
assert_equal "Heinemeier Hansson", result[2]["author"]["last_name"]
|
316
|
+
assert_equal "Pavel", result[5]["author"]["first_name"]
|
317
|
+
|
318
|
+
expected = {
|
319
|
+
"id" => 1,
|
320
|
+
"body" => "Post #1",
|
321
|
+
"author" => {
|
322
|
+
"first_name" => "David",
|
323
|
+
"last_name" => "Heinemeier Hansson"
|
324
|
+
}
|
325
|
+
}
|
326
|
+
|
327
|
+
assert_equal expected, Rails.cache.read("post-1")
|
328
|
+
|
329
|
+
result = render('json.array! @posts, partial: "post", as: :post, cached: true', posts: POSTS)
|
330
|
+
|
331
|
+
assert_equal 10, result.count
|
332
|
+
assert_equal "Post #5", result[4]["body"]
|
333
|
+
assert_equal "Heinemeier Hansson", result[2]["author"]["last_name"]
|
334
|
+
assert_equal "Pavel", result[5]["author"]["first_name"]
|
335
|
+
end
|
336
|
+
|
337
|
+
test "supports the cached: ->() {} option" do
|
338
|
+
result = render('json.array! @posts, partial: "post", as: :post, cached: ->(post) { [post, "foo"] }', posts: POSTS)
|
339
|
+
|
340
|
+
assert_equal 10, result.count
|
341
|
+
assert_equal "Post #5", result[4]["body"]
|
342
|
+
assert_equal "Heinemeier Hansson", result[2]["author"]["last_name"]
|
343
|
+
assert_equal "Pavel", result[5]["author"]["first_name"]
|
344
|
+
|
345
|
+
expected = {
|
346
|
+
"id" => 1,
|
347
|
+
"body" => "Post #1",
|
348
|
+
"author" => {
|
349
|
+
"first_name" => "David",
|
350
|
+
"last_name" => "Heinemeier Hansson"
|
351
|
+
}
|
352
|
+
}
|
353
|
+
|
354
|
+
assert_equal expected, Rails.cache.read("post-1/foo")
|
355
|
+
|
356
|
+
result = render('json.array! @posts, partial: "post", as: :post, cached: ->(post) { [post, "foo"] }', posts: POSTS)
|
357
|
+
|
358
|
+
assert_equal 10, result.count
|
359
|
+
assert_equal "Post #5", result[4]["body"]
|
360
|
+
assert_equal "Heinemeier Hansson", result[2]["author"]["last_name"]
|
361
|
+
assert_equal "Pavel", result[5]["author"]["first_name"]
|
362
|
+
end
|
363
|
+
|
364
|
+
test "raises an error on a render call with the :layout option" do
|
365
|
+
error = assert_raises NotImplementedError do
|
366
|
+
render('json.array! @posts, partial: "post", as: :post, layout: "layout"', posts: POSTS)
|
367
|
+
end
|
368
|
+
|
369
|
+
assert_equal "The `:layout' option is not supported in collection rendering.", error.message
|
370
|
+
end
|
371
|
+
|
372
|
+
test "raises an error on a render call with the :spacer_template option" do
|
373
|
+
error = assert_raises NotImplementedError do
|
374
|
+
render('json.array! @posts, partial: "post", as: :post, spacer_template: "template"', posts: POSTS)
|
375
|
+
end
|
376
|
+
|
377
|
+
assert_equal "The `:spacer_template' option is not supported in collection rendering.", error.message
|
378
|
+
end
|
379
|
+
end
|
380
|
+
|
286
381
|
private
|
287
382
|
def render(*args)
|
288
383
|
JSON.load render_without_parsing(*args)
|
@@ -290,7 +385,7 @@ class JbuilderTemplateTest < ActiveSupport::TestCase
|
|
290
385
|
|
291
386
|
def render_without_parsing(source, assigns = {})
|
292
387
|
view = build_view(fixtures: PARTIALS.merge("source.json.jbuilder" => source), assigns: assigns)
|
293
|
-
view.render(template: "source
|
388
|
+
view.render(template: "source")
|
294
389
|
end
|
295
390
|
|
296
391
|
def build_view(options = {})
|
@@ -306,6 +401,9 @@ class JbuilderTemplateTest < ActiveSupport::TestCase
|
|
306
401
|
end
|
307
402
|
|
308
403
|
def view.view_cache_dependencies; []; end
|
404
|
+
def view.combined_fragment_cache_key(key) [ key ] end
|
405
|
+
def view.cache_fragment_name(key, *) key end
|
406
|
+
def view.fragment_name_with_digest(key) key end
|
309
407
|
|
310
408
|
view
|
311
409
|
end
|
data/test/jbuilder_test.rb
CHANGED
@@ -312,7 +312,7 @@ class JbuilderTest < ActiveSupport::TestCase
|
|
312
312
|
assert_equal 'world', result['comments'].second['content']
|
313
313
|
end
|
314
314
|
|
315
|
-
test 'nesting multiple
|
315
|
+
test 'nesting multiple children from a non-Enumerable that responds to #map with inline loop' do
|
316
316
|
comments = NonEnumerable.new([ Comment.new('hello', 1), Comment.new('world', 2) ])
|
317
317
|
|
318
318
|
result = jbuild do |json|
|
@@ -566,6 +566,36 @@ class JbuilderTest < ActiveSupport::TestCase
|
|
566
566
|
assert_equal 'one', result['level1']
|
567
567
|
end
|
568
568
|
|
569
|
+
test 'key_format! can be changed in child elements' do
|
570
|
+
result = jbuild do |json|
|
571
|
+
json.key_format! camelize: :lower
|
572
|
+
|
573
|
+
json.level_one do
|
574
|
+
json.key_format! :upcase
|
575
|
+
json.value 'two'
|
576
|
+
end
|
577
|
+
end
|
578
|
+
|
579
|
+
assert_equal ['levelOne'], result.keys
|
580
|
+
assert_equal ['VALUE'], result['levelOne'].keys
|
581
|
+
end
|
582
|
+
|
583
|
+
test 'key_format! can be changed in array!' do
|
584
|
+
result = jbuild do |json|
|
585
|
+
json.key_format! camelize: :lower
|
586
|
+
|
587
|
+
json.level_one do
|
588
|
+
json.array! [{value: 'two'}] do |object|
|
589
|
+
json.key_format! :upcase
|
590
|
+
json.value object[:value]
|
591
|
+
end
|
592
|
+
end
|
593
|
+
end
|
594
|
+
|
595
|
+
assert_equal ['levelOne'], result.keys
|
596
|
+
assert_equal ['VALUE'], result['levelOne'][0].keys
|
597
|
+
end
|
598
|
+
|
569
599
|
test 'key_format! with no parameter' do
|
570
600
|
result = jbuild do |json|
|
571
601
|
json.key_format! :upcase
|
@@ -593,51 +623,121 @@ class JbuilderTest < ActiveSupport::TestCase
|
|
593
623
|
assert_equal ['oats and friends'], result.keys
|
594
624
|
end
|
595
625
|
|
596
|
-
test 'key_format!
|
626
|
+
test 'key_format! is not applied deeply by default' do
|
627
|
+
names = { first_name: 'camel', last_name: 'case' }
|
628
|
+
result = jbuild do |json|
|
629
|
+
json.key_format! camelize: :lower
|
630
|
+
json.set! :all_names, names
|
631
|
+
end
|
632
|
+
|
633
|
+
assert_equal %i[first_name last_name], result['allNames'].keys
|
634
|
+
end
|
635
|
+
|
636
|
+
test 'applying key_format! deeply can be enabled per scope' do
|
637
|
+
names = { first_name: 'camel', last_name: 'case' }
|
638
|
+
result = jbuild do |json|
|
639
|
+
json.key_format! camelize: :lower
|
640
|
+
json.scope do
|
641
|
+
json.deep_format_keys!
|
642
|
+
json.set! :all_names, names
|
643
|
+
end
|
644
|
+
json.set! :all_names, names
|
645
|
+
end
|
646
|
+
|
647
|
+
assert_equal %w[firstName lastName], result['scope']['allNames'].keys
|
648
|
+
assert_equal %i[first_name last_name], result['allNames'].keys
|
649
|
+
end
|
650
|
+
|
651
|
+
test 'applying key_format! deeply can be disabled per scope' do
|
652
|
+
names = { first_name: 'camel', last_name: 'case' }
|
653
|
+
result = jbuild do |json|
|
654
|
+
json.key_format! camelize: :lower
|
655
|
+
json.deep_format_keys!
|
656
|
+
json.set! :all_names, names
|
657
|
+
json.scope do
|
658
|
+
json.deep_format_keys! false
|
659
|
+
json.set! :all_names, names
|
660
|
+
end
|
661
|
+
end
|
662
|
+
|
663
|
+
assert_equal %w[firstName lastName], result['allNames'].keys
|
664
|
+
assert_equal %i[first_name last_name], result['scope']['allNames'].keys
|
665
|
+
end
|
666
|
+
|
667
|
+
test 'applying key_format! deeply can be enabled globally' do
|
668
|
+
names = { first_name: 'camel', last_name: 'case' }
|
669
|
+
|
670
|
+
Jbuilder.deep_format_keys true
|
671
|
+
result = jbuild do |json|
|
672
|
+
json.key_format! camelize: :lower
|
673
|
+
json.set! :all_names, names
|
674
|
+
end
|
675
|
+
|
676
|
+
assert_equal %w[firstName lastName], result['allNames'].keys
|
677
|
+
Jbuilder.send(:class_variable_set, '@@deep_format_keys', false)
|
678
|
+
end
|
679
|
+
|
680
|
+
test 'deep key_format! with merge!' do
|
597
681
|
hash = { camel_style: 'for JS' }
|
598
682
|
result = jbuild do |json|
|
599
683
|
json.key_format! camelize: :lower
|
684
|
+
json.deep_format_keys!
|
600
685
|
json.merge! hash
|
601
686
|
end
|
602
687
|
|
603
688
|
assert_equal ['camelStyle'], result.keys
|
604
689
|
end
|
605
690
|
|
606
|
-
test 'key_format! with merge! deep' do
|
691
|
+
test 'deep key_format! with merge! deep' do
|
607
692
|
hash = { camel_style: { sub_attr: 'for JS' } }
|
608
693
|
result = jbuild do |json|
|
609
694
|
json.key_format! camelize: :lower
|
695
|
+
json.deep_format_keys!
|
610
696
|
json.merge! hash
|
611
697
|
end
|
612
698
|
|
613
699
|
assert_equal ['subAttr'], result['camelStyle'].keys
|
614
700
|
end
|
615
701
|
|
616
|
-
test 'key_format! with set! array of hashes' do
|
702
|
+
test 'deep key_format! with set! array of hashes' do
|
617
703
|
names = [{ first_name: 'camel', last_name: 'case' }]
|
618
704
|
result = jbuild do |json|
|
619
705
|
json.key_format! camelize: :lower
|
706
|
+
json.deep_format_keys!
|
620
707
|
json.set! :names, names
|
621
708
|
end
|
622
709
|
|
623
710
|
assert_equal %w[firstName lastName], result['names'][0].keys
|
624
711
|
end
|
625
712
|
|
626
|
-
test 'key_format! with
|
713
|
+
test 'deep key_format! with set! extracting hash from object' do
|
714
|
+
comment = Struct.new(:author).new({ first_name: 'camel', last_name: 'case' })
|
715
|
+
result = jbuild do |json|
|
716
|
+
json.key_format! camelize: :lower
|
717
|
+
json.deep_format_keys!
|
718
|
+
json.set! :comment, comment, :author
|
719
|
+
end
|
720
|
+
|
721
|
+
assert_equal %w[firstName lastName], result['comment']['author'].keys
|
722
|
+
end
|
723
|
+
|
724
|
+
test 'deep key_format! with array! of hashes' do
|
627
725
|
names = [{ first_name: 'camel', last_name: 'case' }]
|
628
726
|
result = jbuild do |json|
|
629
727
|
json.key_format! camelize: :lower
|
728
|
+
json.deep_format_keys!
|
630
729
|
json.array! names
|
631
730
|
end
|
632
731
|
|
633
732
|
assert_equal %w[firstName lastName], result[0].keys
|
634
733
|
end
|
635
734
|
|
636
|
-
test 'key_format! with merge! array of hashes' do
|
735
|
+
test 'deep key_format! with merge! array of hashes' do
|
637
736
|
names = [{ first_name: 'camel', last_name: 'case' }]
|
638
737
|
new_names = [{ first_name: 'snake', last_name: 'case' }]
|
639
738
|
result = jbuild do |json|
|
640
739
|
json.key_format! camelize: :lower
|
740
|
+
json.deep_format_keys!
|
641
741
|
json.array! names
|
642
742
|
json.merge! new_names
|
643
743
|
end
|
@@ -645,6 +745,39 @@ class JbuilderTest < ActiveSupport::TestCase
|
|
645
745
|
assert_equal %w[firstName lastName], result[1].keys
|
646
746
|
end
|
647
747
|
|
748
|
+
test 'deep key_format! is applied to hash extracted from object' do
|
749
|
+
comment = Struct.new(:author).new({ first_name: 'camel', last_name: 'case' })
|
750
|
+
result = jbuild do |json|
|
751
|
+
json.key_format! camelize: :lower
|
752
|
+
json.deep_format_keys!
|
753
|
+
json.extract! comment, :author
|
754
|
+
end
|
755
|
+
|
756
|
+
assert_equal %w[firstName lastName], result['author'].keys
|
757
|
+
end
|
758
|
+
|
759
|
+
test 'deep key_format! is applied to hash extracted from hash' do
|
760
|
+
comment = {author: { first_name: 'camel', last_name: 'case' }}
|
761
|
+
result = jbuild do |json|
|
762
|
+
json.key_format! camelize: :lower
|
763
|
+
json.deep_format_keys!
|
764
|
+
json.extract! comment, :author
|
765
|
+
end
|
766
|
+
|
767
|
+
assert_equal %w[firstName lastName], result['author'].keys
|
768
|
+
end
|
769
|
+
|
770
|
+
test 'deep key_format! is applied to hash extracted directly from array' do
|
771
|
+
comments = [Struct.new(:author).new({ first_name: 'camel', last_name: 'case' })]
|
772
|
+
result = jbuild do |json|
|
773
|
+
json.key_format! camelize: :lower
|
774
|
+
json.deep_format_keys!
|
775
|
+
json.array! comments, :author
|
776
|
+
end
|
777
|
+
|
778
|
+
assert_equal %w[firstName lastName], result[0]['author'].keys
|
779
|
+
end
|
780
|
+
|
648
781
|
test 'default key_format!' do
|
649
782
|
Jbuilder.key_format camelize: :lower
|
650
783
|
result = jbuild{ |json| json.camel_style 'for JS' }
|
@@ -47,7 +47,7 @@ if Rails::VERSION::MAJOR > 4
|
|
47
47
|
end
|
48
48
|
end
|
49
49
|
|
50
|
-
test '
|
50
|
+
test "don't use require and permit if there are no attributes" do
|
51
51
|
run_generator %w(Post --api)
|
52
52
|
|
53
53
|
assert_file 'app/controllers/posts_controller.rb' do |content|
|
@@ -31,14 +31,14 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase
|
|
31
31
|
assert_instance_method :create, content do |m|
|
32
32
|
assert_match %r{@post = Post\.new\(post_params\)}, m
|
33
33
|
assert_match %r{@post\.save}, m
|
34
|
-
assert_match %r{format\.html \{ redirect_to @post, notice: "Post was successfully created\." \}}, m
|
34
|
+
assert_match %r{format\.html \{ redirect_to post_url\(@post\), notice: "Post was successfully created\." \}}, m
|
35
35
|
assert_match %r{format\.json \{ render :show, status: :created, location: @post \}}, m
|
36
36
|
assert_match %r{format\.html \{ render :new, status: :unprocessable_entity \}}, m
|
37
37
|
assert_match %r{format\.json \{ render json: @post\.errors, status: :unprocessable_entity \}}, m
|
38
38
|
end
|
39
39
|
|
40
40
|
assert_instance_method :update, content do |m|
|
41
|
-
assert_match %r{format\.html \{ redirect_to @post, notice: "Post was successfully updated\." \}}, m
|
41
|
+
assert_match %r{format\.html \{ redirect_to post_url\(@post\), notice: "Post was successfully updated\." \}}, m
|
42
42
|
assert_match %r{format\.json \{ render :show, status: :ok, location: @post \}}, m
|
43
43
|
assert_match %r{format\.html \{ render :edit, status: :unprocessable_entity \}}, m
|
44
44
|
assert_match %r{format\.json \{ render json: @post.errors, status: :unprocessable_entity \}}, m
|
@@ -59,7 +59,26 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase
|
|
59
59
|
end
|
60
60
|
end
|
61
61
|
|
62
|
-
|
62
|
+
if Rails::VERSION::MAJOR >= 6
|
63
|
+
test 'controller with namespace' do
|
64
|
+
run_generator %w(Admin::Post --model-name=Post)
|
65
|
+
assert_file 'app/controllers/admin/posts_controller.rb' do |content|
|
66
|
+
assert_instance_method :create, content do |m|
|
67
|
+
assert_match %r{format\.html \{ redirect_to admin_post_url\(@post\), notice: "Post was successfully created\." \}}, m
|
68
|
+
end
|
69
|
+
|
70
|
+
assert_instance_method :update, content do |m|
|
71
|
+
assert_match %r{format\.html \{ redirect_to admin_post_url\(@post\), notice: "Post was successfully updated\." \}}, m
|
72
|
+
end
|
73
|
+
|
74
|
+
assert_instance_method :destroy, content do |m|
|
75
|
+
assert_match %r{format\.html \{ redirect_to admin_posts_url, notice: "Post was successfully destroyed\." \}}, m
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
test "don't use require and permit if there are no attributes" do
|
63
82
|
run_generator %w(Post)
|
64
83
|
|
65
84
|
assert_file 'app/controllers/posts_controller.rb' do |content|
|
data/test/test_helper.rb
CHANGED
@@ -21,7 +21,13 @@ class << Rails
|
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
24
|
-
|
24
|
+
Jbuilder::CollectionRenderer.collection_cache = Rails.cache
|
25
|
+
|
26
|
+
class Post < Struct.new(:id, :body, :author_name)
|
27
|
+
def cache_key
|
28
|
+
"post-#{id}"
|
29
|
+
end
|
30
|
+
end
|
25
31
|
|
26
32
|
class Racer < Struct.new(:id, :name)
|
27
33
|
extend ActiveModel::Naming
|
@@ -29,6 +35,3 @@ class Racer < Struct.new(:id, :name)
|
|
29
35
|
end
|
30
36
|
|
31
37
|
ActionView::Template.register_template_handler :jbuilder, JbuilderHandler
|
32
|
-
|
33
|
-
ActionView::Base.remove_possible_method :fragment_name_with_digest
|
34
|
-
ActionView::Base.remove_possible_method :cache_fragment_name
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jbuilder
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.11.
|
4
|
+
version: 2.11.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Heinemeier Hansson
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-12-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -24,14 +24,14 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: 5.0.0
|
27
|
-
description:
|
27
|
+
description:
|
28
28
|
email: david@basecamp.com
|
29
29
|
executables: []
|
30
30
|
extensions: []
|
31
31
|
extra_rdoc_files: []
|
32
32
|
files:
|
33
|
+
- ".github/workflows/ruby.yml"
|
33
34
|
- ".gitignore"
|
34
|
-
- ".travis.yml"
|
35
35
|
- Appraisals
|
36
36
|
- CHANGELOG.md
|
37
37
|
- CONTRIBUTING.md
|
@@ -43,6 +43,7 @@ files:
|
|
43
43
|
- gemfiles/rails_5_1.gemfile
|
44
44
|
- gemfiles/rails_5_2.gemfile
|
45
45
|
- gemfiles/rails_6_0.gemfile
|
46
|
+
- gemfiles/rails_6_1.gemfile
|
46
47
|
- gemfiles/rails_head.gemfile
|
47
48
|
- jbuilder.gemspec
|
48
49
|
- lib/generators/rails/jbuilder_generator.rb
|
@@ -54,6 +55,7 @@ files:
|
|
54
55
|
- lib/generators/rails/templates/show.json.jbuilder
|
55
56
|
- lib/jbuilder.rb
|
56
57
|
- lib/jbuilder/blank.rb
|
58
|
+
- lib/jbuilder/collection_renderer.rb
|
57
59
|
- lib/jbuilder/dependency_tracker.rb
|
58
60
|
- lib/jbuilder/errors.rb
|
59
61
|
- lib/jbuilder/jbuilder.rb
|
@@ -71,7 +73,7 @@ homepage: https://github.com/rails/jbuilder
|
|
71
73
|
licenses:
|
72
74
|
- MIT
|
73
75
|
metadata: {}
|
74
|
-
post_install_message:
|
76
|
+
post_install_message:
|
75
77
|
rdoc_options: []
|
76
78
|
require_paths:
|
77
79
|
- lib
|
@@ -86,8 +88,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
86
88
|
- !ruby/object:Gem::Version
|
87
89
|
version: '0'
|
88
90
|
requirements: []
|
89
|
-
rubygems_version: 3.
|
90
|
-
signing_key:
|
91
|
+
rubygems_version: 3.2.32
|
92
|
+
signing_key:
|
91
93
|
specification_version: 4
|
92
94
|
summary: Create JSON structures via a Builder-style DSL
|
93
95
|
test_files:
|
data/.travis.yml
DELETED
@@ -1,52 +0,0 @@
|
|
1
|
-
language: ruby
|
2
|
-
|
3
|
-
cache: bundler
|
4
|
-
|
5
|
-
before_install:
|
6
|
-
- "gem install bundler -v '<2'"
|
7
|
-
|
8
|
-
rvm:
|
9
|
-
- 2.2.10
|
10
|
-
- 2.3.8
|
11
|
-
- 2.4.10
|
12
|
-
- 2.5.8
|
13
|
-
- 2.6.6
|
14
|
-
- 2.7.1
|
15
|
-
- ruby-head
|
16
|
-
|
17
|
-
gemfile:
|
18
|
-
- gemfiles/rails_5_0.gemfile
|
19
|
-
- gemfiles/rails_5_1.gemfile
|
20
|
-
- gemfiles/rails_5_2.gemfile
|
21
|
-
- gemfiles/rails_6_0.gemfile
|
22
|
-
- gemfiles/rails_head.gemfile
|
23
|
-
|
24
|
-
matrix:
|
25
|
-
exclude:
|
26
|
-
- rvm: 2.7.1
|
27
|
-
gemfile: gemfiles/rails_5_0.gemfile
|
28
|
-
- rvm: 2.7.1
|
29
|
-
gemfile: gemfiles/rails_5_1.gemfile
|
30
|
-
- rvm: 2.2.10
|
31
|
-
gemfile: gemfiles/rails_5_2.gemfile
|
32
|
-
- rvm: 2.7.1
|
33
|
-
gemfile: gemfiles/rails_5_2.gemfile
|
34
|
-
- rvm: 2.2.10
|
35
|
-
gemfile: gemfiles/rails_6_0.gemfile
|
36
|
-
- rvm: 2.3.8
|
37
|
-
gemfile: gemfiles/rails_6_0.gemfile
|
38
|
-
- rvm: 2.4.10
|
39
|
-
gemfile: gemfiles/rails_6_0.gemfile
|
40
|
-
- rvm: 2.2.10
|
41
|
-
gemfile: gemfiles/rails_head.gemfile
|
42
|
-
- rvm: 2.3.8
|
43
|
-
gemfile: gemfiles/rails_head.gemfile
|
44
|
-
- rvm: 2.4.10
|
45
|
-
gemfile: gemfiles/rails_head.gemfile
|
46
|
-
allow_failures:
|
47
|
-
- rvm: ruby-head
|
48
|
-
- gemfile: gemfiles/rails_head.gemfile
|
49
|
-
fast_finish: true
|
50
|
-
|
51
|
-
notifications:
|
52
|
-
email: false
|