jbuilder 2.3.2 → 2.9.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +1 -0
  3. data/.travis.yml +49 -25
  4. data/Appraisals +19 -36
  5. data/CHANGELOG.md +72 -3
  6. data/CONTRIBUTING.md +14 -8
  7. data/Gemfile +0 -1
  8. data/MIT-LICENSE +1 -1
  9. data/README.md +47 -22
  10. data/Rakefile +1 -5
  11. data/gemfiles/rails_4_2.gemfile +3 -6
  12. data/gemfiles/rails_5_0.gemfile +10 -0
  13. data/gemfiles/rails_5_1.gemfile +10 -0
  14. data/gemfiles/rails_5_2.gemfile +10 -0
  15. data/gemfiles/rails_6_0.gemfile +10 -0
  16. data/gemfiles/rails_head.gemfile +10 -0
  17. data/jbuilder.gemspec +4 -5
  18. data/lib/generators/rails/jbuilder_generator.rb +9 -2
  19. data/lib/generators/rails/scaffold_controller_generator.rb +7 -1
  20. data/lib/generators/rails/templates/api_controller.rb +2 -2
  21. data/lib/generators/rails/templates/controller.rb +2 -2
  22. data/lib/generators/rails/templates/index.json.jbuilder +1 -4
  23. data/lib/generators/rails/templates/partial.json.jbuilder +2 -0
  24. data/lib/generators/rails/templates/show.json.jbuilder +1 -1
  25. data/lib/jbuilder.rb +7 -7
  26. data/lib/jbuilder/errors.rb +7 -0
  27. data/lib/jbuilder/jbuilder_template.rb +61 -8
  28. data/lib/jbuilder/key_formatter.rb +2 -2
  29. data/lib/jbuilder/railtie.rb +12 -5
  30. data/test/jbuilder_dependency_tracker_test.rb +1 -1
  31. data/test/jbuilder_generator_test.rb +18 -4
  32. data/test/jbuilder_template_test.rb +204 -275
  33. data/test/jbuilder_test.rb +38 -2
  34. data/test/scaffold_api_controller_generator_test.rb +24 -11
  35. data/test/scaffold_controller_generator_test.rb +31 -18
  36. data/test/test_helper.rb +26 -6
  37. metadata +12 -36
  38. data/gemfiles/rails_3_0.gemfile +0 -14
  39. data/gemfiles/rails_3_1.gemfile +0 -14
  40. data/gemfiles/rails_3_2.gemfile +0 -14
  41. data/gemfiles/rails_4_0.gemfile +0 -13
  42. data/gemfiles/rails_4_1.gemfile +0 -13
  43. data/gemfiles/rails_edge.gemfile +0 -13
@@ -49,7 +49,7 @@ end
49
49
 
50
50
 
51
51
  class JbuilderTest < ActiveSupport::TestCase
52
- setup do
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
- assert_equal nil, result['content']
78
+ assert_nil result['content']
79
79
  end
80
80
 
81
81
  test 'multiple keys' do
@@ -686,4 +686,40 @@ class JbuilderTest < ActiveSupport::TestCase
686
686
  end
687
687
  end
688
688
  end
689
+
690
+ test "throws MergeError when trying to merge array with non-empty hash" do
691
+ assert_raise Jbuilder::MergeError do
692
+ jbuild do |json|
693
+ json.name "Daniel"
694
+ json.merge! []
695
+ end
696
+ end
697
+ end
698
+
699
+ test "throws MergeError when trying to merge hash with array" do
700
+ assert_raise Jbuilder::MergeError do
701
+ jbuild do |json|
702
+ json.array!
703
+ json.merge!({})
704
+ end
705
+ end
706
+ end
707
+
708
+ test "throws MergeError when trying to merge invalid objects" do
709
+ assert_raise Jbuilder::MergeError do
710
+ jbuild do |json|
711
+ json.name "Daniel"
712
+ json.merge! "Nope"
713
+ end
714
+ end
715
+ end
716
+
717
+ if RUBY_VERSION >= "2.2.10"
718
+ test "respects JSON encoding customizations" do
719
+ # Active Support overrides Time#as_json for custom formatting.
720
+ # Ensure we call #to_json on the final attributes instead of JSON.dump.
721
+ result = JSON.load(Jbuilder.encode { |json| json.time Time.parse("2018-05-13 11:51:00.485 -0400") })
722
+ assert_equal "2018-05-13T11:51:00.485-04:00", result["time"]
723
+ end
724
+ end
689
725
  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
 
@@ -15,7 +15,7 @@ if Rails::VERSION::MAJOR > 4
15
15
 
16
16
  assert_file 'app/controllers/posts_controller.rb' do |content|
17
17
  assert_instance_method :index, content do |m|
18
- assert_match /@posts = Post\.all/, m
18
+ assert_match %r{@posts = Post\.all}, m
19
19
  end
20
20
 
21
21
  assert_instance_method :show, content do |m|
@@ -23,23 +23,36 @@ if Rails::VERSION::MAJOR > 4
23
23
  end
24
24
 
25
25
  assert_instance_method :create, content do |m|
26
- assert_match /@post = Post\.new\(post_params\)/, m
27
- assert_match /@post\.save/, m
28
- assert_match /render :show, status: :created, location: @post/, m
29
- assert_match /render json: @post\.errors, status: :unprocessable_entity/, m
26
+ assert_match %r{@post = Post\.new\(post_params\)}, m
27
+ assert_match %r{@post\.save}, m
28
+ assert_match %r{render :show, status: :created, location: @post}, m
29
+ assert_match %r{render json: @post\.errors, status: :unprocessable_entity}, m
30
30
  end
31
31
 
32
32
  assert_instance_method :update, content do |m|
33
- assert_match /render :show, status: :ok, location: @post/, m
34
- assert_match /render json: @post.errors, status: :unprocessable_entity/, m
33
+ assert_match %r{render :show, status: :ok, location: @post}, m
34
+ assert_match %r{render json: @post.errors, status: :unprocessable_entity}, m
35
35
  end
36
36
 
37
37
  assert_instance_method :destroy, content do |m|
38
- assert_match /@post\.destroy/, m
38
+ assert_match %r{@post\.destroy}, m
39
39
  end
40
40
 
41
- assert_match(/def post_params/, content)
42
- assert_match(/params\.require\(:post\)\.permit\(:title, :body\)/, content)
41
+ assert_match %r{def post_params}, content
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
47
+ end
48
+ end
49
+
50
+ test 'dont use require and permit if there are no attributes' do
51
+ run_generator %w(Post --api)
52
+
53
+ assert_file 'app/controllers/posts_controller.rb' do |content|
54
+ assert_match %r{def post_params}, content
55
+ assert_match %r{params\.fetch\(:post, \{\}\)}, content
43
56
  end
44
57
  end
45
58
  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
 
@@ -13,7 +13,7 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase
13
13
 
14
14
  assert_file 'app/controllers/posts_controller.rb' do |content|
15
15
  assert_instance_method :index, content do |m|
16
- assert_match /@posts = Post\.all/, m
16
+ assert_match %r{@posts = Post\.all}, m
17
17
  end
18
18
 
19
19
  assert_instance_method :show, content do |m|
@@ -21,7 +21,7 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase
21
21
  end
22
22
 
23
23
  assert_instance_method :new, content do |m|
24
- assert_match /@post = Post\.new/, m
24
+ assert_match %r{@post = Post\.new}, m
25
25
  end
26
26
 
27
27
  assert_instance_method :edit, content do |m|
@@ -29,29 +29,42 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase
29
29
  end
30
30
 
31
31
  assert_instance_method :create, content do |m|
32
- assert_match /@post = Post\.new\(post_params\)/, m
33
- assert_match /@post\.save/, m
34
- assert_match /format\.html \{ redirect_to @post, notice: 'Post was successfully created\.' \}/, m
35
- assert_match /format\.json \{ render :show, status: :created, location: @post \}/, m
36
- assert_match /format\.html \{ render :new \}/, m
37
- assert_match /format\.json \{ render json: @post\.errors, status: :unprocessable_entity \}/, m
32
+ assert_match %r{@post = Post\.new\(post_params\)}, m
33
+ assert_match %r{@post\.save}, m
34
+ assert_match %r{format\.html \{ redirect_to @post, notice: 'Post was successfully created\.' \}}, m
35
+ assert_match %r{format\.json \{ render :show, status: :created, location: @post \}}, m
36
+ assert_match %r{format\.html \{ render :new \}}, m
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 /format\.html \{ redirect_to @post, notice: 'Post was successfully updated\.' \}/, m
42
- assert_match /format\.json \{ render :show, status: :ok, location: @post \}/, m
43
- assert_match /format\.html \{ render :edit \}/, m
44
- assert_match /format\.json \{ render json: @post.errors, status: :unprocessable_entity \}/, m
41
+ assert_match %r{format\.html \{ redirect_to @post, notice: 'Post was successfully updated\.' \}}, m
42
+ assert_match %r{format\.json \{ render :show, status: :ok, location: @post \}}, m
43
+ assert_match %r{format\.html \{ render :edit \}}, m
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
- assert_match /@post\.destroy/, m
49
- assert_match /format\.html \{ redirect_to posts_url, notice: 'Post was successfully destroyed\.' \}/, m
50
- assert_match /format\.json \{ head :no_content \}/, m
48
+ assert_match %r{@post\.destroy}, m
49
+ assert_match %r{format\.html \{ redirect_to posts_url, notice: 'Post was successfully destroyed\.' \}}, m
50
+ assert_match %r{format\.json \{ head :no_content \}}, m
51
51
  end
52
52
 
53
- assert_match(/def post_params/, content)
54
- assert_match(/params\.require\(:post\)\.permit\(:title, :body\)/, content)
53
+ assert_match %r{def post_params}, content
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
59
+ end
60
+ end
61
+
62
+ test 'dont use require and permit if there are no attributes' do
63
+ run_generator %w(Post)
64
+
65
+ assert_file 'app/controllers/posts_controller.rb' do |content|
66
+ assert_match %r{def post_params}, content
67
+ assert_match %r{params\.fetch\(:post, \{\}\)}, content
55
68
  end
56
69
  end
57
70
  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
- if Rails::VERSION::STRING > "4.0"
6
- require "active_support/testing/autorun"
7
- else
8
- require "test/unit"
11
+ require "jbuilder"
12
+
13
+ require "active_support/testing/autorun"
14
+ require "mocha/setup"
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
- if ActiveSupport.respond_to?(:test_order=)
13
- ActiveSupport.test_order = :random
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,15 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jbuilder
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.3.2
4
+ version: 2.9.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Heinemeier Hansson
8
- - Pavel Pravosud
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2015-09-30 00:00:00.000000000 Z
11
+ date: 2019-05-14 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: activesupport
@@ -17,38 +16,16 @@ dependencies:
17
16
  requirements:
18
17
  - - ">="
19
18
  - !ruby/object:Gem::Version
20
- version: 3.0.0
21
- - - "<"
22
- - !ruby/object:Gem::Version
23
- version: '5'
19
+ version: 4.2.0
24
20
  type: :runtime
25
21
  prerelease: false
26
22
  version_requirements: !ruby/object:Gem::Requirement
27
23
  requirements:
28
24
  - - ">="
29
25
  - !ruby/object:Gem::Version
30
- version: 3.0.0
31
- - - "<"
32
- - !ruby/object:Gem::Version
33
- version: '5'
34
- - !ruby/object:Gem::Dependency
35
- name: multi_json
36
- requirement: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - "~>"
39
- - !ruby/object:Gem::Version
40
- version: '1.2'
41
- type: :runtime
42
- prerelease: false
43
- version_requirements: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - "~>"
46
- - !ruby/object:Gem::Version
47
- version: '1.2'
26
+ version: 4.2.0
48
27
  description:
49
- email:
50
- - david@37signals.com
51
- - pavel@pravosud.com
28
+ email: david@basecamp.com
52
29
  executables: []
53
30
  extensions: []
54
31
  extra_rdoc_files: []
@@ -62,19 +39,19 @@ files:
62
39
  - MIT-LICENSE
63
40
  - README.md
64
41
  - Rakefile
65
- - gemfiles/rails_3_0.gemfile
66
- - gemfiles/rails_3_1.gemfile
67
- - gemfiles/rails_3_2.gemfile
68
- - gemfiles/rails_4_0.gemfile
69
- - gemfiles/rails_4_1.gemfile
70
42
  - gemfiles/rails_4_2.gemfile
71
- - gemfiles/rails_edge.gemfile
43
+ - gemfiles/rails_5_0.gemfile
44
+ - gemfiles/rails_5_1.gemfile
45
+ - gemfiles/rails_5_2.gemfile
46
+ - gemfiles/rails_6_0.gemfile
47
+ - gemfiles/rails_head.gemfile
72
48
  - jbuilder.gemspec
73
49
  - lib/generators/rails/jbuilder_generator.rb
74
50
  - lib/generators/rails/scaffold_controller_generator.rb
75
51
  - lib/generators/rails/templates/api_controller.rb
76
52
  - lib/generators/rails/templates/controller.rb
77
53
  - lib/generators/rails/templates/index.json.jbuilder
54
+ - lib/generators/rails/templates/partial.json.jbuilder
78
55
  - lib/generators/rails/templates/show.json.jbuilder
79
56
  - lib/jbuilder.rb
80
57
  - lib/jbuilder/blank.rb
@@ -110,8 +87,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
110
87
  - !ruby/object:Gem::Version
111
88
  version: '0'
112
89
  requirements: []
113
- rubyforge_project:
114
- rubygems_version: 2.4.8
90
+ rubygems_version: 3.0.3
115
91
  signing_key:
116
92
  specification_version: 4
117
93
  summary: Create JSON structures via a Builder-style DSL
@@ -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 => "../"
@@ -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 => "../"
@@ -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 => "../"
@@ -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 => "../"
@@ -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 => "../"
@@ -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 "rails", ">= 5.0.0.alpha", :github => "rails/rails"
10
- gem "arel", ">= 7.0.0.alpha", :github => "rails/arel"
11
- gem "rack", ">= 2.0.0.alpha", :github => "rack/rack"
12
-
13
- gemspec :path => "../"