jbuilder 2.6.4 → 2.11.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +2 -0
- data/.travis.yml +34 -48
- data/Appraisals +13 -42
- data/CHANGELOG.md +57 -0
- data/CONTRIBUTING.md +1 -3
- data/Gemfile +0 -1
- data/MIT-LICENSE +1 -1
- data/README.md +32 -16
- data/Rakefile +1 -5
- data/gemfiles/rails_5_0.gemfile +3 -6
- data/gemfiles/rails_5_1.gemfile +3 -6
- 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 +11 -6
- 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/jbuilder_template.rb +11 -10
- data/lib/jbuilder.rb +30 -18
- data/test/jbuilder_dependency_tracker_test.rb +1 -1
- data/test/jbuilder_generator_test.rb +25 -5
- data/test/jbuilder_template_test.rb +196 -325
- data/test/jbuilder_test.rb +95 -3
- data/test/scaffold_api_controller_generator_test.rb +17 -2
- data/test/scaffold_controller_generator_test.rb +21 -7
- data/test/test_helper.rb +26 -6
- metadata +10 -29
- 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_4_2.gemfile +0 -14
data/test/jbuilder_test.rb
CHANGED
@@ -49,7 +49,7 @@ end
|
|
49
49
|
|
50
50
|
|
51
51
|
class JbuilderTest < ActiveSupport::TestCase
|
52
|
-
|
52
|
+
teardown do
|
53
53
|
Jbuilder.send :class_variable_set, '@@key_formatter', nil
|
54
54
|
end
|
55
55
|
|
@@ -75,7 +75,7 @@ class JbuilderTest < ActiveSupport::TestCase
|
|
75
75
|
end
|
76
76
|
|
77
77
|
assert result.has_key?('content')
|
78
|
-
|
78
|
+
assert_nil result['content']
|
79
79
|
end
|
80
80
|
|
81
81
|
test 'multiple keys' do
|
@@ -99,7 +99,7 @@ class JbuilderTest < ActiveSupport::TestCase
|
|
99
99
|
assert_equal 32, result['age']
|
100
100
|
end
|
101
101
|
|
102
|
-
test 'extracting from object using call style
|
102
|
+
test 'extracting from object using call style' do
|
103
103
|
person = Struct.new(:name, :age).new('David', 32)
|
104
104
|
|
105
105
|
result = jbuild do |json|
|
@@ -159,6 +159,25 @@ class JbuilderTest < ActiveSupport::TestCase
|
|
159
159
|
assert_equal 32, result['author']['age']
|
160
160
|
end
|
161
161
|
|
162
|
+
test 'nested blocks are additive' do
|
163
|
+
result = jbuild do |json|
|
164
|
+
json.author do
|
165
|
+
json.name do
|
166
|
+
json.first 'David'
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
json.author do
|
171
|
+
json.name do
|
172
|
+
json.last 'Heinemeier Hansson'
|
173
|
+
end
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
assert_equal 'David', result['author']['name']['first']
|
178
|
+
assert_equal 'Heinemeier Hansson', result['author']['name']['last']
|
179
|
+
end
|
180
|
+
|
162
181
|
test 'support merge! method' do
|
163
182
|
result = jbuild do |json|
|
164
183
|
json.merge! 'foo' => 'bar'
|
@@ -177,6 +196,18 @@ class JbuilderTest < ActiveSupport::TestCase
|
|
177
196
|
assert_equal 'Pavel', result['author']['name']
|
178
197
|
end
|
179
198
|
|
199
|
+
test 'support merge! method with Jbuilder instance' do
|
200
|
+
obj = jbuild do |json|
|
201
|
+
json.foo 'bar'
|
202
|
+
end
|
203
|
+
|
204
|
+
result = jbuild do |json|
|
205
|
+
json.merge! obj
|
206
|
+
end
|
207
|
+
|
208
|
+
assert_equal 'bar', result['foo']
|
209
|
+
end
|
210
|
+
|
180
211
|
test 'blocks are additive via extract syntax' do
|
181
212
|
person = Person.new('Pavel', 27)
|
182
213
|
|
@@ -562,6 +593,58 @@ class JbuilderTest < ActiveSupport::TestCase
|
|
562
593
|
assert_equal ['oats and friends'], result.keys
|
563
594
|
end
|
564
595
|
|
596
|
+
test 'key_format! with merge!' do
|
597
|
+
hash = { camel_style: 'for JS' }
|
598
|
+
result = jbuild do |json|
|
599
|
+
json.key_format! camelize: :lower
|
600
|
+
json.merge! hash
|
601
|
+
end
|
602
|
+
|
603
|
+
assert_equal ['camelStyle'], result.keys
|
604
|
+
end
|
605
|
+
|
606
|
+
test 'key_format! with merge! deep' do
|
607
|
+
hash = { camel_style: { sub_attr: 'for JS' } }
|
608
|
+
result = jbuild do |json|
|
609
|
+
json.key_format! camelize: :lower
|
610
|
+
json.merge! hash
|
611
|
+
end
|
612
|
+
|
613
|
+
assert_equal ['subAttr'], result['camelStyle'].keys
|
614
|
+
end
|
615
|
+
|
616
|
+
test 'key_format! with set! array of hashes' do
|
617
|
+
names = [{ first_name: 'camel', last_name: 'case' }]
|
618
|
+
result = jbuild do |json|
|
619
|
+
json.key_format! camelize: :lower
|
620
|
+
json.set! :names, names
|
621
|
+
end
|
622
|
+
|
623
|
+
assert_equal %w[firstName lastName], result['names'][0].keys
|
624
|
+
end
|
625
|
+
|
626
|
+
test 'key_format! with array! of hashes' do
|
627
|
+
names = [{ first_name: 'camel', last_name: 'case' }]
|
628
|
+
result = jbuild do |json|
|
629
|
+
json.key_format! camelize: :lower
|
630
|
+
json.array! names
|
631
|
+
end
|
632
|
+
|
633
|
+
assert_equal %w[firstName lastName], result[0].keys
|
634
|
+
end
|
635
|
+
|
636
|
+
test 'key_format! with merge! array of hashes' do
|
637
|
+
names = [{ first_name: 'camel', last_name: 'case' }]
|
638
|
+
new_names = [{ first_name: 'snake', last_name: 'case' }]
|
639
|
+
result = jbuild do |json|
|
640
|
+
json.key_format! camelize: :lower
|
641
|
+
json.array! names
|
642
|
+
json.merge! new_names
|
643
|
+
end
|
644
|
+
|
645
|
+
assert_equal %w[firstName lastName], result[1].keys
|
646
|
+
end
|
647
|
+
|
565
648
|
test 'default key_format!' do
|
566
649
|
Jbuilder.key_format camelize: :lower
|
567
650
|
result = jbuild{ |json| json.camel_style 'for JS' }
|
@@ -713,4 +796,13 @@ class JbuilderTest < ActiveSupport::TestCase
|
|
713
796
|
end
|
714
797
|
end
|
715
798
|
end
|
799
|
+
|
800
|
+
if RUBY_VERSION >= "2.2.10"
|
801
|
+
test "respects JSON encoding customizations" do
|
802
|
+
# Active Support overrides Time#as_json for custom formatting.
|
803
|
+
# Ensure we call #to_json on the final attributes instead of JSON.dump.
|
804
|
+
result = JSON.load(Jbuilder.encode { |json| json.time Time.parse("2018-05-13 11:51:00.485 -0400") })
|
805
|
+
assert_equal "2018-05-13T11:51:00.485-04:00", result["time"]
|
806
|
+
end
|
807
|
+
end
|
716
808
|
end
|
@@ -6,7 +6,7 @@ if Rails::VERSION::MAJOR > 4
|
|
6
6
|
|
7
7
|
class ScaffoldApiControllerGeneratorTest < Rails::Generators::TestCase
|
8
8
|
tests Rails::Generators::ScaffoldControllerGenerator
|
9
|
-
arguments %w(Post title body:text --api)
|
9
|
+
arguments %w(Post title body:text images:attachments --api)
|
10
10
|
destination File.expand_path('../tmp', __FILE__)
|
11
11
|
setup :prepare_destination
|
12
12
|
|
@@ -39,7 +39,11 @@ if Rails::VERSION::MAJOR > 4
|
|
39
39
|
end
|
40
40
|
|
41
41
|
assert_match %r{def post_params}, content
|
42
|
-
|
42
|
+
if Rails::VERSION::MAJOR >= 6
|
43
|
+
assert_match %r{params\.require\(:post\)\.permit\(:title, :body, images: \[\]\)}, content
|
44
|
+
else
|
45
|
+
assert_match %r{params\.require\(:post\)\.permit\(:title, :body, :images\)}, content
|
46
|
+
end
|
43
47
|
end
|
44
48
|
end
|
45
49
|
|
@@ -51,5 +55,16 @@ if Rails::VERSION::MAJOR > 4
|
|
51
55
|
assert_match %r{params\.fetch\(:post, \{\}\)}, content
|
52
56
|
end
|
53
57
|
end
|
58
|
+
|
59
|
+
|
60
|
+
if Rails::VERSION::MAJOR >= 6
|
61
|
+
test 'handles virtual attributes' do
|
62
|
+
run_generator ["Message", "content:rich_text", "video:attachment", "photos:attachments"]
|
63
|
+
|
64
|
+
assert_file 'app/controllers/messages_controller.rb' do |content|
|
65
|
+
assert_match %r{params\.require\(:message\)\.permit\(:content, :video, photos: \[\]\)}, content
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
54
69
|
end
|
55
70
|
end
|
@@ -4,7 +4,7 @@ require 'generators/rails/scaffold_controller_generator'
|
|
4
4
|
|
5
5
|
class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase
|
6
6
|
tests Rails::Generators::ScaffoldControllerGenerator
|
7
|
-
arguments %w(Post title body:text)
|
7
|
+
arguments %w(Post title body:text images:attachments)
|
8
8
|
destination File.expand_path('../tmp', __FILE__)
|
9
9
|
setup :prepare_destination
|
10
10
|
|
@@ -31,27 +31,31 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase
|
|
31
31
|
assert_instance_method :create, content do |m|
|
32
32
|
assert_match %r{@post = Post\.new\(post_params\)}, m
|
33
33
|
assert_match %r{@post\.save}, m
|
34
|
-
assert_match %r{format\.html \{ redirect_to @post, notice:
|
34
|
+
assert_match %r{format\.html \{ redirect_to @post, notice: "Post was successfully created\." \}}, m
|
35
35
|
assert_match %r{format\.json \{ render :show, status: :created, location: @post \}}, m
|
36
|
-
assert_match %r{format\.html \{ render :new \}}, m
|
36
|
+
assert_match %r{format\.html \{ render :new, status: :unprocessable_entity \}}, m
|
37
37
|
assert_match %r{format\.json \{ render json: @post\.errors, status: :unprocessable_entity \}}, m
|
38
38
|
end
|
39
39
|
|
40
40
|
assert_instance_method :update, content do |m|
|
41
|
-
assert_match %r{format\.html \{ redirect_to @post, notice:
|
41
|
+
assert_match %r{format\.html \{ redirect_to @post, notice: "Post was successfully updated\." \}}, m
|
42
42
|
assert_match %r{format\.json \{ render :show, status: :ok, location: @post \}}, m
|
43
|
-
assert_match %r{format\.html \{ render :edit \}}, m
|
43
|
+
assert_match %r{format\.html \{ render :edit, status: :unprocessable_entity \}}, m
|
44
44
|
assert_match %r{format\.json \{ render json: @post.errors, status: :unprocessable_entity \}}, m
|
45
45
|
end
|
46
46
|
|
47
47
|
assert_instance_method :destroy, content do |m|
|
48
48
|
assert_match %r{@post\.destroy}, m
|
49
|
-
assert_match %r{format\.html \{ redirect_to posts_url, notice:
|
49
|
+
assert_match %r{format\.html \{ redirect_to posts_url, notice: "Post was successfully destroyed\." \}}, m
|
50
50
|
assert_match %r{format\.json \{ head :no_content \}}, m
|
51
51
|
end
|
52
52
|
|
53
53
|
assert_match %r{def post_params}, content
|
54
|
-
|
54
|
+
if Rails::VERSION::MAJOR >= 6
|
55
|
+
assert_match %r{params\.require\(:post\)\.permit\(:title, :body, images: \[\]\)}, content
|
56
|
+
else
|
57
|
+
assert_match %r{params\.require\(:post\)\.permit\(:title, :body, :images\)}, content
|
58
|
+
end
|
55
59
|
end
|
56
60
|
end
|
57
61
|
|
@@ -63,4 +67,14 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase
|
|
63
67
|
assert_match %r{params\.fetch\(:post, \{\}\)}, content
|
64
68
|
end
|
65
69
|
end
|
70
|
+
|
71
|
+
if Rails::VERSION::MAJOR >= 6
|
72
|
+
test 'handles virtual attributes' do
|
73
|
+
run_generator %w(Message content:rich_text video:attachment photos:attachments)
|
74
|
+
|
75
|
+
assert_file 'app/controllers/messages_controller.rb' do |content|
|
76
|
+
assert_match %r{params\.require\(:message\)\.permit\(:content, :video, photos: \[\]\)}, content
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
66
80
|
end
|
data/test/test_helper.rb
CHANGED
@@ -1,14 +1,34 @@
|
|
1
1
|
require "bundler/setup"
|
2
|
+
|
2
3
|
require "active_support"
|
4
|
+
require "active_support/core_ext/array/access"
|
5
|
+
require "active_support/cache/memory_store"
|
6
|
+
require "active_support/json"
|
7
|
+
require "active_model"
|
8
|
+
require "action_view"
|
3
9
|
require "rails/version"
|
4
10
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
11
|
+
require "jbuilder"
|
12
|
+
|
13
|
+
require "active_support/testing/autorun"
|
14
|
+
require "mocha/minitest"
|
15
|
+
|
16
|
+
ActiveSupport.test_order = :random
|
17
|
+
|
18
|
+
class << Rails
|
19
|
+
def cache
|
20
|
+
@cache ||= ActiveSupport::Cache::MemoryStore.new
|
21
|
+
end
|
9
22
|
end
|
10
23
|
|
24
|
+
class Post < Struct.new(:id, :body, :author_name); end
|
11
25
|
|
12
|
-
|
13
|
-
|
26
|
+
class Racer < Struct.new(:id, :name)
|
27
|
+
extend ActiveModel::Naming
|
28
|
+
include ActiveModel::Conversion
|
14
29
|
end
|
30
|
+
|
31
|
+
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.
|
4
|
+
version: 2.11.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Heinemeier Hansson
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-01-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -16,31 +16,16 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 5.0.0
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
27
|
-
- !ruby/object:Gem::Dependency
|
28
|
-
name: multi_json
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - ">="
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: '1.2'
|
34
|
-
type: :runtime
|
35
|
-
prerelease: false
|
36
|
-
version_requirements: !ruby/object:Gem::Requirement
|
37
|
-
requirements:
|
38
|
-
- - ">="
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
version: '1.2'
|
26
|
+
version: 5.0.0
|
41
27
|
description:
|
42
|
-
email:
|
43
|
-
- david@37signals.com
|
28
|
+
email: david@basecamp.com
|
44
29
|
executables: []
|
45
30
|
extensions: []
|
46
31
|
extra_rdoc_files: []
|
@@ -54,14 +39,11 @@ files:
|
|
54
39
|
- MIT-LICENSE
|
55
40
|
- README.md
|
56
41
|
- Rakefile
|
57
|
-
- gemfiles/rails_3_0.gemfile
|
58
|
-
- gemfiles/rails_3_1.gemfile
|
59
|
-
- gemfiles/rails_3_2.gemfile
|
60
|
-
- gemfiles/rails_4_0.gemfile
|
61
|
-
- gemfiles/rails_4_1.gemfile
|
62
|
-
- gemfiles/rails_4_2.gemfile
|
63
42
|
- gemfiles/rails_5_0.gemfile
|
64
43
|
- gemfiles/rails_5_1.gemfile
|
44
|
+
- gemfiles/rails_5_2.gemfile
|
45
|
+
- gemfiles/rails_6_0.gemfile
|
46
|
+
- gemfiles/rails_head.gemfile
|
65
47
|
- jbuilder.gemspec
|
66
48
|
- lib/generators/rails/jbuilder_generator.rb
|
67
49
|
- lib/generators/rails/scaffold_controller_generator.rb
|
@@ -97,15 +79,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
97
79
|
requirements:
|
98
80
|
- - ">="
|
99
81
|
- !ruby/object:Gem::Version
|
100
|
-
version:
|
82
|
+
version: 2.2.2
|
101
83
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
84
|
requirements:
|
103
85
|
- - ">="
|
104
86
|
- !ruby/object:Gem::Version
|
105
87
|
version: '0'
|
106
88
|
requirements: []
|
107
|
-
|
108
|
-
rubygems_version: 2.6.8
|
89
|
+
rubygems_version: 3.1.2
|
109
90
|
signing_key:
|
110
91
|
specification_version: 4
|
111
92
|
summary: Create JSON structures via a Builder-style DSL
|
data/gemfiles/rails_3_0.gemfile
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
# This file was generated by Appraisal
|
2
|
-
|
3
|
-
source "https://rubygems.org"
|
4
|
-
|
5
|
-
gem "rake"
|
6
|
-
gem "mocha", :require => false
|
7
|
-
gem "appraisal"
|
8
|
-
gem "pry"
|
9
|
-
gem "test-unit"
|
10
|
-
gem "railties", "~> 3.0.0"
|
11
|
-
gem "actionpack", "~> 3.0.0"
|
12
|
-
gem "activemodel", "~> 3.0.0"
|
13
|
-
|
14
|
-
gemspec :path => "../"
|
data/gemfiles/rails_3_1.gemfile
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
# This file was generated by Appraisal
|
2
|
-
|
3
|
-
source "https://rubygems.org"
|
4
|
-
|
5
|
-
gem "rake"
|
6
|
-
gem "mocha", :require => false
|
7
|
-
gem "appraisal"
|
8
|
-
gem "pry"
|
9
|
-
gem "test-unit"
|
10
|
-
gem "railties", "~> 3.1.0"
|
11
|
-
gem "actionpack", "~> 3.1.0"
|
12
|
-
gem "activemodel", "~> 3.1.0"
|
13
|
-
|
14
|
-
gemspec :path => "../"
|
data/gemfiles/rails_3_2.gemfile
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
# This file was generated by Appraisal
|
2
|
-
|
3
|
-
source "https://rubygems.org"
|
4
|
-
|
5
|
-
gem "rake"
|
6
|
-
gem "mocha", :require => false
|
7
|
-
gem "appraisal"
|
8
|
-
gem "pry"
|
9
|
-
gem "test-unit"
|
10
|
-
gem "railties", "~> 3.2.0"
|
11
|
-
gem "actionpack", "~> 3.2.0"
|
12
|
-
gem "activemodel", "~> 3.2.0"
|
13
|
-
|
14
|
-
gemspec :path => "../"
|
data/gemfiles/rails_4_0.gemfile
DELETED
@@ -1,13 +0,0 @@
|
|
1
|
-
# This file was generated by Appraisal
|
2
|
-
|
3
|
-
source "https://rubygems.org"
|
4
|
-
|
5
|
-
gem "rake"
|
6
|
-
gem "mocha", :require => false
|
7
|
-
gem "appraisal"
|
8
|
-
gem "pry"
|
9
|
-
gem "railties", "~> 4.0.0"
|
10
|
-
gem "actionpack", "~> 4.0.0"
|
11
|
-
gem "activemodel", "~> 4.0.0"
|
12
|
-
|
13
|
-
gemspec :path => "../"
|
data/gemfiles/rails_4_1.gemfile
DELETED
@@ -1,13 +0,0 @@
|
|
1
|
-
# This file was generated by Appraisal
|
2
|
-
|
3
|
-
source "https://rubygems.org"
|
4
|
-
|
5
|
-
gem "rake"
|
6
|
-
gem "mocha", :require => false
|
7
|
-
gem "appraisal"
|
8
|
-
gem "pry"
|
9
|
-
gem "railties", "~> 4.1.0"
|
10
|
-
gem "actionpack", "~> 4.1.0"
|
11
|
-
gem "activemodel", "~> 4.1.0"
|
12
|
-
|
13
|
-
gemspec :path => "../"
|
data/gemfiles/rails_4_2.gemfile
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
# This file was generated by Appraisal
|
2
|
-
|
3
|
-
source "https://rubygems.org"
|
4
|
-
|
5
|
-
gem "rake"
|
6
|
-
gem "mocha", :require => false
|
7
|
-
gem "appraisal"
|
8
|
-
gem "pry"
|
9
|
-
gem "railties", "~> 4.2.0"
|
10
|
-
gem "actionpack", "~> 4.2.0"
|
11
|
-
gem "activemodel", "~> 4.2.0"
|
12
|
-
gem "nokogiri", "< 1.7" if RUBY_VERSION < "2.1"
|
13
|
-
|
14
|
-
gemspec :path => "../"
|