Skip to content

Commit 0a67bbd

Browse files
authored
Cleanup project for Rails 7+ support (#3)
* Update gemspec for Rails 7 * Update gemspec for Ruby 3.0 * Cleanup test cases for Rails 7 * Cleanup Railtie for Rails 7 * Cleanup test cases for Ruby 3.0 * Refactor CollectionRenderer for Rails 7+ support * Cleanup one more spot in tests
1 parent a1ddb07 commit 0a67bbd

9 files changed

+188
-293
lines changed

jbuilder.gemspec

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ Gem::Specification.new do |s|
99
s.homepage = 'https://github.com/rails/jbuilder'
1010
s.license = 'MIT'
1111

12-
s.required_ruby_version = '>= 2.2.2'
12+
s.required_ruby_version = '>= 3.0.0'
1313

14-
s.add_dependency 'activesupport', '>= 5.0.0'
15-
s.add_dependency 'actionview', '>= 5.0.0'
14+
s.add_dependency 'activesupport', '>= 7.0.0'
15+
s.add_dependency 'actionview', '>= 7.0.0'
1616

1717
if RUBY_ENGINE == 'rbx'
1818
s.add_development_dependency('racc')

lib/jbuilder/collection_renderer.rb

Lines changed: 17 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,9 @@
11
require 'delegate'
2-
require 'active_support/concern'
32
require 'action_view'
4-
5-
begin
6-
require 'action_view/renderer/collection_renderer'
7-
rescue LoadError
8-
require 'action_view/renderer/partial_renderer'
9-
end
3+
require 'action_view/renderer/collection_renderer'
104

115
class Jbuilder
12-
module CollectionRenderable # :nodoc:
13-
extend ActiveSupport::Concern
14-
15-
class_methods do
16-
def supported?
17-
superclass.private_method_defined?(:build_rendered_template) && self.superclass.private_method_defined?(:build_rendered_collection)
18-
end
19-
end
20-
21-
private
22-
23-
def build_rendered_template(content, template, layout = nil)
24-
super(content || json.attributes!, template)
25-
end
26-
27-
def build_rendered_collection(templates, _spacer)
28-
json.merge!(templates.map(&:body))
29-
end
30-
31-
def json
32-
@options[:locals].fetch(:json)
33-
end
34-
6+
class CollectionRenderer < ::ActionView::CollectionRenderer # :nodoc:
357
class ScopedIterator < ::SimpleDelegator # :nodoc:
368
include Enumerable
379

@@ -40,16 +12,6 @@ def initialize(obj, scope)
4012
@scope = scope
4113
end
4214

43-
# Rails 6.0 support:
44-
def each
45-
return enum_for(:each) unless block_given?
46-
47-
__getobj__.each do |object|
48-
@scope.call { yield(object) }
49-
end
50-
end
51-
52-
# Rails 6.1 support:
5315
def each_with_info
5416
return enum_for(:each_with_info) unless block_given?
5517

@@ -60,51 +22,29 @@ def each_with_info
6022
end
6123

6224
private_constant :ScopedIterator
63-
end
64-
65-
if defined?(::ActionView::CollectionRenderer)
66-
# Rails 6.1 support:
67-
class CollectionRenderer < ::ActionView::CollectionRenderer # :nodoc:
68-
include CollectionRenderable
6925

70-
def initialize(lookup_context, options, &scope)
71-
super(lookup_context, options)
72-
@scope = scope
73-
end
74-
75-
private
76-
def collection_with_template(view, template, layout, collection)
77-
super(view, template, layout, ScopedIterator.new(collection, @scope))
78-
end
26+
def initialize(lookup_context, options, &scope)
27+
super(lookup_context, options)
28+
@scope = scope
7929
end
80-
else
81-
# Rails 6.0 support:
82-
class CollectionRenderer < ::ActionView::PartialRenderer # :nodoc:
83-
include CollectionRenderable
8430

85-
def initialize(lookup_context, options, &scope)
86-
super(lookup_context)
87-
@options = options
88-
@scope = scope
89-
end
31+
private
9032

91-
def render_collection_with_partial(collection, partial, context, block)
92-
render(context, @options.merge(collection: collection, partial: partial), block)
33+
def build_rendered_template(content, template, layout = nil)
34+
super(content || json.attributes!, template)
9335
end
9436

95-
private
96-
def collection_without_template(view)
97-
@collection = ScopedIterator.new(@collection, @scope)
98-
99-
super(view)
100-
end
37+
def build_rendered_collection(templates, _spacer)
38+
json.merge!(templates.map(&:body))
39+
end
10140

102-
def collection_with_template(view, template)
103-
@collection = ScopedIterator.new(@collection, @scope)
41+
def json
42+
@options[:locals].fetch(:json)
43+
end
10444

105-
super(view, template)
106-
end
107-
end
45+
def collection_with_template(view, template, layout, collection)
46+
super(view, template, layout, ScopedIterator.new(collection, @scope))
47+
end
10848
end
10949

11050
class EnumerableCompat < ::SimpleDelegator

lib/jbuilder/jbuilder_template.rb

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def _render_partial_with_options(options)
141141
options.reverse_merge! ::JbuilderTemplate.template_lookup_options
142142
as = options[:as]
143143

144-
if as && options.key?(:collection) && CollectionRenderer.supported?
144+
if as && options.key?(:collection)
145145
collection = options.delete(:collection) || []
146146
partial = options.delete(:partial)
147147
options[:locals].merge!(json: self)
@@ -164,22 +164,6 @@ def _render_partial_with_options(options)
164164
else
165165
array!
166166
end
167-
elsif as && options.key?(:collection) && !CollectionRenderer.supported?
168-
# For Rails <= 5.2:
169-
as = as.to_sym
170-
collection = options.delete(:collection)
171-
172-
if collection.present?
173-
locals = options.delete(:locals)
174-
array! collection do |member|
175-
member_locals = locals.clone
176-
member_locals.merge! collection: collection
177-
member_locals.merge! as => member
178-
_render_partial options.merge(locals: member_locals)
179-
end
180-
else
181-
array!
182-
end
183167
else
184168
_render_partial options
185169
end

lib/jbuilder/railtie.rb

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,24 @@ class Railtie < ::Rails::Railtie
99
require 'jbuilder/jbuilder_dependency_tracker'
1010
end
1111

12-
if Rails::VERSION::MAJOR >= 5
13-
module ::ActionController
14-
module ApiRendering
15-
include ActionView::Rendering
16-
end
12+
module ::ActionController
13+
module ApiRendering
14+
include ActionView::Rendering
1715
end
16+
end
1817

19-
ActiveSupport.on_load :action_controller do
20-
if name == 'ActionController::API'
21-
include ActionController::Helpers
22-
include ActionController::ImplicitRender
23-
end
18+
ActiveSupport.on_load :action_controller do
19+
if name == 'ActionController::API'
20+
include ActionController::Helpers
21+
include ActionController::ImplicitRender
2422
end
2523
end
2624
end
2725

28-
if Rails::VERSION::MAJOR >= 4
29-
generators do |app|
30-
Rails::Generators.configure! app.config.generators
31-
Rails::Generators.hidden_namespaces.uniq!
32-
require 'generators/rails/scaffold_controller_generator'
33-
end
26+
generators do |app|
27+
Rails::Generators.configure! app.config.generators
28+
Rails::Generators.hidden_namespaces.uniq!
29+
require 'generators/rails/scaffold_controller_generator'
3430
end
3531
end
3632
end

test/jbuilder_generator_test.rb

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,13 @@ class JbuilderGeneratorTest < Rails::Generators::TestCase
5656
end
5757
end
5858

59-
if Rails::VERSION::MAJOR >= 6
60-
test 'handles virtual attributes' do
61-
run_generator %w(Message content:rich_text video:attachment photos:attachments)
59+
test 'handles virtual attributes' do
60+
run_generator %w(Message content:rich_text video:attachment photos:attachments)
6261

63-
assert_file 'app/views/messages/_message.json.jbuilder' do |content|
64-
assert_match %r{json\.content message\.content\.to_s}, content
65-
assert_match %r{json\.video url_for\(message\.video\)}, content
66-
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
67-
end
62+
assert_file 'app/views/messages/_message.json.jbuilder' do |content|
63+
assert_match %r{json\.content message\.content\.to_s}, content
64+
assert_match %r{json\.video url_for\(message\.video\)}, content
65+
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
6866
end
6967
end
7068
end

0 commit comments

Comments
 (0)