jbuilder 2.7.0 → 2.10.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 +5 -5
- data/.gitignore +2 -0
- data/.travis.yml +34 -48
- data/Appraisals +16 -12
- data/CHANGELOG.md +44 -0
- data/CONTRIBUTING.md +0 -2
- data/Gemfile +0 -1
- data/MIT-LICENSE +1 -1
- data/README.md +32 -16
- data/gemfiles/rails_5_0.gemfile +1 -4
- data/gemfiles/rails_5_1.gemfile +1 -4
- data/gemfiles/{rails_4_2.gemfile → rails_5_2.gemfile} +1 -4
- 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 +8 -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 +9 -9
- data/lib/generators/rails/templates/index.json.jbuilder +1 -1
- data/lib/generators/rails/templates/partial.json.jbuilder +1 -1
- data/lib/jbuilder/jbuilder_template.rb +10 -9
- data/lib/jbuilder.rb +13 -12
- data/test/jbuilder_dependency_tracker_test.rb +1 -1
- data/test/jbuilder_generator_test.rb +13 -5
- data/test/jbuilder_template_test.rb +193 -338
- data/test/jbuilder_test.rb +31 -3
- data/test/scaffold_api_controller_generator_test.rb +6 -2
- data/test/scaffold_controller_generator_test.rb +11 -7
- data/test/test_helper.rb +26 -6
- metadata +9 -22
data/lib/jbuilder.rb
CHANGED
@@ -2,8 +2,9 @@ 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
|
@@ -26,12 +27,12 @@ class Jbuilder
|
|
26
27
|
BLANK = Blank.new
|
27
28
|
NON_ENUMERABLES = [ ::Struct, ::OpenStruct ].to_set
|
28
29
|
|
29
|
-
def set!(key, value = BLANK, *args)
|
30
|
+
def set!(key, value = BLANK, *args, &block)
|
30
31
|
result = if ::Kernel.block_given?
|
31
32
|
if !_blank?(value)
|
32
33
|
# json.comments @post.comments { |comment| ... }
|
33
34
|
# { "comments": [ { ... }, { ... } ] }
|
34
|
-
_scope{ array! value,
|
35
|
+
_scope{ array! value, &block }
|
35
36
|
else
|
36
37
|
# json.comments { ... }
|
37
38
|
# { "comments": ... }
|
@@ -61,9 +62,9 @@ class Jbuilder
|
|
61
62
|
_set_value key, result
|
62
63
|
end
|
63
64
|
|
64
|
-
def method_missing(*args)
|
65
|
+
def method_missing(*args, &block)
|
65
66
|
if ::Kernel.block_given?
|
66
|
-
set!(*args,
|
67
|
+
set!(*args, &block)
|
67
68
|
else
|
68
69
|
set!(*args)
|
69
70
|
end
|
@@ -163,7 +164,7 @@ class Jbuilder
|
|
163
164
|
#
|
164
165
|
# [ { "name": David", "age": 32 }, { "name": Jamie", "age": 31 } ]
|
165
166
|
#
|
166
|
-
#
|
167
|
+
# You can use the call syntax instead of an explicit extract! call:
|
167
168
|
#
|
168
169
|
# json.(@people) { |person| ... }
|
169
170
|
#
|
@@ -181,11 +182,11 @@ class Jbuilder
|
|
181
182
|
# json.array! [1, 2, 3]
|
182
183
|
#
|
183
184
|
# [1,2,3]
|
184
|
-
def array!(collection = [], *attributes)
|
185
|
+
def array!(collection = [], *attributes, &block)
|
185
186
|
array = if collection.nil?
|
186
187
|
[]
|
187
188
|
elsif ::Kernel.block_given?
|
188
|
-
_map_collection(collection,
|
189
|
+
_map_collection(collection, &block)
|
189
190
|
elsif attributes.any?
|
190
191
|
_map_collection(collection) { |element| extract! element, *attributes }
|
191
192
|
else
|
@@ -220,9 +221,9 @@ class Jbuilder
|
|
220
221
|
end
|
221
222
|
end
|
222
223
|
|
223
|
-
def call(object, *attributes)
|
224
|
+
def call(object, *attributes, &block)
|
224
225
|
if ::Kernel.block_given?
|
225
|
-
array! object,
|
226
|
+
array! object, &block
|
226
227
|
else
|
227
228
|
extract! object, *attributes
|
228
229
|
end
|
@@ -247,7 +248,7 @@ class Jbuilder
|
|
247
248
|
|
248
249
|
# Encodes the current builder as JSON.
|
249
250
|
def target!
|
250
|
-
|
251
|
+
@attributes.to_json
|
251
252
|
end
|
252
253
|
|
253
254
|
private
|
@@ -275,7 +276,7 @@ class Jbuilder
|
|
275
276
|
elsif ::Array === current_value && ::Array === updates
|
276
277
|
current_value + updates
|
277
278
|
elsif ::Hash === current_value && ::Hash === updates
|
278
|
-
current_value.
|
279
|
+
current_value.deep_merge(updates)
|
279
280
|
else
|
280
281
|
raise MergeError.build(current_value, updates)
|
281
282
|
end
|
@@ -53,7 +53,7 @@ class JbuilderDependencyTrackerTest < ActiveSupport::TestCase
|
|
53
53
|
assert_equal %w[path/to/partial], dependencies
|
54
54
|
end
|
55
55
|
|
56
|
-
test 'detects partial in indirect
|
56
|
+
test 'detects partial in indirect collection calls' do
|
57
57
|
dependencies = track_dependencies <<-RUBY
|
58
58
|
json.comments @post.comments, partial: 'comments/comment', as: :comment
|
59
59
|
RUBY
|
@@ -21,18 +21,26 @@ 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
|
36
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
|
37
45
|
end
|
38
46
|
end
|