Skip to content

Commit d604797

Browse files
authored
Merge branch 'main' into skip_empty_collection_partial_render
2 parents d8088d6 + 8dbc23f commit d604797

14 files changed

+33
-147
lines changed

.github/workflows/ruby.yml

-43
Original file line numberDiff line numberDiff line change
@@ -15,64 +15,21 @@ jobs:
1515
fail-fast: false
1616
matrix:
1717
ruby:
18-
- "2.7"
1918
- "3.0"
2019
- "3.1"
2120
- "3.2"
2221
- "3.3"
2322

2423
gemfile:
25-
- "rails_6_1"
2624
- "rails_7_0"
2725
- "rails_7_1"
2826
- "rails_head"
2927

3028
exclude:
31-
- ruby: 2.7
32-
gemfile: rails_head
3329
- ruby: '3.0'
3430
gemfile: rails_head
3531

3632
include:
37-
- ruby: 2.2
38-
gemfile: rails_5_0
39-
- ruby: 2.2
40-
gemfile: rails_5_1
41-
- ruby: 2.3
42-
gemfile: rails_5_0
43-
- ruby: 2.3
44-
gemfile: rails_5_1
45-
- ruby: 2.3
46-
gemfile: rails_5_2
47-
- ruby: 2.4
48-
gemfile: rails_5_0
49-
- ruby: 2.4
50-
gemfile: rails_5_1
51-
- ruby: 2.4
52-
gemfile: rails_5_2
53-
- ruby: 2.5
54-
gemfile: rails_5_0
55-
- ruby: 2.5
56-
gemfile: rails_5_1
57-
- ruby: 2.5
58-
gemfile: rails_5_2
59-
- ruby: 2.5
60-
gemfile: rails_6_0
61-
- ruby: 2.5
62-
gemfile: rails_6_1
63-
- ruby: 2.6
64-
gemfile: rails_5_0
65-
- ruby: 2.6
66-
gemfile: rails_5_1
67-
- ruby: 2.6
68-
gemfile: rails_5_2
69-
- ruby: 2.6
70-
gemfile: rails_6_0
71-
- ruby: 2.6
72-
gemfile: rails_6_1
73-
- ruby: 2.7
74-
gemfile: rails_6_0
75-
7633
- ruby: '3.1'
7734
gemfile: rails_head
7835
- ruby: '3.2'

Appraisals

-27
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,3 @@
1-
if RUBY_VERSION < "2.7.0"
2-
appraise "rails-5-0" do
3-
gem "rails", "~> 5.0.0"
4-
gem "loofah", "< 2.21.0"
5-
end
6-
7-
appraise "rails-5-1" do
8-
gem "rails", "~> 5.1.0"
9-
gem "loofah", "< 2.21.0"
10-
end
11-
12-
appraise "rails-5-2" do
13-
gem "rails", "~> 5.2.0"
14-
gem "loofah", "< 2.21.0"
15-
end
16-
end
17-
18-
if RUBY_VERSION >= "2.5.0"
19-
appraise "rails-6-0" do
20-
gem "rails", "~> 6.0.0"
21-
end
22-
23-
appraise "rails-6-1" do
24-
gem "rails", "~> 6.1.0"
25-
end
26-
end
27-
281
if RUBY_VERSION >= "2.7.0"
292
appraise "rails-7-0" do
303
gem "rails", "~> 7.0.0"

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,10 @@ environment.rb for example):
338338
Jbuilder.deep_format_keys true
339339
```
340340

341+
## Testing JBuilder Response body with RSpec
342+
343+
To test the response body of your controller spec, enable `render_views` in your RSpec context. This [configuration](https://rspec.info/features/6-0/rspec-rails/controller-specs/render-views) renders the views in a controller test.
344+
341345
## Contributing to Jbuilder
342346

343347
Jbuilder is the work of many contributors. You're encouraged to submit pull requests, propose

gemfiles/rails_5_0.gemfile

-11
This file was deleted.

gemfiles/rails_5_1.gemfile

-11
This file was deleted.

gemfiles/rails_5_2.gemfile

-11
This file was deleted.

gemfiles/rails_6_0.gemfile

-10
This file was deleted.

gemfiles/rails_6_1.gemfile

-10
This file was deleted.

jbuilder.gemspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Gem::Specification.new do |s|
22
s.name = 'jbuilder'
3-
s.version = '2.11.5'
3+
s.version = '2.12.0'
44
s.authors = 'David Heinemeier Hansson'
55
s.email = '[email protected]'
66
s.summary = 'Create JSON structures via a Builder-style DSL'

lib/generators/rails/templates/controller.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def create
3030

3131
respond_to do |format|
3232
if @<%= orm_instance.save %>
33-
format.html { redirect_to <%= show_helper %>, notice: <%= %("#{human_name} was successfully created.") %> }
33+
format.html { redirect_to <%= redirect_resource_name %>, notice: <%= %("#{human_name} was successfully created.") %> }
3434
format.json { render :show, status: :created, location: <%= "@#{singular_table_name}" %> }
3535
else
3636
format.html { render :new, status: :unprocessable_entity }
@@ -43,7 +43,7 @@ def create
4343
def update
4444
respond_to do |format|
4545
if @<%= orm_instance.update("#{singular_table_name}_params") %>
46-
format.html { redirect_to <%= show_helper %>, notice: <%= %("#{human_name} was successfully updated.") %> }
46+
format.html { redirect_to <%= redirect_resource_name %>, notice: <%= %("#{human_name} was successfully updated.") %> }
4747
format.json { render :show, status: :ok, location: <%= "@#{singular_table_name}" %> }
4848
else
4949
format.html { render :edit, status: :unprocessable_entity }
@@ -57,7 +57,7 @@ def destroy
5757
@<%= orm_instance.destroy %>
5858
5959
respond_to do |format|
60-
format.html { redirect_to <%= index_helper %>_url, notice: <%= %("#{human_name} was successfully destroyed.") %> }
60+
format.html { redirect_to <%= index_helper %>_path, notice: <%= %("#{human_name} was successfully destroyed.") %> }
6161
format.json { head :no_content }
6262
end
6363
end

lib/jbuilder.rb

+9-6
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44
require 'jbuilder/key_formatter'
55
require 'jbuilder/errors'
66
require 'json'
7-
require 'ostruct'
87
require 'active_support/core_ext/hash/deep_merge'
8+
begin
9+
require 'ostruct'
10+
rescue LoadError
11+
end
912

1013
class Jbuilder
1114
@@key_formatter = nil
@@ -28,7 +31,7 @@ def self.encode(*args, &block)
2831
end
2932

3033
BLANK = Blank.new
31-
NON_ENUMERABLES = [ ::Struct, ::OpenStruct ].to_set
34+
NON_ENUMERABLES = defined?(::OpenStruct) ? [::Struct, ::OpenStruct].to_set : [::Struct].to_set
3235

3336
def set!(key, value = BLANK, *args, &block)
3437
result = if ::Kernel.block_given?
@@ -292,7 +295,7 @@ def _extract_method_values(object, attributes)
292295

293296
def _merge_block(key)
294297
current_value = _blank? ? BLANK : @attributes.fetch(_key(key), BLANK)
295-
raise NullError.build(key) if current_value.nil?
298+
::Kernel.raise NullError.build(key) if current_value.nil?
296299
new_value = _scope{ yield self }
297300
_merge_values(current_value, new_value)
298301
end
@@ -307,7 +310,7 @@ def _merge_values(current_value, updates)
307310
elsif ::Hash === current_value && ::Hash === updates
308311
current_value.deep_merge(updates)
309312
else
310-
raise MergeError.build(current_value, updates)
313+
::Kernel.raise MergeError.build(current_value, updates)
311314
end
312315
end
313316

@@ -328,8 +331,8 @@ def _format_keys(hash_or_array)
328331
end
329332

330333
def _set_value(key, value)
331-
raise NullError.build(key) if @attributes.nil?
332-
raise ArrayError.build(key) if ::Array === @attributes
334+
::Kernel.raise NullError.build(key) if @attributes.nil?
335+
::Kernel.raise ArrayError.build(key) if ::Array === @attributes
333336
return if @ignore_nil && value.nil? or _blank?(value)
334337
@attributes = {} if _blank?
335338
@attributes[_key(key)] = value

lib/jbuilder/jbuilder.rb

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1 @@
1-
Jbuilder = Class.new(begin
2-
require 'active_support/proxy_object'
3-
ActiveSupport::ProxyObject
4-
rescue LoadError
5-
require 'active_support/basic_object'
6-
ActiveSupport::BasicObject
7-
end)
1+
Jbuilder = Class.new(BasicObject)

lib/jbuilder/jbuilder_template.rb

+9-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def cache!(key=nil, options={})
8989
# # json.extra 'This will not work either, the root must be exclusive'
9090
def cache_root!(key=nil, options={})
9191
if @context.controller.perform_caching
92-
raise "cache_root! can't be used after JSON structures have been defined" if @attributes.present?
92+
::Kernel.raise "cache_root! can't be used after JSON structures have been defined" if @attributes.present?
9393

9494
@cached_root = _cache_fragment_for([ :root, key ], options) { yield; target! }
9595
else
@@ -147,6 +147,14 @@ def _render_partial_with_options(options)
147147
options[:locals].merge!(json: self)
148148
collection = EnumerableCompat.new(collection) if collection.respond_to?(:count) && !collection.respond_to?(:size)
149149

150+
if options.has_key?(:layout)
151+
::Kernel.raise ::NotImplementedError, "The `:layout' option is not supported in collection rendering."
152+
end
153+
154+
if options.has_key?(:spacer_template)
155+
::Kernel.raise ::NotImplementedError, "The `:spacer_template' option is not supported in collection rendering."
156+
end
157+
150158
if collection.present?
151159
if options.has_key?(:layout)
152160
raise ::NotImplementedError, "The `:layout' option is not supported in collection rendering."

test/scaffold_controller_generator_test.rb

+6-6
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,22 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase
3131
assert_instance_method :create, content do |m|
3232
assert_match %r{@post = Post\.new\(post_params\)}, m
3333
assert_match %r{@post\.save}, m
34-
assert_match %r{format\.html \{ redirect_to post_url\(@post\), notice: "Post was successfully created\." \}}, m
34+
assert_match %r{format\.html \{ redirect_to @post, notice: "Post was successfully created\." \}}, m
3535
assert_match %r{format\.json \{ render :show, status: :created, location: @post \}}, m
3636
assert_match %r{format\.html \{ render :new, status: :unprocessable_entity \}}, m
3737
assert_match %r{format\.json \{ render json: @post\.errors, status: :unprocessable_entity \}}, m
3838
end
3939

4040
assert_instance_method :update, content do |m|
41-
assert_match %r{format\.html \{ redirect_to post_url\(@post\), notice: "Post was successfully updated\." \}}, m
41+
assert_match %r{format\.html \{ redirect_to @post, notice: "Post was successfully updated\." \}}, m
4242
assert_match %r{format\.json \{ render :show, status: :ok, location: @post \}}, m
4343
assert_match %r{format\.html \{ render :edit, status: :unprocessable_entity \}}, m
4444
assert_match %r{format\.json \{ render json: @post.errors, status: :unprocessable_entity \}}, m
4545
end
4646

4747
assert_instance_method :destroy, content do |m|
4848
assert_match %r{@post\.destroy}, m
49-
assert_match %r{format\.html \{ redirect_to posts_url, notice: "Post was successfully destroyed\." \}}, m
49+
assert_match %r{format\.html \{ redirect_to posts_path, notice: "Post was successfully destroyed\." \}}, m
5050
assert_match %r{format\.json \{ head :no_content \}}, m
5151
end
5252

@@ -64,15 +64,15 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase
6464
run_generator %w(Admin::Post --model-name=Post)
6565
assert_file 'app/controllers/admin/posts_controller.rb' do |content|
6666
assert_instance_method :create, content do |m|
67-
assert_match %r{format\.html \{ redirect_to admin_post_url\(@post\), notice: "Post was successfully created\." \}}, m
67+
assert_match %r{format\.html \{ redirect_to \[:admin, @post\], notice: "Post was successfully created\." \}}, m
6868
end
6969

7070
assert_instance_method :update, content do |m|
71-
assert_match %r{format\.html \{ redirect_to admin_post_url\(@post\), notice: "Post was successfully updated\." \}}, m
71+
assert_match %r{format\.html \{ redirect_to \[:admin, @post\], notice: "Post was successfully updated\." \}}, m
7272
end
7373

7474
assert_instance_method :destroy, content do |m|
75-
assert_match %r{format\.html \{ redirect_to admin_posts_url, notice: "Post was successfully destroyed\." \}}, m
75+
assert_match %r{format\.html \{ redirect_to admin_posts_path, notice: "Post was successfully destroyed\." \}}, m
7676
end
7777
end
7878
end

0 commit comments

Comments
 (0)