jbuilder 2.11.2 → 2.11.3
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/CONTRIBUTING.md +3 -3
- data/README.md +45 -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/jbuilder/collection_renderer.rb +108 -0
- data/lib/jbuilder/jbuilder_template.rb +54 -7
- data/test/jbuilder_dependency_tracker_test.rb +1 -1
- data/test/jbuilder_template_test.rb +57 -2
- data/test/jbuilder_test.rb +1 -1
- data/test/scaffold_api_controller_generator_test.rb +1 -1
- data/test/scaffold_controller_generator_test.rb +1 -1
- 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: 4dacba51a263fc3bcbea4471ae1557dea10c014f68657a39ab812e55a167eba9
|
4
|
+
data.tar.gz: 544f954ce6f89f7775853b91bfc99f5f1634596389f5750028e13532b314a63c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a71638ae38f5755b97a40db81c2121f92788b3e4242de6d4789032f0ff7bab4f62d2dd96b7cab19e8583ee448946bb2e66e8cf5f6ab65c7b514ff88c19af1407
|
7
|
+
data.tar.gz: cd3477c065ebdbacc5da5d383973e996499cce7872d8303691376739732735441b003981a767af1bdb8ccea6648685291ff20e81c662a95ad54c87205ba233f9
|
@@ -0,0 +1,108 @@
|
|
1
|
+
name: Ruby test
|
2
|
+
|
3
|
+
on: [push, pull_request]
|
4
|
+
|
5
|
+
jobs:
|
6
|
+
test:
|
7
|
+
name: Ruby test
|
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.10,
|
19
|
+
2.3.8,
|
20
|
+
2.4.10,
|
21
|
+
2.5.8,
|
22
|
+
2.6.6,
|
23
|
+
2.7.1,
|
24
|
+
3.0.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.1
|
37
|
+
gemfile: rails_5_0
|
38
|
+
- ruby: 3.0.0
|
39
|
+
gemfile: rails_5_0
|
40
|
+
- ruby: head
|
41
|
+
gemfile: rails_5_0
|
42
|
+
- ruby: 2.7.1
|
43
|
+
gemfile: rails_5_1
|
44
|
+
- ruby: 3.0.0
|
45
|
+
gemfile: rails_5_1
|
46
|
+
- ruby: head
|
47
|
+
gemfile: rails_5_1
|
48
|
+
- ruby: 2.2.10
|
49
|
+
gemfile: rails_5_2
|
50
|
+
- ruby: 2.7.1
|
51
|
+
gemfile: rails_5_2
|
52
|
+
- ruby: 3.0.0
|
53
|
+
gemfile: rails_5_2
|
54
|
+
- ruby: head
|
55
|
+
gemfile: rails_5_2
|
56
|
+
- ruby: 2.2.10
|
57
|
+
gemfile: rails_6_0
|
58
|
+
- ruby: 2.3.8
|
59
|
+
gemfile: rails_6_0
|
60
|
+
- ruby: 2.4.10
|
61
|
+
gemfile: rails_6_0
|
62
|
+
- ruby: 3.0.0
|
63
|
+
gemfile: rails_6_0
|
64
|
+
- ruby: head
|
65
|
+
gemfile: rails_6_0
|
66
|
+
- ruby: 2.2.10
|
67
|
+
gemfile: rails_6_1
|
68
|
+
- ruby: 2.3.8
|
69
|
+
gemfile: rails_6_1
|
70
|
+
- ruby: 2.4.10
|
71
|
+
gemfile: rails_6_1
|
72
|
+
- ruby: 2.2.10
|
73
|
+
gemfile: rails_head
|
74
|
+
- ruby: 2.3.8
|
75
|
+
gemfile: rails_head
|
76
|
+
- ruby: 2.4.10
|
77
|
+
gemfile: rails_head
|
78
|
+
- ruby: 2.5.8
|
79
|
+
gemfile: rails_head
|
80
|
+
- ruby: 2.6.6
|
81
|
+
gemfile: rails_head
|
82
|
+
- ruby: 2.7.1
|
83
|
+
gemfile: rails_head
|
84
|
+
experimental: false
|
85
|
+
- ruby: 3.0.0
|
86
|
+
gemfile: rails_head
|
87
|
+
experimental: false
|
88
|
+
include:
|
89
|
+
- ruby: 2.7.1
|
90
|
+
gemfile: rails_head
|
91
|
+
experimental: true
|
92
|
+
- ruby: 3.0.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/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,32 @@ 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
|
+
You don't always have or need a collection when building an array.
|
121
|
+
|
122
|
+
```ruby
|
123
|
+
json.people do
|
124
|
+
json.child! do
|
125
|
+
json.id 1
|
126
|
+
json.name 'David'
|
127
|
+
end
|
128
|
+
json.child! do
|
129
|
+
json.id 2
|
130
|
+
json.name 'Jamie'
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
# => { "people": [ { "id": 1, "name": "David" }, { "id": 2, "name": "Jamie" } ] }
|
135
|
+
```
|
136
|
+
|
111
137
|
Jbuilder objects can be directly nested inside each other. Useful for composing objects.
|
112
138
|
|
113
139
|
``` ruby
|
@@ -137,7 +163,7 @@ company.to_builder.target!
|
|
137
163
|
```
|
138
164
|
|
139
165
|
You can either use Jbuilder stand-alone or directly as an ActionView template
|
140
|
-
language. When required in Rails, you can create views
|
166
|
+
language. When required in Rails, you can create views à la show.json.jbuilder
|
141
167
|
(the json is already yielded):
|
142
168
|
|
143
169
|
``` ruby
|
@@ -171,19 +197,19 @@ It's also possible to render collections of partials:
|
|
171
197
|
json.array! @posts, partial: 'posts/post', as: :post
|
172
198
|
|
173
199
|
# or
|
174
|
-
|
175
200
|
json.partial! 'posts/post', collection: @posts, as: :post
|
176
201
|
|
177
202
|
# or
|
178
|
-
|
179
203
|
json.partial! partial: 'posts/post', collection: @posts, as: :post
|
180
204
|
|
181
205
|
# or
|
182
|
-
|
183
206
|
json.comments @post.comments, partial: 'comments/comment', as: :comment
|
184
207
|
```
|
185
208
|
|
186
|
-
The `as: :some_symbol` is used with partials. It will take care of mapping the passed in object to a variable for the
|
209
|
+
The `as: :some_symbol` is used with partials. It will take care of mapping the passed in object to a variable for the
|
210
|
+
partial. If the value is a collection either implicitly or explicitly by using the `collection:` option, then each
|
211
|
+
value of the collection is passed to the partial as the variable `some_symbol`. If the value is a singular object,
|
212
|
+
then the object is passed to the partial as the variable `some_symbol`.
|
187
213
|
|
188
214
|
Be sure not to confuse the `as:` option to mean nesting of the partial. For example:
|
189
215
|
|
@@ -193,7 +219,7 @@ Be sure not to confuse the `as:` option to mean nesting of the partial. For exam
|
|
193
219
|
json.partial! @comment, as: :comment
|
194
220
|
```
|
195
221
|
|
196
|
-
is quite different
|
222
|
+
is quite different from:
|
197
223
|
|
198
224
|
```ruby
|
199
225
|
# comment attributes are nested under a "comment" property
|
@@ -236,6 +262,8 @@ json.bar "bar"
|
|
236
262
|
# => { "bar": "bar" }
|
237
263
|
```
|
238
264
|
|
265
|
+
## Caching
|
266
|
+
|
239
267
|
Fragment caching is supported, it uses `Rails.cache` and works like caching in
|
240
268
|
HTML templates:
|
241
269
|
|
@@ -253,9 +281,17 @@ json.cache_if! !admin?, ['v1', @person], expires_in: 10.minutes do
|
|
253
281
|
end
|
254
282
|
```
|
255
283
|
|
256
|
-
|
257
|
-
|
258
|
-
|
284
|
+
Aside from that, the `:cached` options on collection rendering is available on Rails >= 6.0. This will cache the
|
285
|
+
rendered results effectively using the multi fetch feature.
|
286
|
+
|
287
|
+
```
|
288
|
+
json.array! @posts, partial: "posts/post", as: :post, cached: true
|
289
|
+
|
290
|
+
# or:
|
291
|
+
json.comments @post.comments, partial: "comments/comment", as: :comment, cached: true
|
292
|
+
```
|
293
|
+
|
294
|
+
## Formatting Keys
|
259
295
|
|
260
296
|
Keys can be auto formatted using `key_format!`, this can be used to convert
|
261
297
|
keynames from the standard ruby_format to camelCase:
|
data/Rakefile
CHANGED
data/gemfiles/rails_head.gemfile
CHANGED
data/jbuilder.gemspec
CHANGED
@@ -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
|
144
|
+
if options.key?(:collection) && (options[:collection].nil? || options[:collection].empty?)
|
145
|
+
array!
|
146
|
+
elsif as && options.key?(:collection) && CollectionRenderer.supported?
|
147
|
+
collection = options.delete(:collection) || []
|
148
|
+
partial = options.delete(:partial)
|
149
|
+
options[:locals].merge!(json: self)
|
150
|
+
|
151
|
+
if options.has_key?(:layout)
|
152
|
+
raise ::NotImplementedError, "The `:layout' option is not supported in collection rendering."
|
153
|
+
end
|
154
|
+
|
155
|
+
if options.has_key?(:spacer_template)
|
156
|
+
raise ::NotImplementedError, "The `:spacer_template' option is not supported in collection rendering."
|
157
|
+
end
|
158
|
+
|
159
|
+
CollectionRenderer
|
160
|
+
.new(@context.lookup_context, options) { |&block| _scope(&block) }
|
161
|
+
.render_collection_with_partial(collection, partial, @context, 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
|
@@ -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,58 @@ 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 "supports the cached: true option" do
|
295
|
+
result = render('json.array! @posts, partial: "post", as: :post, cached: true', posts: POSTS)
|
296
|
+
|
297
|
+
assert_equal 10, result.count
|
298
|
+
assert_equal "Post #5", result[4]["body"]
|
299
|
+
assert_equal "Heinemeier Hansson", result[2]["author"]["last_name"]
|
300
|
+
assert_equal "Pavel", result[5]["author"]["first_name"]
|
301
|
+
|
302
|
+
expected = {
|
303
|
+
"id" => 1,
|
304
|
+
"body" => "Post #1",
|
305
|
+
"author" => {
|
306
|
+
"first_name" => "David",
|
307
|
+
"last_name" => "Heinemeier Hansson"
|
308
|
+
}
|
309
|
+
}
|
310
|
+
|
311
|
+
assert_equal expected, Rails.cache.read("post-1")
|
312
|
+
|
313
|
+
result = render('json.array! @posts, partial: "post", as: :post, cached: true', posts: POSTS)
|
314
|
+
|
315
|
+
assert_equal 10, result.count
|
316
|
+
assert_equal "Post #5", result[4]["body"]
|
317
|
+
assert_equal "Heinemeier Hansson", result[2]["author"]["last_name"]
|
318
|
+
assert_equal "Pavel", result[5]["author"]["first_name"]
|
319
|
+
end
|
320
|
+
|
321
|
+
test "raises an error on a render call with the :layout option" do
|
322
|
+
error = assert_raises NotImplementedError do
|
323
|
+
render('json.array! @posts, partial: "post", as: :post, layout: "layout"', posts: POSTS)
|
324
|
+
end
|
325
|
+
|
326
|
+
assert_equal "The `:layout' option is not supported in collection rendering.", error.message
|
327
|
+
end
|
328
|
+
|
329
|
+
test "raises an error on a render call with the :spacer_template option" do
|
330
|
+
error = assert_raises NotImplementedError do
|
331
|
+
render('json.array! @posts, partial: "post", as: :post, spacer_template: "template"', posts: POSTS)
|
332
|
+
end
|
333
|
+
|
334
|
+
assert_equal "The `:spacer_template' option is not supported in collection rendering.", error.message
|
335
|
+
end
|
336
|
+
end
|
337
|
+
|
286
338
|
private
|
287
339
|
def render(*args)
|
288
340
|
JSON.load render_without_parsing(*args)
|
@@ -290,7 +342,7 @@ class JbuilderTemplateTest < ActiveSupport::TestCase
|
|
290
342
|
|
291
343
|
def render_without_parsing(source, assigns = {})
|
292
344
|
view = build_view(fixtures: PARTIALS.merge("source.json.jbuilder" => source), assigns: assigns)
|
293
|
-
view.render(template: "source
|
345
|
+
view.render(template: "source")
|
294
346
|
end
|
295
347
|
|
296
348
|
def build_view(options = {})
|
@@ -306,6 +358,9 @@ class JbuilderTemplateTest < ActiveSupport::TestCase
|
|
306
358
|
end
|
307
359
|
|
308
360
|
def view.view_cache_dependencies; []; end
|
361
|
+
def view.combined_fragment_cache_key(key) [ key ] end
|
362
|
+
def view.cache_fragment_name(key, *) key end
|
363
|
+
def view.fragment_name_with_digest(key) key end
|
309
364
|
|
310
365
|
view
|
311
366
|
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|
|
@@ -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|
|
@@ -59,7 +59,7 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase
|
|
59
59
|
end
|
60
60
|
end
|
61
61
|
|
62
|
-
test '
|
62
|
+
test "don't use require and permit if there are no attributes" do
|
63
63
|
run_generator %w(Post)
|
64
64
|
|
65
65
|
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.3
|
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-11-14 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.22
|
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
|