jbuilder 2.8.0 → 2.11.2
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 +4 -4
- data/.gitignore +2 -0
- data/.travis.yml +31 -35
- data/Appraisals +15 -7
- data/CHANGELOG.md +50 -0
- data/CONTRIBUTING.md +1 -3
- data/Gemfile +0 -1
- data/README.md +34 -12
- data/gemfiles/rails_5_0.gemfile +1 -2
- data/gemfiles/rails_5_1.gemfile +1 -2
- data/gemfiles/{rails_4_2.gemfile → rails_5_2.gemfile} +1 -2
- data/gemfiles/rails_6_0.gemfile +10 -0
- data/gemfiles/rails_head.gemfile +10 -0
- data/jbuilder.gemspec +9 -4
- data/lib/generators/rails/jbuilder_generator.rb +12 -2
- data/lib/generators/rails/scaffold_controller_generator.rb +6 -0
- data/lib/generators/rails/templates/api_controller.rb +4 -4
- data/lib/generators/rails/templates/controller.rb +14 -19
- data/lib/generators/rails/templates/index.json.jbuilder +1 -1
- data/lib/generators/rails/templates/partial.json.jbuilder +15 -1
- data/lib/jbuilder.rb +64 -23
- data/lib/jbuilder/jbuilder_template.rb +8 -7
- data/test/jbuilder_generator_test.rb +25 -5
- data/test/jbuilder_template_test.rb +193 -338
- data/test/jbuilder_test.rb +227 -2
- data/test/scaffold_api_controller_generator_test.rb +17 -2
- data/test/scaffold_controller_generator_test.rb +21 -7
- data/test/test_helper.rb +25 -7
- metadata +9 -22
|
@@ -1,19 +1,17 @@
|
|
|
1
1
|
<% if namespaced? -%>
|
|
2
|
-
require_dependency "<%=
|
|
2
|
+
require_dependency "<%= namespaced_path %>/application_controller"
|
|
3
3
|
|
|
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,42 +24,39 @@ 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 @<%= singular_table_name %>, notice: <%= "
|
|
33
|
+
format.html { redirect_to @<%= singular_table_name %>, notice: <%= %("#{human_name} was successfully created.") %> }
|
|
37
34
|
format.json { render :show, status: :created, location: <%= "@#{singular_table_name}" %> }
|
|
38
35
|
else
|
|
39
|
-
format.html { render :new }
|
|
36
|
+
format.html { render :new, status: :unprocessable_entity }
|
|
40
37
|
format.json { render json: <%= "@#{orm_instance.errors}" %>, status: :unprocessable_entity }
|
|
41
38
|
end
|
|
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 @<%= singular_table_name %>, notice: <%= "
|
|
46
|
+
format.html { redirect_to @<%= singular_table_name %>, notice: <%= %("#{human_name} was successfully updated.") %> }
|
|
51
47
|
format.json { render :show, status: :ok, location: <%= "@#{singular_table_name}" %> }
|
|
52
48
|
else
|
|
53
|
-
format.html { render :edit }
|
|
49
|
+
format.html { render :edit, status: :unprocessable_entity }
|
|
54
50
|
format.json { render json: <%= "@#{orm_instance.errors}" %>, status: :unprocessable_entity }
|
|
55
51
|
end
|
|
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 %>
|
|
63
58
|
respond_to do |format|
|
|
64
|
-
format.html { redirect_to <%= index_helper %>_url, notice: <%= "
|
|
59
|
+
format.html { redirect_to <%= index_helper %>_url, notice: <%= %("#{human_name} was successfully destroyed.") %> }
|
|
65
60
|
format.json { head :no_content }
|
|
66
61
|
end
|
|
67
62
|
end
|
|
@@ -72,12 +67,12 @@ class <%= controller_class_name %>Controller < ApplicationController
|
|
|
72
67
|
@<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
|
|
73
68
|
end
|
|
74
69
|
|
|
75
|
-
#
|
|
70
|
+
# Only allow a list of trusted parameters through.
|
|
76
71
|
def <%= "#{singular_table_name}_params" %>
|
|
77
72
|
<%- if attributes_names.empty? -%>
|
|
78
73
|
params.fetch(<%= ":#{singular_table_name}" %>, {})
|
|
79
74
|
<%- else -%>
|
|
80
|
-
params.require(<%= ":#{singular_table_name}" %>).permit(<%=
|
|
75
|
+
params.require(<%= ":#{singular_table_name}" %>).permit(<%= permitted_params %>)
|
|
81
76
|
<%- end -%>
|
|
82
77
|
end
|
|
83
78
|
end
|
|
@@ -1 +1 @@
|
|
|
1
|
-
json.array! @<%= plural_table_name %>, partial:
|
|
1
|
+
json.array! @<%= plural_table_name %>, partial: "<%= plural_table_name %>/<%= singular_table_name %>", as: :<%= singular_table_name %>
|
|
@@ -1,2 +1,16 @@
|
|
|
1
|
-
json.extract! <%= singular_table_name %>, <%=
|
|
1
|
+
json.extract! <%= singular_table_name %>, <%= full_attributes_list %>
|
|
2
2
|
json.url <%= singular_table_name %>_url(<%= singular_table_name %>, format: :json)
|
|
3
|
+
<%- virtual_attributes.each do |attribute| -%>
|
|
4
|
+
<%- if attribute.type == :rich_text -%>
|
|
5
|
+
json.<%= attribute.name %> <%= singular_table_name %>.<%= attribute.name %>.to_s
|
|
6
|
+
<%- elsif attribute.type == :attachment -%>
|
|
7
|
+
json.<%= attribute.name %> url_for(<%= singular_table_name %>.<%= attribute.name %>)
|
|
8
|
+
<%- elsif attribute.type == :attachments -%>
|
|
9
|
+
json.<%= attribute.name %> do
|
|
10
|
+
json.array!(<%= singular_table_name %>.<%= attribute.name %>) do |<%= attribute.singular_name %>|
|
|
11
|
+
json.id <%= attribute.singular_name %>.id
|
|
12
|
+
json.url url_for(<%= attribute.singular_name %>)
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
<%- end -%>
|
|
16
|
+
<%- end -%>
|
data/lib/jbuilder.rb
CHANGED
|
@@ -2,18 +2,21 @@ require 'jbuilder/jbuilder'
|
|
|
2
2
|
require 'jbuilder/blank'
|
|
3
3
|
require 'jbuilder/key_formatter'
|
|
4
4
|
require 'jbuilder/errors'
|
|
5
|
-
require '
|
|
5
|
+
require 'json'
|
|
6
6
|
require 'ostruct'
|
|
7
|
+
require 'active_support/core_ext/hash/deep_merge'
|
|
7
8
|
|
|
8
9
|
class Jbuilder
|
|
9
10
|
@@key_formatter = nil
|
|
10
11
|
@@ignore_nil = false
|
|
12
|
+
@@deep_format_keys = false
|
|
11
13
|
|
|
12
14
|
def initialize(options = {})
|
|
13
15
|
@attributes = {}
|
|
14
16
|
|
|
15
17
|
@key_formatter = options.fetch(:key_formatter){ @@key_formatter ? @@key_formatter.clone : nil}
|
|
16
18
|
@ignore_nil = options.fetch(:ignore_nil, @@ignore_nil)
|
|
19
|
+
@deep_format_keys = options.fetch(:deep_format_keys, @@deep_format_keys)
|
|
17
20
|
|
|
18
21
|
yield self if ::Kernel.block_given?
|
|
19
22
|
end
|
|
@@ -26,12 +29,12 @@ class Jbuilder
|
|
|
26
29
|
BLANK = Blank.new
|
|
27
30
|
NON_ENUMERABLES = [ ::Struct, ::OpenStruct ].to_set
|
|
28
31
|
|
|
29
|
-
def set!(key, value = BLANK, *args)
|
|
32
|
+
def set!(key, value = BLANK, *args, &block)
|
|
30
33
|
result = if ::Kernel.block_given?
|
|
31
34
|
if !_blank?(value)
|
|
32
35
|
# json.comments @post.comments { |comment| ... }
|
|
33
36
|
# { "comments": [ { ... }, { ... } ] }
|
|
34
|
-
_scope{ array! value,
|
|
37
|
+
_scope{ array! value, &block }
|
|
35
38
|
else
|
|
36
39
|
# json.comments { ... }
|
|
37
40
|
# { "comments": ... }
|
|
@@ -42,11 +45,11 @@ class Jbuilder
|
|
|
42
45
|
# json.age 32
|
|
43
46
|
# json.person another_jbuilder
|
|
44
47
|
# { "age": 32, "person": { ... }
|
|
45
|
-
value.attributes!
|
|
48
|
+
_format_keys(value.attributes!)
|
|
46
49
|
else
|
|
47
50
|
# json.age 32
|
|
48
51
|
# { "age": 32 }
|
|
49
|
-
value
|
|
52
|
+
_format_keys(value)
|
|
50
53
|
end
|
|
51
54
|
elsif _is_collection?(value)
|
|
52
55
|
# json.comments @post.comments, :content, :created_at
|
|
@@ -61,9 +64,9 @@ class Jbuilder
|
|
|
61
64
|
_set_value key, result
|
|
62
65
|
end
|
|
63
66
|
|
|
64
|
-
def method_missing(*args)
|
|
67
|
+
def method_missing(*args, &block)
|
|
65
68
|
if ::Kernel.block_given?
|
|
66
|
-
set!(*args,
|
|
69
|
+
set!(*args, &block)
|
|
67
70
|
else
|
|
68
71
|
set!(*args)
|
|
69
72
|
end
|
|
@@ -130,6 +133,31 @@ class Jbuilder
|
|
|
130
133
|
@@ignore_nil = value
|
|
131
134
|
end
|
|
132
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
|
+
|
|
133
161
|
# Turns the current element into an array and yields a builder to add a hash.
|
|
134
162
|
#
|
|
135
163
|
# Example:
|
|
@@ -163,7 +191,7 @@ class Jbuilder
|
|
|
163
191
|
#
|
|
164
192
|
# [ { "name": David", "age": 32 }, { "name": Jamie", "age": 31 } ]
|
|
165
193
|
#
|
|
166
|
-
#
|
|
194
|
+
# You can use the call syntax instead of an explicit extract! call:
|
|
167
195
|
#
|
|
168
196
|
# json.(@people) { |person| ... }
|
|
169
197
|
#
|
|
@@ -181,18 +209,18 @@ class Jbuilder
|
|
|
181
209
|
# json.array! [1, 2, 3]
|
|
182
210
|
#
|
|
183
211
|
# [1,2,3]
|
|
184
|
-
def array!(collection = [], *attributes)
|
|
212
|
+
def array!(collection = [], *attributes, &block)
|
|
185
213
|
array = if collection.nil?
|
|
186
214
|
[]
|
|
187
215
|
elsif ::Kernel.block_given?
|
|
188
|
-
_map_collection(collection,
|
|
216
|
+
_map_collection(collection, &block)
|
|
189
217
|
elsif attributes.any?
|
|
190
218
|
_map_collection(collection) { |element| extract! element, *attributes }
|
|
191
219
|
else
|
|
192
|
-
collection.to_a
|
|
220
|
+
_format_keys(collection.to_a)
|
|
193
221
|
end
|
|
194
222
|
|
|
195
|
-
|
|
223
|
+
@attributes = _merge_values(@attributes, array)
|
|
196
224
|
end
|
|
197
225
|
|
|
198
226
|
# Extracts the mentioned attributes or hash elements from the passed object and turns them into attributes of the JSON.
|
|
@@ -220,9 +248,9 @@ class Jbuilder
|
|
|
220
248
|
end
|
|
221
249
|
end
|
|
222
250
|
|
|
223
|
-
def call(object, *attributes)
|
|
251
|
+
def call(object, *attributes, &block)
|
|
224
252
|
if ::Kernel.block_given?
|
|
225
|
-
array! object,
|
|
253
|
+
array! object, &block
|
|
226
254
|
else
|
|
227
255
|
extract! object, *attributes
|
|
228
256
|
end
|
|
@@ -240,24 +268,25 @@ class Jbuilder
|
|
|
240
268
|
@attributes
|
|
241
269
|
end
|
|
242
270
|
|
|
243
|
-
# Merges hash or
|
|
244
|
-
def merge!(
|
|
245
|
-
|
|
271
|
+
# Merges hash, array, or Jbuilder instance into current builder.
|
|
272
|
+
def merge!(object)
|
|
273
|
+
hash_or_array = ::Jbuilder === object ? object.attributes! : object
|
|
274
|
+
@attributes = _merge_values(@attributes, _format_keys(hash_or_array))
|
|
246
275
|
end
|
|
247
276
|
|
|
248
277
|
# Encodes the current builder as JSON.
|
|
249
278
|
def target!
|
|
250
|
-
|
|
279
|
+
@attributes.to_json
|
|
251
280
|
end
|
|
252
281
|
|
|
253
282
|
private
|
|
254
283
|
|
|
255
284
|
def _extract_hash_values(object, attributes)
|
|
256
|
-
attributes.each{ |key| _set_value key, object.fetch(key) }
|
|
285
|
+
attributes.each{ |key| _set_value key, _format_keys(object.fetch(key)) }
|
|
257
286
|
end
|
|
258
287
|
|
|
259
288
|
def _extract_method_values(object, attributes)
|
|
260
|
-
attributes.each{ |key| _set_value key, object.public_send(key) }
|
|
289
|
+
attributes.each{ |key| _set_value key, _format_keys(object.public_send(key)) }
|
|
261
290
|
end
|
|
262
291
|
|
|
263
292
|
def _merge_block(key)
|
|
@@ -275,7 +304,7 @@ class Jbuilder
|
|
|
275
304
|
elsif ::Array === current_value && ::Array === updates
|
|
276
305
|
current_value + updates
|
|
277
306
|
elsif ::Hash === current_value && ::Hash === updates
|
|
278
|
-
current_value.
|
|
307
|
+
current_value.deep_merge(updates)
|
|
279
308
|
else
|
|
280
309
|
raise MergeError.build(current_value, updates)
|
|
281
310
|
end
|
|
@@ -285,6 +314,18 @@ class Jbuilder
|
|
|
285
314
|
@key_formatter ? @key_formatter.format(key) : key.to_s
|
|
286
315
|
end
|
|
287
316
|
|
|
317
|
+
def _format_keys(hash_or_array)
|
|
318
|
+
return hash_or_array unless @deep_format_keys
|
|
319
|
+
|
|
320
|
+
if ::Array === hash_or_array
|
|
321
|
+
hash_or_array.map { |value| _format_keys(value) }
|
|
322
|
+
elsif ::Hash === hash_or_array
|
|
323
|
+
::Hash[hash_or_array.collect { |k, v| [_key(k), _format_keys(v)] }]
|
|
324
|
+
else
|
|
325
|
+
hash_or_array
|
|
326
|
+
end
|
|
327
|
+
end
|
|
328
|
+
|
|
288
329
|
def _set_value(key, value)
|
|
289
330
|
raise NullError.build(key) if @attributes.nil?
|
|
290
331
|
raise ArrayError.build(key) if ::Array === @attributes
|
|
@@ -300,12 +341,12 @@ class Jbuilder
|
|
|
300
341
|
end
|
|
301
342
|
|
|
302
343
|
def _scope
|
|
303
|
-
parent_attributes, parent_formatter = @attributes, @key_formatter
|
|
344
|
+
parent_attributes, parent_formatter, parent_deep_format_keys = @attributes, @key_formatter, @deep_format_keys
|
|
304
345
|
@attributes = BLANK
|
|
305
346
|
yield
|
|
306
347
|
@attributes
|
|
307
348
|
ensure
|
|
308
|
-
@attributes, @key_formatter = parent_attributes, parent_formatter
|
|
349
|
+
@attributes, @key_formatter, @deep_format_keys = parent_attributes, parent_formatter, parent_deep_format_keys
|
|
309
350
|
end
|
|
310
351
|
|
|
311
352
|
def _is_collection?(object)
|
|
@@ -73,8 +73,8 @@ class JbuilderTemplate < Jbuilder
|
|
|
73
73
|
# json.cache_if! !admin?, @person, expires_in: 10.minutes do
|
|
74
74
|
# json.extract! @person, :name, :age
|
|
75
75
|
# end
|
|
76
|
-
def cache_if!(condition, *args)
|
|
77
|
-
condition ? cache!(*args,
|
|
76
|
+
def cache_if!(condition, *args, &block)
|
|
77
|
+
condition ? cache!(*args, &block) : yield
|
|
78
78
|
end
|
|
79
79
|
|
|
80
80
|
def target!
|
|
@@ -104,7 +104,7 @@ class JbuilderTemplate < Jbuilder
|
|
|
104
104
|
private
|
|
105
105
|
|
|
106
106
|
def _render_partial_with_options(options)
|
|
107
|
-
options.reverse_merge! locals:
|
|
107
|
+
options.reverse_merge! locals: options.except(:partial, :as, :collection)
|
|
108
108
|
options.reverse_merge! ::JbuilderTemplate.template_lookup_options
|
|
109
109
|
as = options[:as]
|
|
110
110
|
|
|
@@ -164,7 +164,7 @@ class JbuilderTemplate < Jbuilder
|
|
|
164
164
|
if @context.respond_to?(:cache_fragment_name)
|
|
165
165
|
# Current compatibility, fragment_name_with_digest is private again and cache_fragment_name
|
|
166
166
|
# should be used instead.
|
|
167
|
-
@context.cache_fragment_name(key, options)
|
|
167
|
+
@context.cache_fragment_name(key, **options)
|
|
168
168
|
elsif @context.respond_to?(:fragment_name_with_digest)
|
|
169
169
|
# Backwards compatibility for period of time when fragment_name_with_digest was made public.
|
|
170
170
|
@context.fragment_name_with_digest(key)
|
|
@@ -222,11 +222,12 @@ end
|
|
|
222
222
|
|
|
223
223
|
class JbuilderHandler
|
|
224
224
|
cattr_accessor :default_format
|
|
225
|
-
self.default_format =
|
|
225
|
+
self.default_format = :json
|
|
226
226
|
|
|
227
|
-
def self.call(template)
|
|
227
|
+
def self.call(template, source = nil)
|
|
228
|
+
source ||= template.source
|
|
228
229
|
# this juggling is required to keep line numbers right in the error
|
|
229
|
-
%{__already_defined = defined?(json); json||=JbuilderTemplate.new(self); #{
|
|
230
|
+
%{__already_defined = defined?(json); json||=JbuilderTemplate.new(self); #{source}
|
|
230
231
|
json.target! unless (__already_defined && __already_defined != "method")}
|
|
231
232
|
end
|
|
232
233
|
end
|
|
@@ -21,18 +21,38 @@ class JbuilderGeneratorTest < Rails::Generators::TestCase
|
|
|
21
21
|
run_generator
|
|
22
22
|
|
|
23
23
|
assert_file 'app/views/posts/index.json.jbuilder' do |content|
|
|
24
|
-
assert_match %r{json
|
|
24
|
+
assert_match %r{json\.array! @posts, partial: "posts/post", as: :post}, content
|
|
25
25
|
end
|
|
26
26
|
|
|
27
27
|
assert_file 'app/views/posts/show.json.jbuilder' do |content|
|
|
28
|
-
assert_match %r{json
|
|
28
|
+
assert_match %r{json\.partial! "posts/post", post: @post}, content
|
|
29
29
|
end
|
|
30
|
-
|
|
31
|
-
assert_file 'app/views/posts/_post.json.jbuilder' do |content|
|
|
30
|
+
|
|
31
|
+
assert_file 'app/views/posts/_post.json.jbuilder' do |content|
|
|
32
32
|
assert_match %r{json\.extract! post, :id, :title, :body}, content
|
|
33
|
+
assert_match %r{:created_at, :updated_at}, content
|
|
33
34
|
assert_match %r{json\.url post_url\(post, format: :json\)}, content
|
|
34
35
|
end
|
|
35
|
-
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
test 'timestamps are not generated in partial with --no-timestamps' do
|
|
39
|
+
run_generator %w(Post title body:text --no-timestamps)
|
|
40
|
+
|
|
41
|
+
assert_file 'app/views/posts/_post.json.jbuilder' do |content|
|
|
42
|
+
assert_match %r{json\.extract! post, :id, :title, :body$}, content
|
|
43
|
+
assert_no_match %r{:created_at, :updated_at}, content
|
|
44
|
+
end
|
|
45
|
+
end
|
|
36
46
|
|
|
47
|
+
if Rails::VERSION::MAJOR >= 6
|
|
48
|
+
test 'handles virtual attributes' do
|
|
49
|
+
run_generator %w(Message content:rich_text video:attachment photos:attachments)
|
|
50
|
+
|
|
51
|
+
assert_file 'app/views/messages/_message.json.jbuilder' do |content|
|
|
52
|
+
assert_match %r{json\.content message\.content\.to_s}, content
|
|
53
|
+
assert_match %r{json\.video url_for\(message\.video\)}, content
|
|
54
|
+
assert_match %r{json\.photos do\n json\.array!\(message\.photos\) do \|photo\|\n json\.id photo\.id\n json\.url url_for\(photo\)\n end\nend}, content
|
|
55
|
+
end
|
|
56
|
+
end
|
|
37
57
|
end
|
|
38
58
|
end
|
|
@@ -1,457 +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
|
-
|
|
22
|
-
RACER_PARTIAL = <<-JBUILDER
|
|
23
|
-
json.extract! racer, :id, :name
|
|
24
|
-
JBUILDER
|
|
25
|
-
|
|
26
|
-
class Racer
|
|
27
|
-
extend ActiveModel::Naming
|
|
28
|
-
include ActiveModel::Conversion
|
|
29
|
-
|
|
30
|
-
def initialize(id, name)
|
|
31
|
-
@id, @name = id, name
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
attr_reader :id, :name
|
|
35
|
-
end
|
|
36
|
-
|
|
37
3
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
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
|
-
}
|
|
52
|
-
|
|
53
|
-
module Rails
|
|
54
|
-
def self.cache
|
|
55
|
-
@cache ||= ActiveSupport::Cache::MemoryStore.new
|
|
56
|
-
end
|
|
57
|
-
end
|
|
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
|
|
58
13
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
Rails.cache.clear
|
|
63
|
-
end
|
|
14
|
+
COLLECTION_PARTIAL = <<-JBUILDER
|
|
15
|
+
json.extract! collection, :id, :name
|
|
16
|
+
JBUILDER
|
|
64
17
|
|
|
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)
|
|
74
|
-
end
|
|
18
|
+
RACER_PARTIAL = <<-JBUILDER
|
|
19
|
+
json.extract! racer, :id, :name
|
|
20
|
+
JBUILDER
|
|
75
21
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
end
|
|
82
|
-
end
|
|
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,
|
|
83
27
|
|
|
84
|
-
|
|
85
|
-
|
|
28
|
+
# Ensure we find only Jbuilder partials from within Jbuilder templates.
|
|
29
|
+
"_post.html.erb" => "Hello world!"
|
|
30
|
+
}
|
|
86
31
|
|
|
87
|
-
|
|
88
|
-
|
|
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"]
|
|
92
|
-
end
|
|
32
|
+
AUTHORS = [ "David Heinemeier Hansson", "Pavel Pravosud" ].cycle
|
|
33
|
+
POSTS = (1..10).collect { |i| Post.new(i, "Post ##{i}", AUTHORS.next) }
|
|
93
34
|
|
|
94
|
-
|
|
95
|
-
result = jbuild(<<-JBUILDER)
|
|
96
|
-
json.content "hello"
|
|
97
|
-
JBUILDER
|
|
35
|
+
setup { Rails.cache.clear }
|
|
98
36
|
|
|
37
|
+
test "basic template" do
|
|
38
|
+
result = render('json.content "hello"')
|
|
99
39
|
assert_equal "hello", result["content"]
|
|
100
40
|
end
|
|
101
41
|
|
|
102
|
-
test "
|
|
103
|
-
result =
|
|
104
|
-
|
|
105
|
-
json.camel_style "for JS"
|
|
106
|
-
JBUILDER
|
|
107
|
-
|
|
108
|
-
assert_equal ["camelStyle"], result.keys
|
|
109
|
-
end
|
|
110
|
-
|
|
111
|
-
test "key_format! propagates to child elements" do
|
|
112
|
-
result = jbuild(<<-JBUILDER)
|
|
113
|
-
json.key_format! :upcase
|
|
114
|
-
json.level1 "one"
|
|
115
|
-
json.level2 do
|
|
116
|
-
json.value "two"
|
|
117
|
-
end
|
|
118
|
-
JBUILDER
|
|
119
|
-
|
|
120
|
-
assert_equal "one", result["LEVEL1"]
|
|
121
|
-
assert_equal "two", result["LEVEL2"]["VALUE"]
|
|
42
|
+
test "partial by name with top-level locals" do
|
|
43
|
+
result = render('json.partial! "partial", content: "hello"')
|
|
44
|
+
assert_equal "hello", result["content"]
|
|
122
45
|
end
|
|
123
46
|
|
|
124
|
-
test "partial
|
|
125
|
-
result =
|
|
126
|
-
json.partial! "partial"
|
|
127
|
-
JBUILDER
|
|
128
|
-
|
|
47
|
+
test "partial by name with nested locals" do
|
|
48
|
+
result = render('json.partial! "partial", locals: { content: "hello" }')
|
|
129
49
|
assert_equal "hello", result["content"]
|
|
130
50
|
end
|
|
131
51
|
|
|
132
|
-
test "partial
|
|
133
|
-
result =
|
|
134
|
-
|
|
135
|
-
JBUILDER
|
|
136
|
-
|
|
137
|
-
assert_equal "howdy", result["content"]
|
|
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"]
|
|
138
55
|
end
|
|
139
56
|
|
|
140
|
-
test "partial
|
|
141
|
-
result =
|
|
142
|
-
|
|
143
|
-
JBUILDER
|
|
144
|
-
|
|
145
|
-
assert_equal "goodbye", result["content"]
|
|
57
|
+
test "partial by options containing top-level locals" do
|
|
58
|
+
result = render('json.partial! partial: "partial", content: "hello"')
|
|
59
|
+
assert_equal "hello", result["content"]
|
|
146
60
|
end
|
|
147
61
|
|
|
148
|
-
test "partial
|
|
149
|
-
result =
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
assert_collection_rendered result
|
|
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"]
|
|
154
66
|
end
|
|
155
67
|
|
|
156
|
-
test "partial
|
|
157
|
-
result =
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
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"]
|
|
162
74
|
end
|
|
163
75
|
|
|
164
|
-
test "partial
|
|
165
|
-
result =
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
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"]
|
|
170
82
|
end
|
|
171
83
|
|
|
172
|
-
test "partial
|
|
173
|
-
result =
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
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"]
|
|
178
90
|
end
|
|
179
91
|
|
|
180
|
-
test "partial
|
|
181
|
-
|
|
182
|
-
json.partial! partial: "blog_post", collection: BLOG_POST_COLLECTION, as: :blog_post
|
|
183
|
-
JBUILDER
|
|
184
|
-
|
|
185
|
-
assert_collection_rendered result
|
|
92
|
+
test "nil partial collection by name" do
|
|
93
|
+
assert_equal [], render('json.partial! "post", collection: @posts, as: :post', posts: nil)
|
|
186
94
|
end
|
|
187
95
|
|
|
188
|
-
test "partial
|
|
189
|
-
|
|
190
|
-
json.partial! partial: "blog_post", collection: nil, as: :blog_post
|
|
191
|
-
JBUILDER
|
|
192
|
-
|
|
193
|
-
assert_equal [], result
|
|
96
|
+
test "nil partial collection by options" do
|
|
97
|
+
assert_equal [], render('json.partial! partial: "post", collection: @posts, as: :post', posts: nil)
|
|
194
98
|
end
|
|
195
99
|
|
|
196
|
-
test "
|
|
197
|
-
result =
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
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"]
|
|
202
106
|
end
|
|
203
107
|
|
|
204
|
-
test "
|
|
205
|
-
|
|
206
|
-
json.array! nil, partial: "blog_post", as: :blog_post
|
|
207
|
-
JBUILDER
|
|
208
|
-
|
|
209
|
-
assert_equal [], result
|
|
108
|
+
test "empty array of partials from nil collection" do
|
|
109
|
+
assert_equal [], render('json.array! @posts, partial: "post", as: :post', posts: nil)
|
|
210
110
|
end
|
|
211
111
|
|
|
212
|
-
test "
|
|
213
|
-
result =
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
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"]
|
|
218
118
|
end
|
|
219
119
|
|
|
220
|
-
test "
|
|
221
|
-
result =
|
|
222
|
-
json.posts nil, partial: "blog_post", as: :blog_post
|
|
223
|
-
JBUILDER
|
|
224
|
-
|
|
120
|
+
test "empty array of partials under key from nil collection" do
|
|
121
|
+
result = render('json.posts @posts, partial: "post", as: :post', posts: nil)
|
|
225
122
|
assert_equal [], result["posts"]
|
|
226
123
|
end
|
|
227
124
|
|
|
228
|
-
test "
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
json.cache! "nothing" do
|
|
125
|
+
test "object fragment caching" do
|
|
126
|
+
render(<<-JBUILDER)
|
|
127
|
+
json.cache! "cache-key" do
|
|
128
|
+
json.name "Hit"
|
|
233
129
|
end
|
|
234
130
|
JBUILDER
|
|
235
131
|
|
|
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"]
|
|
132
|
+
hit = render('json.cache! "cache-key" do; end')
|
|
133
|
+
assert_equal "Hit", hit["name"]
|
|
247
134
|
end
|
|
248
135
|
|
|
249
|
-
test "fragment caching
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
json.cache! "cachekey" do
|
|
254
|
-
json.name "Cache"
|
|
136
|
+
test "conditional object fragment caching" do
|
|
137
|
+
render(<<-JBUILDER)
|
|
138
|
+
json.cache_if! true, "cache-key" do
|
|
139
|
+
json.a "Hit"
|
|
255
140
|
end
|
|
256
|
-
JBUILDER
|
|
257
141
|
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
json.name "Miss"
|
|
142
|
+
json.cache_if! false, "cache-key" do
|
|
143
|
+
json.b "Hit"
|
|
261
144
|
end
|
|
262
145
|
JBUILDER
|
|
263
146
|
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
test "conditionally fragment caching a JSON object" do
|
|
268
|
-
undef_context_methods :fragment_name_with_digest, :cache_fragment_name
|
|
269
|
-
|
|
270
|
-
jbuild <<-JBUILDER
|
|
271
|
-
json.cache_if! true, "cachekey" do
|
|
272
|
-
json.test1 "Cache"
|
|
147
|
+
result = render(<<-JBUILDER)
|
|
148
|
+
json.cache_if! true, "cache-key" do
|
|
149
|
+
json.a "Miss"
|
|
273
150
|
end
|
|
274
|
-
json.cache_if! false, "cachekey" do
|
|
275
|
-
json.test2 "Cache"
|
|
276
|
-
end
|
|
277
|
-
JBUILDER
|
|
278
151
|
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
json.test1 "Miss"
|
|
282
|
-
end
|
|
283
|
-
json.cache_if! false, "cachekey" do
|
|
284
|
-
json.test2 "Miss"
|
|
152
|
+
json.cache_if! false, "cache-key" do
|
|
153
|
+
json.b "Miss"
|
|
285
154
|
end
|
|
286
155
|
JBUILDER
|
|
287
156
|
|
|
288
|
-
assert_equal "
|
|
289
|
-
assert_equal "Miss", result["
|
|
157
|
+
assert_equal "Hit", result["a"]
|
|
158
|
+
assert_equal "Miss", result["b"]
|
|
290
159
|
end
|
|
291
160
|
|
|
292
|
-
test "fragment caching
|
|
293
|
-
|
|
161
|
+
test "object fragment caching with expiry" do
|
|
162
|
+
travel_to "2018-05-12 11:29:00 -0400"
|
|
294
163
|
|
|
295
|
-
|
|
296
|
-
json.cache! "
|
|
297
|
-
json.
|
|
164
|
+
render <<-JBUILDER
|
|
165
|
+
json.cache! "cache-key", expires_in: 1.minute do
|
|
166
|
+
json.name "Hit"
|
|
298
167
|
end
|
|
299
168
|
JBUILDER
|
|
300
169
|
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
170
|
+
travel 30.seconds
|
|
171
|
+
|
|
172
|
+
result = render(<<-JBUILDER)
|
|
173
|
+
json.cache! "cache-key", expires_in: 1.minute do
|
|
174
|
+
json.name "Miss"
|
|
304
175
|
end
|
|
305
176
|
JBUILDER
|
|
306
177
|
|
|
307
|
-
assert_equal
|
|
308
|
-
end
|
|
309
|
-
|
|
310
|
-
test "fragment caching works with current cache digests" do
|
|
311
|
-
undef_context_methods :fragment_name_with_digest
|
|
178
|
+
assert_equal "Hit", result["name"]
|
|
312
179
|
|
|
313
|
-
|
|
314
|
-
ActiveSupport::Cache.expects :expand_cache_key
|
|
180
|
+
travel 31.seconds
|
|
315
181
|
|
|
316
|
-
|
|
317
|
-
json.cache! "
|
|
318
|
-
json.name "
|
|
182
|
+
result = render(<<-JBUILDER)
|
|
183
|
+
json.cache! "cache-key", expires_in: 1.minute do
|
|
184
|
+
json.name "Miss"
|
|
319
185
|
end
|
|
320
186
|
JBUILDER
|
|
321
|
-
end
|
|
322
187
|
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
@context.expects(:combined_fragment_cache_key).with("cachekey")
|
|
188
|
+
assert_equal "Miss", result["name"]
|
|
189
|
+
end
|
|
327
190
|
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
191
|
+
test "object root caching" do
|
|
192
|
+
render <<-JBUILDER
|
|
193
|
+
json.cache_root! "cache-key" do
|
|
194
|
+
json.name "Hit"
|
|
331
195
|
end
|
|
332
196
|
JBUILDER
|
|
333
|
-
end
|
|
334
|
-
|
|
335
|
-
test "fragment caching instrumentation" do
|
|
336
|
-
undef_context_methods :fragment_name_with_digest, :cache_fragment_name
|
|
337
197
|
|
|
338
|
-
|
|
339
|
-
ActiveSupport::Notifications.subscribe("read_fragment.action_controller") { |*args| payloads[:read_fragment] = args.last }
|
|
340
|
-
ActiveSupport::Notifications.subscribe("write_fragment.action_controller") { |*args| payloads[:write_fragment] = args.last }
|
|
198
|
+
assert_equal JSON.dump(name: "Hit"), Rails.cache.read("jbuilder/root/cache-key")
|
|
341
199
|
|
|
342
|
-
|
|
343
|
-
json.
|
|
344
|
-
json.name "
|
|
200
|
+
result = render(<<-JBUILDER)
|
|
201
|
+
json.cache_root! "cache-key" do
|
|
202
|
+
json.name "Miss"
|
|
345
203
|
end
|
|
346
204
|
JBUILDER
|
|
347
205
|
|
|
348
|
-
assert_equal "
|
|
349
|
-
assert_equal "jbuilder/cachekey", payloads[:write_fragment][:key]
|
|
206
|
+
assert_equal "Hit", result["name"]
|
|
350
207
|
end
|
|
351
208
|
|
|
352
|
-
test "
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
ActiveSupport::Cache.expects :expand_cache_key
|
|
357
|
-
|
|
358
|
-
jbuild <<-JBUILDER
|
|
359
|
-
json.cache! "cachekey", skip_digest: true do
|
|
360
|
-
json.name "Cache"
|
|
209
|
+
test "array fragment caching" do
|
|
210
|
+
render <<-JBUILDER
|
|
211
|
+
json.cache! "cache-key" do
|
|
212
|
+
json.array! %w[ a b c ]
|
|
361
213
|
end
|
|
362
214
|
JBUILDER
|
|
363
|
-
end
|
|
364
215
|
|
|
365
|
-
|
|
366
|
-
undef_context_methods :fragment_name_with_digest
|
|
367
|
-
|
|
368
|
-
@context.expects(:cache_fragment_name).with("cachekey", {})
|
|
369
|
-
|
|
370
|
-
jbuild <<-JBUILDER
|
|
371
|
-
json.cache! "cachekey", expires_in: 1.minute do
|
|
372
|
-
json.name "Cache"
|
|
373
|
-
end
|
|
374
|
-
JBUILDER
|
|
216
|
+
assert_equal %w[ a b c ], render('json.cache! "cache-key" do; end')
|
|
375
217
|
end
|
|
376
218
|
|
|
377
|
-
test "
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
json.cache_root! "cachekey" do
|
|
382
|
-
json.name "Miss"
|
|
219
|
+
test "array root caching" do
|
|
220
|
+
render <<-JBUILDER
|
|
221
|
+
json.cache_root! "cache-key" do
|
|
222
|
+
json.array! %w[ a b c ]
|
|
383
223
|
end
|
|
384
224
|
JBUILDER
|
|
385
225
|
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
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 ]
|
|
389
231
|
end
|
|
390
232
|
JBUILDER
|
|
391
|
-
|
|
392
|
-
assert_equal cache_miss_result, cache_hit_result
|
|
393
233
|
end
|
|
394
234
|
|
|
395
|
-
test "failing to cache root after
|
|
235
|
+
test "failing to cache root after JSON structures have been defined" do
|
|
396
236
|
assert_raises ActionView::Template::Error, "cache_root! can't be used after JSON structures have been defined" do
|
|
397
|
-
|
|
237
|
+
render <<-JBUILDER
|
|
398
238
|
json.name "Kaboom"
|
|
399
|
-
json.cache_root! "
|
|
239
|
+
json.cache_root! "cache-key" do
|
|
400
240
|
json.name "Miss"
|
|
401
241
|
end
|
|
402
242
|
JBUILDER
|
|
403
243
|
end
|
|
404
244
|
end
|
|
405
245
|
|
|
406
|
-
test "
|
|
407
|
-
|
|
246
|
+
test "empty fragment caching" do
|
|
247
|
+
render 'json.cache! "nothing" do; end'
|
|
408
248
|
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
249
|
+
result = nil
|
|
250
|
+
|
|
251
|
+
assert_nothing_raised do
|
|
252
|
+
result = render(<<-JBUILDER)
|
|
253
|
+
json.foo "bar"
|
|
254
|
+
json.cache! "nothing" do; end
|
|
255
|
+
JBUILDER
|
|
256
|
+
end
|
|
414
257
|
|
|
415
|
-
assert_equal
|
|
258
|
+
assert_equal "bar", result["foo"]
|
|
416
259
|
end
|
|
417
260
|
|
|
418
|
-
test "
|
|
419
|
-
|
|
261
|
+
test "cache instrumentation" do
|
|
262
|
+
payloads = {}
|
|
420
263
|
|
|
421
|
-
|
|
422
|
-
|
|
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 }
|
|
266
|
+
|
|
267
|
+
render <<-JBUILDER
|
|
268
|
+
json.cache! "cache-key" do
|
|
269
|
+
json.name "Cache"
|
|
270
|
+
end
|
|
423
271
|
JBUILDER
|
|
424
272
|
|
|
425
|
-
assert_equal
|
|
426
|
-
assert_equal "
|
|
427
|
-
assert_equal "David", result["post"]["author"]["first_name"]
|
|
273
|
+
assert_equal "jbuilder/cache-key", payloads[:read][:key]
|
|
274
|
+
assert_equal "jbuilder/cache-key", payloads[:write][:key]
|
|
428
275
|
end
|
|
429
276
|
|
|
430
|
-
test "
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
json.partial! @racer
|
|
277
|
+
test "camelized keys" do
|
|
278
|
+
result = render(<<-JBUILDER)
|
|
279
|
+
json.key_format! camelize: [:lower]
|
|
280
|
+
json.first_name "David"
|
|
435
281
|
JBUILDER
|
|
436
282
|
|
|
437
|
-
assert_equal
|
|
438
|
-
assert_equal 123, result["id"]
|
|
439
|
-
assert_equal "Chris Harris", result["name"]
|
|
283
|
+
assert_equal "David", result["firstName"]
|
|
440
284
|
end
|
|
441
285
|
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
}
|
|
286
|
+
private
|
|
287
|
+
def render(*args)
|
|
288
|
+
JSON.load render_without_parsing(*args)
|
|
289
|
+
end
|
|
447
290
|
|
|
448
|
-
|
|
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
|
|
449
295
|
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
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
|
|
453
300
|
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
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
|
|
307
|
+
|
|
308
|
+
def view.view_cache_dependencies; []; end
|
|
309
|
+
|
|
310
|
+
view
|
|
311
|
+
end
|
|
457
312
|
end
|