jbuilder 2.3.2 → 2.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/.gitignore +1 -0
- data/.travis.yml +49 -25
- data/Appraisals +19 -36
- data/CHANGELOG.md +72 -3
- data/CONTRIBUTING.md +14 -8
- data/Gemfile +0 -1
- data/MIT-LICENSE +1 -1
- data/README.md +47 -22
- data/Rakefile +1 -5
- data/gemfiles/rails_4_2.gemfile +3 -6
- data/gemfiles/rails_5_0.gemfile +10 -0
- data/gemfiles/rails_5_1.gemfile +10 -0
- data/gemfiles/rails_5_2.gemfile +10 -0
- data/gemfiles/rails_6_0.gemfile +10 -0
- data/gemfiles/rails_head.gemfile +10 -0
- data/jbuilder.gemspec +4 -5
- data/lib/generators/rails/jbuilder_generator.rb +9 -2
- data/lib/generators/rails/scaffold_controller_generator.rb +7 -1
- data/lib/generators/rails/templates/api_controller.rb +2 -2
- data/lib/generators/rails/templates/controller.rb +2 -2
- data/lib/generators/rails/templates/index.json.jbuilder +1 -4
- data/lib/generators/rails/templates/partial.json.jbuilder +2 -0
- data/lib/generators/rails/templates/show.json.jbuilder +1 -1
- data/lib/jbuilder.rb +7 -7
- data/lib/jbuilder/errors.rb +7 -0
- data/lib/jbuilder/jbuilder_template.rb +61 -8
- data/lib/jbuilder/key_formatter.rb +2 -2
- data/lib/jbuilder/railtie.rb +12 -5
- data/test/jbuilder_dependency_tracker_test.rb +1 -1
- data/test/jbuilder_generator_test.rb +18 -4
- data/test/jbuilder_template_test.rb +204 -275
- data/test/jbuilder_test.rb +38 -2
- data/test/scaffold_api_controller_generator_test.rb +24 -11
- data/test/scaffold_controller_generator_test.rb +31 -18
- data/test/test_helper.rb +26 -6
- metadata +12 -36
- data/gemfiles/rails_3_0.gemfile +0 -14
- data/gemfiles/rails_3_1.gemfile +0 -14
- data/gemfiles/rails_3_2.gemfile +0 -14
- data/gemfiles/rails_4_0.gemfile +0 -13
- data/gemfiles/rails_4_1.gemfile +0 -13
- data/gemfiles/rails_edge.gemfile +0 -13
@@ -1,383 +1,312 @@
|
|
1
1
|
require "test_helper"
|
2
|
-
require "mocha/setup"
|
3
|
-
require "active_model"
|
4
|
-
require "action_view"
|
5
2
|
require "action_view/testing/resolvers"
|
6
|
-
require "active_support/cache"
|
7
|
-
require "jbuilder/jbuilder_template"
|
8
|
-
|
9
|
-
BLOG_POST_PARTIAL = <<-JBUILDER
|
10
|
-
json.extract! blog_post, :id, :body
|
11
|
-
json.author do
|
12
|
-
first_name, last_name = blog_post.author_name.split(nil, 2)
|
13
|
-
json.first_name first_name
|
14
|
-
json.last_name last_name
|
15
|
-
end
|
16
|
-
JBUILDER
|
17
|
-
|
18
|
-
COLLECTION_PARTIAL = <<-JBUILDER
|
19
|
-
json.extract! collection, :id, :name
|
20
|
-
JBUILDER
|
21
3
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
4
|
+
class JbuilderTemplateTest < ActiveSupport::TestCase
|
5
|
+
POST_PARTIAL = <<-JBUILDER
|
6
|
+
json.extract! post, :id, :body
|
7
|
+
json.author do
|
8
|
+
first_name, last_name = post.author_name.split(nil, 2)
|
9
|
+
json.first_name first_name
|
10
|
+
json.last_name last_name
|
11
|
+
end
|
12
|
+
JBUILDER
|
29
13
|
|
30
|
-
|
31
|
-
|
32
|
-
|
14
|
+
COLLECTION_PARTIAL = <<-JBUILDER
|
15
|
+
json.extract! collection, :id, :name
|
16
|
+
JBUILDER
|
33
17
|
|
34
|
-
|
35
|
-
|
18
|
+
RACER_PARTIAL = <<-JBUILDER
|
19
|
+
json.extract! racer, :id, :name
|
20
|
+
JBUILDER
|
36
21
|
|
22
|
+
PARTIALS = {
|
23
|
+
"_partial.json.jbuilder" => "json.content content",
|
24
|
+
"_post.json.jbuilder" => POST_PARTIAL,
|
25
|
+
"racers/_racer.json.jbuilder" => RACER_PARTIAL,
|
26
|
+
"_collection.json.jbuilder" => COLLECTION_PARTIAL,
|
37
27
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
BLOG_POST_COLLECTION = Array.new(10){ |i| BlogPost.new(i+1, "post body #{i+1}", blog_authors.next) }
|
42
|
-
COLLECTION_COLLECTION = Array.new(5){ |i| Collection.new(i+1, "collection #{i+1}") }
|
28
|
+
# Ensure we find only Jbuilder partials from within Jbuilder templates.
|
29
|
+
"_post.html.erb" => "Hello world!"
|
30
|
+
}
|
43
31
|
|
44
|
-
|
32
|
+
AUTHORS = [ "David Heinemeier Hansson", "Pavel Pravosud" ].cycle
|
33
|
+
POSTS = (1..10).collect { |i| Post.new(i, "Post ##{i}", AUTHORS.next) }
|
45
34
|
|
46
|
-
|
47
|
-
"_partial.json.jbuilder" => "foo ||= 'hello'; json.content foo",
|
48
|
-
"_blog_post.json.jbuilder" => BLOG_POST_PARTIAL,
|
49
|
-
"racers/_racer.json.jbuilder" => RACER_PARTIAL,
|
50
|
-
"_collection.json.jbuilder" => COLLECTION_PARTIAL
|
51
|
-
}
|
35
|
+
setup { Rails.cache.clear }
|
52
36
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
class JbuilderTemplateTest < ActionView::TestCase
|
60
|
-
setup do
|
61
|
-
@context = self
|
62
|
-
Rails.cache.clear
|
37
|
+
test "basic template" do
|
38
|
+
result = render('json.content "hello"')
|
39
|
+
assert_equal "hello", result["content"]
|
63
40
|
end
|
64
41
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
partials["test.json.jbuilder"] = source
|
69
|
-
resolver = ActionView::FixtureResolver.new(partials)
|
70
|
-
lookup_context.view_paths = [resolver]
|
71
|
-
template = ActionView::Template.new(source, "test", JbuilderHandler, virtual_path: "test")
|
72
|
-
json = template.render(self, {}).strip
|
73
|
-
MultiJson.load(json)
|
42
|
+
test "partial by name with top-level locals" do
|
43
|
+
result = render('json.partial! "partial", content: "hello"')
|
44
|
+
assert_equal "hello", result["content"]
|
74
45
|
end
|
75
46
|
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
undef_method name.to_sym if method_defined?(name.to_sym)
|
80
|
-
end
|
81
|
-
end
|
47
|
+
test "partial by name with nested locals" do
|
48
|
+
result = render('json.partial! "partial", locals: { content: "hello" }')
|
49
|
+
assert_equal "hello", result["content"]
|
82
50
|
end
|
83
51
|
|
84
|
-
|
85
|
-
result =
|
86
|
-
|
87
|
-
assert_equal 10, result.length
|
88
|
-
assert_equal Array, result.class
|
89
|
-
assert_equal "post body 5", result[4]["body"]
|
90
|
-
assert_equal "Heinemeier Hansson", result[2]["author"]["last_name"]
|
91
|
-
assert_equal "Pavel", result[5]["author"]["first_name"]
|
52
|
+
test "partial by options containing nested locals" do
|
53
|
+
result = render('json.partial! partial: "partial", locals: { content: "hello" }')
|
54
|
+
assert_equal "hello", result["content"]
|
92
55
|
end
|
93
56
|
|
94
|
-
test "
|
95
|
-
result =
|
96
|
-
json.content "hello"
|
97
|
-
JBUILDER
|
98
|
-
|
57
|
+
test "partial by options containing top-level locals" do
|
58
|
+
result = render('json.partial! partial: "partial", content: "hello"')
|
99
59
|
assert_equal "hello", result["content"]
|
100
60
|
end
|
101
61
|
|
102
|
-
test "
|
103
|
-
result =
|
104
|
-
|
105
|
-
|
106
|
-
JBUILDER
|
107
|
-
|
108
|
-
assert_equal ["camelStyle"], result.keys
|
62
|
+
test "partial for Active Model" do
|
63
|
+
result = render('json.partial! @racer', racer: Racer.new(123, "Chris Harris"))
|
64
|
+
assert_equal 123, result["id"]
|
65
|
+
assert_equal "Chris Harris", result["name"]
|
109
66
|
end
|
110
67
|
|
111
|
-
test "
|
112
|
-
result =
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
end
|
118
|
-
JBUILDER
|
119
|
-
|
120
|
-
assert_equal "one", result["LEVEL1"]
|
121
|
-
assert_equal "two", result["LEVEL2"]["VALUE"]
|
68
|
+
test "partial collection by name with symbol local" do
|
69
|
+
result = render('json.partial! "post", collection: @posts, as: :post', posts: POSTS)
|
70
|
+
assert_equal 10, result.count
|
71
|
+
assert_equal "Post #5", result[4]["body"]
|
72
|
+
assert_equal "Heinemeier Hansson", result[2]["author"]["last_name"]
|
73
|
+
assert_equal "Pavel", result[5]["author"]["first_name"]
|
122
74
|
end
|
123
75
|
|
124
|
-
test "partial
|
125
|
-
result =
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
assert_equal "
|
76
|
+
test "partial collection by name with string local" do
|
77
|
+
result = render('json.partial! "post", collection: @posts, as: "post"', posts: POSTS)
|
78
|
+
assert_equal 10, result.count
|
79
|
+
assert_equal "Post #5", result[4]["body"]
|
80
|
+
assert_equal "Heinemeier Hansson", result[2]["author"]["last_name"]
|
81
|
+
assert_equal "Pavel", result[5]["author"]["first_name"]
|
130
82
|
end
|
131
83
|
|
132
|
-
test "partial
|
133
|
-
result =
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
assert_equal "
|
84
|
+
test "partial collection by options" do
|
85
|
+
result = render('json.partial! partial: "post", collection: @posts, as: :post', posts: POSTS)
|
86
|
+
assert_equal 10, result.count
|
87
|
+
assert_equal "Post #5", result[4]["body"]
|
88
|
+
assert_equal "Heinemeier Hansson", result[2]["author"]["last_name"]
|
89
|
+
assert_equal "Pavel", result[5]["author"]["first_name"]
|
138
90
|
end
|
139
91
|
|
140
|
-
test "partial
|
141
|
-
|
142
|
-
json.partial! "partial", foo: "goodbye"
|
143
|
-
JBUILDER
|
144
|
-
|
145
|
-
assert_equal "goodbye", result["content"]
|
92
|
+
test "nil partial collection by name" do
|
93
|
+
assert_equal [], render('json.partial! "post", collection: @posts, as: :post', posts: nil)
|
146
94
|
end
|
147
95
|
|
148
|
-
test "partial
|
149
|
-
|
150
|
-
json.partial! "blog_post", collection: BLOG_POST_COLLECTION, as: :blog_post
|
151
|
-
JBUILDER
|
152
|
-
|
153
|
-
assert_collection_rendered result
|
96
|
+
test "nil partial collection by options" do
|
97
|
+
assert_equal [], render('json.partial! partial: "post", collection: @posts, as: :post', posts: nil)
|
154
98
|
end
|
155
99
|
|
156
|
-
test "
|
157
|
-
result =
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
100
|
+
test "array of partials" do
|
101
|
+
result = render('json.array! @posts, partial: "post", as: :post', posts: POSTS)
|
102
|
+
assert_equal 10, result.count
|
103
|
+
assert_equal "Post #5", result[4]["body"]
|
104
|
+
assert_equal "Heinemeier Hansson", result[2]["author"]["last_name"]
|
105
|
+
assert_equal "Pavel", result[5]["author"]["first_name"]
|
162
106
|
end
|
163
107
|
|
164
|
-
test "
|
165
|
-
|
166
|
-
json.partial! "collection", collection: COLLECTION_COLLECTION, as: :collection
|
167
|
-
JBUILDER
|
168
|
-
|
169
|
-
assert_equal 5, result.length
|
108
|
+
test "empty array of partials from nil collection" do
|
109
|
+
assert_equal [], render('json.array! @posts, partial: "post", as: :post', posts: nil)
|
170
110
|
end
|
171
111
|
|
172
|
-
test "
|
173
|
-
result =
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
assert_equal
|
112
|
+
test "array of partials under key" do
|
113
|
+
result = render('json.posts @posts, partial: "post", as: :post', posts: POSTS)
|
114
|
+
assert_equal 10, result["posts"].count
|
115
|
+
assert_equal "Post #5", result["posts"][4]["body"]
|
116
|
+
assert_equal "Heinemeier Hansson", result["posts"][2]["author"]["last_name"]
|
117
|
+
assert_equal "Pavel", result["posts"][5]["author"]["first_name"]
|
178
118
|
end
|
179
119
|
|
180
|
-
test "
|
181
|
-
result =
|
182
|
-
|
183
|
-
JBUILDER
|
184
|
-
|
185
|
-
assert_collection_rendered result
|
120
|
+
test "empty array of partials under key from nil collection" do
|
121
|
+
result = render('json.posts @posts, partial: "post", as: :post', posts: nil)
|
122
|
+
assert_equal [], result["posts"]
|
186
123
|
end
|
187
124
|
|
188
|
-
test "
|
189
|
-
|
190
|
-
json.
|
125
|
+
test "object fragment caching" do
|
126
|
+
render(<<-JBUILDER)
|
127
|
+
json.cache! "cache-key" do
|
128
|
+
json.name "Hit"
|
129
|
+
end
|
191
130
|
JBUILDER
|
192
131
|
|
193
|
-
|
132
|
+
hit = render('json.cache! "cache-key" do; end')
|
133
|
+
assert_equal "Hit", hit["name"]
|
194
134
|
end
|
195
135
|
|
196
|
-
test "
|
197
|
-
|
198
|
-
json.
|
136
|
+
test "conditional object fragment caching" do
|
137
|
+
render(<<-JBUILDER)
|
138
|
+
json.cache_if! true, "cache-key" do
|
139
|
+
json.a "Hit"
|
140
|
+
end
|
141
|
+
|
142
|
+
json.cache_if! false, "cache-key" do
|
143
|
+
json.b "Hit"
|
144
|
+
end
|
199
145
|
JBUILDER
|
200
146
|
|
201
|
-
|
202
|
-
|
147
|
+
result = render(<<-JBUILDER)
|
148
|
+
json.cache_if! true, "cache-key" do
|
149
|
+
json.a "Miss"
|
150
|
+
end
|
203
151
|
|
204
|
-
|
205
|
-
|
206
|
-
|
152
|
+
json.cache_if! false, "cache-key" do
|
153
|
+
json.b "Miss"
|
154
|
+
end
|
207
155
|
JBUILDER
|
208
156
|
|
209
|
-
assert_equal
|
157
|
+
assert_equal "Hit", result["a"]
|
158
|
+
assert_equal "Miss", result["b"]
|
210
159
|
end
|
211
160
|
|
212
|
-
test "
|
213
|
-
|
214
|
-
|
161
|
+
test "object fragment caching with expiry" do
|
162
|
+
travel_to "2018-05-12 11:29:00 -0400"
|
163
|
+
|
164
|
+
render <<-JBUILDER
|
165
|
+
json.cache! "cache-key", expires_in: 1.minute do
|
166
|
+
json.name "Hit"
|
167
|
+
end
|
215
168
|
JBUILDER
|
216
169
|
|
217
|
-
|
218
|
-
end
|
170
|
+
travel 30.seconds
|
219
171
|
|
220
|
-
|
221
|
-
|
222
|
-
|
172
|
+
result = render(<<-JBUILDER)
|
173
|
+
json.cache! "cache-key", expires_in: 1.minute do
|
174
|
+
json.name "Miss"
|
175
|
+
end
|
223
176
|
JBUILDER
|
224
177
|
|
225
|
-
assert_equal
|
226
|
-
end
|
178
|
+
assert_equal "Hit", result["name"]
|
227
179
|
|
228
|
-
|
229
|
-
undef_context_methods :fragment_name_with_digest, :cache_fragment_name
|
180
|
+
travel 31.seconds
|
230
181
|
|
231
|
-
|
232
|
-
json.cache! "
|
182
|
+
result = render(<<-JBUILDER)
|
183
|
+
json.cache! "cache-key", expires_in: 1.minute do
|
184
|
+
json.name "Miss"
|
233
185
|
end
|
234
186
|
JBUILDER
|
235
187
|
|
236
|
-
|
237
|
-
|
238
|
-
assert_nothing_raised do
|
239
|
-
result = jbuild(<<-JBUILDER)
|
240
|
-
json.foo "bar"
|
241
|
-
json.cache! "nothing" do
|
242
|
-
end
|
243
|
-
JBUILDER
|
244
|
-
end
|
245
|
-
|
246
|
-
assert_equal "bar", result["foo"]
|
188
|
+
assert_equal "Miss", result["name"]
|
247
189
|
end
|
248
190
|
|
249
|
-
test "
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
json.cache! "cachekey" do
|
254
|
-
json.name "Cache"
|
191
|
+
test "object root caching" do
|
192
|
+
render <<-JBUILDER
|
193
|
+
json.cache_root! "cache-key" do
|
194
|
+
json.name "Hit"
|
255
195
|
end
|
256
196
|
JBUILDER
|
257
197
|
|
258
|
-
|
259
|
-
|
198
|
+
assert_equal JSON.dump(name: "Hit"), Rails.cache.read("jbuilder/root/cache-key")
|
199
|
+
|
200
|
+
result = render(<<-JBUILDER)
|
201
|
+
json.cache_root! "cache-key" do
|
260
202
|
json.name "Miss"
|
261
203
|
end
|
262
204
|
JBUILDER
|
263
205
|
|
264
|
-
assert_equal "
|
206
|
+
assert_equal "Hit", result["name"]
|
265
207
|
end
|
266
208
|
|
267
|
-
test "
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
json.cache_if! true, "cachekey" do
|
272
|
-
json.test1 "Cache"
|
273
|
-
end
|
274
|
-
json.cache_if! false, "cachekey" do
|
275
|
-
json.test2 "Cache"
|
276
|
-
end
|
277
|
-
JBUILDER
|
278
|
-
|
279
|
-
result = jbuild(<<-JBUILDER)
|
280
|
-
json.cache_if! true, "cachekey" do
|
281
|
-
json.test1 "Miss"
|
282
|
-
end
|
283
|
-
json.cache_if! false, "cachekey" do
|
284
|
-
json.test2 "Miss"
|
209
|
+
test "array fragment caching" do
|
210
|
+
render <<-JBUILDER
|
211
|
+
json.cache! "cache-key" do
|
212
|
+
json.array! %w[ a b c ]
|
285
213
|
end
|
286
214
|
JBUILDER
|
287
215
|
|
288
|
-
assert_equal
|
289
|
-
assert_equal "Miss", result["test2"]
|
216
|
+
assert_equal %w[ a b c ], render('json.cache! "cache-key" do; end')
|
290
217
|
end
|
291
218
|
|
292
|
-
test "
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
json.cache! "cachekey" do
|
297
|
-
json.array! %w[a b c]
|
219
|
+
test "array root caching" do
|
220
|
+
render <<-JBUILDER
|
221
|
+
json.cache_root! "cache-key" do
|
222
|
+
json.array! %w[ a b c ]
|
298
223
|
end
|
299
224
|
JBUILDER
|
300
225
|
|
301
|
-
|
302
|
-
|
303
|
-
|
226
|
+
assert_equal JSON.dump(%w[ a b c ]), Rails.cache.read("jbuilder/root/cache-key")
|
227
|
+
|
228
|
+
assert_equal %w[ a b c ], render(<<-JBUILDER)
|
229
|
+
json.cache_root! "cache-key" do
|
230
|
+
json.array! %w[ d e f ]
|
304
231
|
end
|
305
232
|
JBUILDER
|
306
|
-
|
307
|
-
assert_equal %w[a b c], result
|
308
233
|
end
|
309
234
|
|
310
|
-
test "
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
JBUILDER
|
235
|
+
test "failing to cache root after JSON structures have been defined" do
|
236
|
+
assert_raises ActionView::Template::Error, "cache_root! can't be used after JSON structures have been defined" do
|
237
|
+
render <<-JBUILDER
|
238
|
+
json.name "Kaboom"
|
239
|
+
json.cache_root! "cache-key" do
|
240
|
+
json.name "Miss"
|
241
|
+
end
|
242
|
+
JBUILDER
|
243
|
+
end
|
320
244
|
end
|
321
245
|
|
322
|
-
test "fragment caching
|
323
|
-
|
246
|
+
test "empty fragment caching" do
|
247
|
+
render 'json.cache! "nothing" do; end'
|
324
248
|
|
325
|
-
|
326
|
-
ActiveSupport::Cache.expects :expand_cache_key
|
249
|
+
result = nil
|
327
250
|
|
328
|
-
|
329
|
-
|
330
|
-
json.
|
331
|
-
|
332
|
-
|
251
|
+
assert_nothing_raised do
|
252
|
+
result = render(<<-JBUILDER)
|
253
|
+
json.foo "bar"
|
254
|
+
json.cache! "nothing" do; end
|
255
|
+
JBUILDER
|
256
|
+
end
|
257
|
+
|
258
|
+
assert_equal "bar", result["foo"]
|
333
259
|
end
|
334
260
|
|
335
|
-
test "
|
336
|
-
|
261
|
+
test "cache instrumentation" do
|
262
|
+
payloads = {}
|
337
263
|
|
338
|
-
|
339
|
-
ActiveSupport::
|
264
|
+
ActiveSupport::Notifications.subscribe("read_fragment.action_controller") { |*args| payloads[:read] = args.last }
|
265
|
+
ActiveSupport::Notifications.subscribe("write_fragment.action_controller") { |*args| payloads[:write] = args.last }
|
340
266
|
|
341
|
-
|
342
|
-
json.cache! "
|
267
|
+
render <<-JBUILDER
|
268
|
+
json.cache! "cache-key" do
|
343
269
|
json.name "Cache"
|
344
270
|
end
|
345
271
|
JBUILDER
|
346
|
-
end
|
347
272
|
|
348
|
-
|
349
|
-
|
273
|
+
assert_equal "jbuilder/cache-key", payloads[:read][:key]
|
274
|
+
assert_equal "jbuilder/cache-key", payloads[:write][:key]
|
275
|
+
end
|
350
276
|
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
277
|
+
test "camelized keys" do
|
278
|
+
result = render(<<-JBUILDER)
|
279
|
+
json.key_format! camelize: [:lower]
|
280
|
+
json.first_name "David"
|
355
281
|
JBUILDER
|
356
282
|
|
357
|
-
assert_equal
|
283
|
+
assert_equal "David", result["firstName"]
|
358
284
|
end
|
359
285
|
|
360
|
-
|
361
|
-
|
286
|
+
private
|
287
|
+
def render(*args)
|
288
|
+
JSON.load render_without_parsing(*args)
|
289
|
+
end
|
362
290
|
|
363
|
-
|
364
|
-
|
365
|
-
|
291
|
+
def render_without_parsing(source, assigns = {})
|
292
|
+
view = build_view(fixtures: PARTIALS.merge("source.json.jbuilder" => source), assigns: assigns)
|
293
|
+
view.render(template: "source.json.jbuilder")
|
294
|
+
end
|
366
295
|
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
296
|
+
def build_view(options = {})
|
297
|
+
resolver = ActionView::FixtureResolver.new(options.fetch(:fixtures))
|
298
|
+
lookup_context = ActionView::LookupContext.new([ resolver ], {}, [""])
|
299
|
+
controller = ActionView::TestCase::TestController.new
|
371
300
|
|
372
|
-
|
373
|
-
|
301
|
+
# TODO: Use with_empty_template_cache unconditionally after dropping support for Rails <6.0.
|
302
|
+
view = if ActionView::Base.respond_to?(:with_empty_template_cache)
|
303
|
+
ActionView::Base.with_empty_template_cache.new(lookup_context, options.fetch(:assigns, {}), controller)
|
304
|
+
else
|
305
|
+
ActionView::Base.new(lookup_context, options.fetch(:assigns, {}), controller)
|
306
|
+
end
|
374
307
|
|
375
|
-
|
376
|
-
json.partial! @racer
|
377
|
-
JBUILDER
|
308
|
+
def view.view_cache_dependencies; []; end
|
378
309
|
|
379
|
-
|
380
|
-
|
381
|
-
assert_equal "Chris Harris", result["name"]
|
382
|
-
end
|
310
|
+
view
|
311
|
+
end
|
383
312
|
end
|