micro-lite-hub 0.0.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.
Files changed (42) hide show
  1. checksums.yaml +7 -0
  2. data/jbuilder-2.15.1/Appraisals +26 -0
  3. data/jbuilder-2.15.1/CONTRIBUTING.md +100 -0
  4. data/jbuilder-2.15.1/Gemfile +9 -0
  5. data/jbuilder-2.15.1/MIT-LICENSE +20 -0
  6. data/jbuilder-2.15.1/README.md +394 -0
  7. data/jbuilder-2.15.1/Rakefile +21 -0
  8. data/jbuilder-2.15.1/bin/release +14 -0
  9. data/jbuilder-2.15.1/bin/test +6 -0
  10. data/jbuilder-2.15.1/gemfiles/rails_7_0.gemfile +11 -0
  11. data/jbuilder-2.15.1/gemfiles/rails_7_1.gemfile +10 -0
  12. data/jbuilder-2.15.1/gemfiles/rails_7_2.gemfile +10 -0
  13. data/jbuilder-2.15.1/gemfiles/rails_8_0.gemfile +10 -0
  14. data/jbuilder-2.15.1/gemfiles/rails_8_1.gemfile +10 -0
  15. data/jbuilder-2.15.1/gemfiles/rails_head.gemfile +10 -0
  16. data/jbuilder-2.15.1/jbuilder.gemspec +35 -0
  17. data/jbuilder-2.15.1/lib/generators/rails/jbuilder_generator.rb +65 -0
  18. data/jbuilder-2.15.1/lib/generators/rails/scaffold_controller_generator.rb +24 -0
  19. data/jbuilder-2.15.1/lib/generators/rails/templates/api_controller.rb +69 -0
  20. data/jbuilder-2.15.1/lib/generators/rails/templates/controller.rb +86 -0
  21. data/jbuilder-2.15.1/lib/generators/rails/templates/index.json.jbuilder +1 -0
  22. data/jbuilder-2.15.1/lib/generators/rails/templates/partial.json.jbuilder +16 -0
  23. data/jbuilder-2.15.1/lib/generators/rails/templates/show.json.jbuilder +1 -0
  24. data/jbuilder-2.15.1/lib/jbuilder/blank.rb +13 -0
  25. data/jbuilder-2.15.1/lib/jbuilder/collection_renderer.rb +58 -0
  26. data/jbuilder-2.15.1/lib/jbuilder/errors.rb +26 -0
  27. data/jbuilder-2.15.1/lib/jbuilder/jbuilder.rb +3 -0
  28. data/jbuilder-2.15.1/lib/jbuilder/jbuilder_dependency_tracker.rb +75 -0
  29. data/jbuilder-2.15.1/lib/jbuilder/jbuilder_template.rb +264 -0
  30. data/jbuilder-2.15.1/lib/jbuilder/key_formatter.rb +32 -0
  31. data/jbuilder-2.15.1/lib/jbuilder/railtie.rb +34 -0
  32. data/jbuilder-2.15.1/lib/jbuilder/version.rb +5 -0
  33. data/jbuilder-2.15.1/lib/jbuilder.rb +384 -0
  34. data/jbuilder-2.15.1/test/jbuilder_dependency_tracker_test.rb +71 -0
  35. data/jbuilder-2.15.1/test/jbuilder_generator_test.rb +68 -0
  36. data/jbuilder-2.15.1/test/jbuilder_template_test.rb +469 -0
  37. data/jbuilder-2.15.1/test/jbuilder_test.rb +954 -0
  38. data/jbuilder-2.15.1/test/scaffold_api_controller_generator_test.rb +83 -0
  39. data/jbuilder-2.15.1/test/scaffold_controller_generator_test.rb +116 -0
  40. data/jbuilder-2.15.1/test/test_helper.rb +47 -0
  41. data/micro-lite-hub.gemspec +11 -0
  42. metadata +80 -0
@@ -0,0 +1,469 @@
1
+ require "test_helper"
2
+ require "action_view/testing/resolvers"
3
+
4
+ class JbuilderTemplateTest < ActiveSupport::TestCase
5
+ POST_PARTIAL = <<-JBUILDER
6
+ json.extract! post, :id, :body
7
+ json.title post.title if local_assigns.fetch(:include_title, false)
8
+ json.author do
9
+ first_name, last_name = post.author_name.split(nil, 2)
10
+ json.first_name first_name
11
+ json.last_name last_name
12
+ end
13
+ JBUILDER
14
+
15
+ COLLECTION_PARTIAL = <<-JBUILDER
16
+ json.extract! collection, :id, :name
17
+ JBUILDER
18
+
19
+ RACER_PARTIAL = <<-JBUILDER
20
+ json.extract! racer, :id, :name
21
+ json.highlighted local_assigns.fetch(:highlighted, false)
22
+ JBUILDER
23
+
24
+ PARTIALS = {
25
+ "_partial.json.jbuilder" => "json.content content",
26
+ "_post.json.jbuilder" => POST_PARTIAL,
27
+ "racers/_racer.json.jbuilder" => RACER_PARTIAL,
28
+ "_collection.json.jbuilder" => COLLECTION_PARTIAL,
29
+
30
+ # Ensure we find only Jbuilder partials from within Jbuilder templates.
31
+ "_post.html.erb" => "Hello world!"
32
+ }
33
+
34
+ AUTHORS = [ "David Heinemeier Hansson", "Pavel Pravosud" ].cycle
35
+ POSTS = (1..10).collect { |i| Post.new(i, "Title #{i}", "Post ##{i}", AUTHORS.next) }
36
+
37
+ setup { Rails.cache.clear }
38
+
39
+ test "basic template" do
40
+ result = render('json.content "hello"')
41
+ assert_equal "hello", result["content"]
42
+ end
43
+
44
+ test "method_missing can be used as a key" do
45
+ result = render('json.method_missing "hello"')
46
+ assert_equal({ "method_missing" => "hello" }, result)
47
+ end
48
+
49
+ test "partial by name with top-level locals" do
50
+ result = render('json.partial! "partial", content: "hello"')
51
+ assert_equal "hello", result["content"]
52
+ end
53
+
54
+ test "partial by name with nested locals" do
55
+ result = render('json.partial! "partial", locals: { content: "hello" }')
56
+ assert_equal "hello", result["content"]
57
+ end
58
+
59
+ test "partial by name with hash value omission (punning) as last statement [3.1+]" do
60
+ major, minor, _ = RUBY_VERSION.split(".").map(&:to_i)
61
+ return unless (major == 3 && minor >= 1) || major > 3
62
+
63
+ result = render(<<-JBUILDER)
64
+ content = "hello"
65
+ json.partial! "partial", content:
66
+ JBUILDER
67
+ assert_equal "hello", result["content"]
68
+ end
69
+
70
+ test "partial by options containing nested locals" do
71
+ result = render('json.partial! partial: "partial", locals: { content: "hello" }')
72
+ assert_equal "hello", result["content"]
73
+ end
74
+
75
+ test "partial by options containing top-level locals" do
76
+ result = render('json.partial! partial: "partial", content: "hello"')
77
+ assert_equal "hello", result["content"]
78
+ end
79
+
80
+ test "partial for Active Model" do
81
+ result = render('json.partial! @racer', racer: Racer.new(123, "Chris Harris"))
82
+ assert_equal 123, result["id"]
83
+ assert_equal "Chris Harris", result["name"]
84
+ end
85
+
86
+ test "partial for Active Model preserves extra locals" do
87
+ result = render('json.partial! @racer, highlighted: true', racer: Racer.new(123, "Chris Harris"))
88
+ assert_equal 123, result["id"]
89
+ assert_equal "Chris Harris", result["name"]
90
+ assert_equal true, result["highlighted"]
91
+ end
92
+
93
+ test "partial collection by name with symbol local" do
94
+ result = render('json.partial! "post", collection: @posts, as: :post', posts: POSTS)
95
+ assert_equal 10, result.count
96
+ assert_equal "Post #5", result[4]["body"]
97
+ assert_equal "Heinemeier Hansson", result[2]["author"]["last_name"]
98
+ assert_equal "Pavel", result[5]["author"]["first_name"]
99
+ end
100
+
101
+ test "partial collection by name with caching" do
102
+ result = render('json.partial! "post", collection: @posts, cached: true, as: :post', posts: POSTS)
103
+ assert_equal 10, result.count
104
+ assert_equal "Post #5", result[4]["body"]
105
+ assert_equal "Heinemeier Hansson", result[2]["author"]["last_name"]
106
+ assert_equal "Pavel", result[5]["author"]["first_name"]
107
+ end
108
+
109
+ test "partial collection by name with string local" do
110
+ result = render('json.partial! "post", collection: @posts, as: "post"', posts: POSTS)
111
+ assert_equal 10, result.count
112
+ assert_equal "Post #5", result[4]["body"]
113
+ assert_equal "Heinemeier Hansson", result[2]["author"]["last_name"]
114
+ assert_equal "Pavel", result[5]["author"]["first_name"]
115
+ end
116
+
117
+ test "partial collection by options" do
118
+ result = render('json.partial! partial: "post", collection: @posts, as: :post', posts: POSTS)
119
+ assert_equal 10, result.count
120
+ assert_equal "Post #5", result[4]["body"]
121
+ assert_equal "Heinemeier Hansson", result[2]["author"]["last_name"]
122
+ assert_equal "Pavel", result[5]["author"]["first_name"]
123
+ end
124
+
125
+ test "nil partial collection by name" do
126
+ Jbuilder::CollectionRenderer.expects(:new).never
127
+ assert_equal [], render('json.partial! "post", collection: @posts, as: :post', posts: nil)
128
+ end
129
+
130
+ test "nil partial collection by options" do
131
+ Jbuilder::CollectionRenderer.expects(:new).never
132
+ assert_equal [], render('json.partial! partial: "post", collection: @posts, as: :post', posts: nil)
133
+ end
134
+
135
+ test "array of partials" do
136
+ result = render('json.array! @posts, partial: "post", as: :post', posts: POSTS)
137
+ assert_equal 10, result.count
138
+ assert_equal "Post #5", result[4]["body"]
139
+ assert_equal "Heinemeier Hansson", result[2]["author"]["last_name"]
140
+ assert_equal "Pavel", result[5]["author"]["first_name"]
141
+ end
142
+
143
+ test "empty array of partials from empty collection" do
144
+ Jbuilder::CollectionRenderer.expects(:new).never
145
+ assert_equal [], render('json.array! @posts, partial: "post", as: :post', posts: [])
146
+ end
147
+
148
+ test "empty array of partials from nil collection" do
149
+ Jbuilder::CollectionRenderer.expects(:new).never
150
+ assert_equal [], render('json.array! @posts, partial: "post", as: :post', posts: nil)
151
+ end
152
+
153
+ test "single partial under key" do
154
+ result = render('json.post @post, partial: "post", as: :post', post: POSTS.first)
155
+ assert_equal "Post #1", result["post"]["body"]
156
+ assert_equal "Heinemeier Hansson", result["post"]["author"]["last_name"]
157
+ assert_equal "David", result["post"]["author"]["first_name"]
158
+ end
159
+
160
+ test 'single partial under key with local' do
161
+ result = render('json.post @post, partial: "post", as: :post, include_title: true', post: POSTS.first)
162
+ assert_equal "Title 1", result["post"]["title"]
163
+ end
164
+
165
+ test "array of partials under key" do
166
+ result = render('json.posts @posts, partial: "post", as: :post', posts: POSTS)
167
+ assert_equal 10, result["posts"].count
168
+ assert_equal "Post #5", result["posts"][4]["body"]
169
+ assert_equal "Heinemeier Hansson", result["posts"][2]["author"]["last_name"]
170
+ assert_equal "Pavel", result["posts"][5]["author"]["first_name"]
171
+ end
172
+
173
+ test "array of partials under key with local" do
174
+ result = render('json.posts @posts, partial: "post", as: :post, include_title: true', posts: POSTS)
175
+ assert_equal "Title 1", result["posts"][0]["title"]
176
+ assert_equal "Title 2", result["posts"][1]["title"]
177
+ end
178
+
179
+ test "empty array of partials under key from nil collection" do
180
+ Jbuilder::CollectionRenderer.expects(:new).never
181
+ result = render('json.posts @posts, partial: "post", as: :post', posts: nil)
182
+ assert_equal [], result["posts"]
183
+ end
184
+
185
+ test "empty array of partials under key from an empty collection" do
186
+ Jbuilder::CollectionRenderer.expects(:new).never
187
+ result = render('json.posts @posts, partial: "post", as: :post', posts: [])
188
+ assert_equal [], result["posts"]
189
+ end
190
+
191
+ test "object fragment caching" do
192
+ render(<<-JBUILDER)
193
+ json.cache! "cache-key" do
194
+ json.name "Hit"
195
+ end
196
+ JBUILDER
197
+
198
+ hit = render('json.cache! "cache-key" do; end')
199
+ assert_equal "Hit", hit["name"]
200
+ end
201
+
202
+ test "conditional object fragment caching" do
203
+ render(<<-JBUILDER)
204
+ json.cache_if! true, "cache-key" do
205
+ json.a "Hit"
206
+ end
207
+
208
+ json.cache_if! false, "cache-key" do
209
+ json.b "Hit"
210
+ end
211
+ JBUILDER
212
+
213
+ result = render(<<-JBUILDER)
214
+ json.cache_if! true, "cache-key" do
215
+ json.a "Miss"
216
+ end
217
+
218
+ json.cache_if! false, "cache-key" do
219
+ json.b "Miss"
220
+ end
221
+ JBUILDER
222
+
223
+ assert_equal "Hit", result["a"]
224
+ assert_equal "Miss", result["b"]
225
+ end
226
+
227
+ test "object fragment caching with expiry" do
228
+ travel_to Time.iso8601("2018-05-12T11:29:00-04:00")
229
+
230
+ render <<-JBUILDER
231
+ json.cache! "cache-key", expires_in: 1.minute do
232
+ json.name "Hit"
233
+ end
234
+ JBUILDER
235
+
236
+ travel 30.seconds
237
+
238
+ result = render(<<-JBUILDER)
239
+ json.cache! "cache-key", expires_in: 1.minute do
240
+ json.name "Miss"
241
+ end
242
+ JBUILDER
243
+
244
+ assert_equal "Hit", result["name"]
245
+
246
+ travel 31.seconds
247
+
248
+ result = render(<<-JBUILDER)
249
+ json.cache! "cache-key", expires_in: 1.minute do
250
+ json.name "Miss"
251
+ end
252
+ JBUILDER
253
+
254
+ assert_equal "Miss", result["name"]
255
+ end
256
+
257
+ test "object root caching" do
258
+ render <<-JBUILDER
259
+ json.cache_root! "cache-key" do
260
+ json.name "Hit"
261
+ end
262
+ JBUILDER
263
+
264
+ assert_equal JSON.dump(name: "Hit"), Rails.cache.read("jbuilder/root/cache-key")
265
+
266
+ result = render(<<-JBUILDER)
267
+ json.cache_root! "cache-key" do
268
+ json.name "Miss"
269
+ end
270
+ JBUILDER
271
+
272
+ assert_equal "Hit", result["name"]
273
+ end
274
+
275
+ test "array fragment caching" do
276
+ render <<-JBUILDER
277
+ json.cache! "cache-key" do
278
+ json.array! %w[ a b c ]
279
+ end
280
+ JBUILDER
281
+
282
+ assert_equal %w[ a b c ], render('json.cache! "cache-key" do; end')
283
+ end
284
+
285
+ test "array root caching" do
286
+ render <<-JBUILDER
287
+ json.cache_root! "cache-key" do
288
+ json.array! %w[ a b c ]
289
+ end
290
+ JBUILDER
291
+
292
+ assert_equal JSON.dump(%w[ a b c ]), Rails.cache.read("jbuilder/root/cache-key")
293
+
294
+ assert_equal %w[ a b c ], render(<<-JBUILDER)
295
+ json.cache_root! "cache-key" do
296
+ json.array! %w[ d e f ]
297
+ end
298
+ JBUILDER
299
+ end
300
+
301
+ test "failing to cache root after JSON structures have been defined" do
302
+ assert_raises ActionView::Template::Error, "cache_root! can't be used after JSON structures have been defined" do
303
+ render <<-JBUILDER
304
+ json.name "Kaboom"
305
+ json.cache_root! "cache-key" do
306
+ json.name "Miss"
307
+ end
308
+ JBUILDER
309
+ end
310
+ end
311
+
312
+ test "empty fragment caching" do
313
+ render 'json.cache! "nothing" do; end'
314
+
315
+ result = nil
316
+
317
+ assert_nothing_raised do
318
+ result = render(<<-JBUILDER)
319
+ json.foo "bar"
320
+ json.cache! "nothing" do; end
321
+ JBUILDER
322
+ end
323
+
324
+ assert_equal "bar", result["foo"]
325
+ end
326
+
327
+ test "cache instrumentation" do
328
+ payloads = {}
329
+
330
+ ActiveSupport::Notifications.subscribe("read_fragment.action_controller") { |*args| payloads[:read] = args.last }
331
+ ActiveSupport::Notifications.subscribe("write_fragment.action_controller") { |*args| payloads[:write] = args.last }
332
+
333
+ render <<-JBUILDER
334
+ json.cache! "cache-key" do
335
+ json.name "Cache"
336
+ end
337
+ JBUILDER
338
+
339
+ assert_equal "jbuilder/cache-key", payloads[:read][:key]
340
+ assert_equal "jbuilder/cache-key", payloads[:write][:key]
341
+ end
342
+
343
+ test "camelized keys" do
344
+ result = render(<<-JBUILDER)
345
+ json.key_format! camelize: [:lower]
346
+ json.first_name "David"
347
+ JBUILDER
348
+
349
+ assert_equal "David", result["firstName"]
350
+ end
351
+
352
+ test "returns an empty array for an empty collection" do
353
+ Jbuilder::CollectionRenderer.expects(:new).never
354
+ result = render('json.array! @posts, partial: "post", as: :post, cached: true', posts: [])
355
+
356
+ # Do not use #assert_empty as it is important to ensure that the type of the JSON result is an array.
357
+ assert_equal [], result
358
+ end
359
+
360
+ test "works with an enumerable object" do
361
+ enumerable_class = Class.new do
362
+ include Enumerable
363
+
364
+ def each(&block)
365
+ [].each(&block)
366
+ end
367
+ end
368
+
369
+ result = render('json.array! @posts, partial: "post", as: :post, cached: true', posts: enumerable_class.new)
370
+
371
+ # Do not use #assert_empty as it is important to ensure that the type of the JSON result is an array.
372
+ assert_equal [], result
373
+ end
374
+
375
+ test "supports the cached: true option" do
376
+ result = render('json.array! @posts, partial: "post", as: :post, cached: true', posts: POSTS)
377
+
378
+ assert_equal 10, result.count
379
+ assert_equal "Post #5", result[4]["body"]
380
+ assert_equal "Heinemeier Hansson", result[2]["author"]["last_name"]
381
+ assert_equal "Pavel", result[5]["author"]["first_name"]
382
+
383
+ expected = {
384
+ "id" => 1,
385
+ "body" => "Post #1",
386
+ "author" => {
387
+ "first_name" => "David",
388
+ "last_name" => "Heinemeier Hansson"
389
+ }
390
+ }
391
+
392
+ assert_equal expected, Rails.cache.read("post-1")
393
+
394
+ result = render('json.array! @posts, partial: "post", as: :post, cached: true', posts: POSTS)
395
+
396
+ assert_equal 10, result.count
397
+ assert_equal "Post #5", result[4]["body"]
398
+ assert_equal "Heinemeier Hansson", result[2]["author"]["last_name"]
399
+ assert_equal "Pavel", result[5]["author"]["first_name"]
400
+ end
401
+
402
+ test "supports the cached: ->() {} option" do
403
+ result = render('json.array! @posts, partial: "post", as: :post, cached: ->(post) { [post, "foo"] }', posts: POSTS)
404
+
405
+ assert_equal 10, result.count
406
+ assert_equal "Post #5", result[4]["body"]
407
+ assert_equal "Heinemeier Hansson", result[2]["author"]["last_name"]
408
+ assert_equal "Pavel", result[5]["author"]["first_name"]
409
+
410
+ expected = {
411
+ "id" => 1,
412
+ "body" => "Post #1",
413
+ "author" => {
414
+ "first_name" => "David",
415
+ "last_name" => "Heinemeier Hansson"
416
+ }
417
+ }
418
+
419
+ assert_equal expected, Rails.cache.read("post-1/foo")
420
+
421
+ result = render('json.array! @posts, partial: "post", as: :post, cached: ->(post) { [post, "foo"] }', posts: POSTS)
422
+
423
+ assert_equal 10, result.count
424
+ assert_equal "Post #5", result[4]["body"]
425
+ assert_equal "Heinemeier Hansson", result[2]["author"]["last_name"]
426
+ assert_equal "Pavel", result[5]["author"]["first_name"]
427
+ end
428
+
429
+ test "raises an error on a render call with the :layout option" do
430
+ error = assert_raises NotImplementedError do
431
+ render('json.array! @posts, partial: "post", as: :post, layout: "layout"', posts: POSTS)
432
+ end
433
+
434
+ assert_equal "The `:layout' option is not supported in collection rendering.", error.message
435
+ end
436
+
437
+ test "raises an error on a render call with the :spacer_template option" do
438
+ error = assert_raises NotImplementedError do
439
+ render('json.array! @posts, partial: "post", as: :post, spacer_template: "template"', posts: POSTS)
440
+ end
441
+
442
+ assert_equal "The `:spacer_template' option is not supported in collection rendering.", error.message
443
+ end
444
+
445
+ private
446
+ def render(*args)
447
+ JSON.load render_without_parsing(*args)
448
+ end
449
+
450
+ def render_without_parsing(source, assigns = {})
451
+ view = build_view(fixtures: PARTIALS.merge("source.json.jbuilder" => source), assigns: assigns)
452
+ view.render(template: "source")
453
+ end
454
+
455
+ def build_view(options = {})
456
+ resolver = ActionView::FixtureResolver.new(options.fetch(:fixtures))
457
+ lookup_context = ActionView::LookupContext.new([ resolver ], {}, [""])
458
+ controller = ActionView::TestCase::TestController.new
459
+
460
+ view = ActionView::Base.with_empty_template_cache.new(lookup_context, options.fetch(:assigns, {}), controller)
461
+
462
+ def view.view_cache_dependencies; []; end
463
+ def view.combined_fragment_cache_key(key) [ key ] end
464
+ def view.cache_fragment_name(key, *) key end
465
+ def view.fragment_name_with_digest(key) key end
466
+
467
+ view
468
+ end
469
+ end