Skip to content

Commit f966bbf

Browse files
authored
Merge pull request #594 from affinity/remove_rails_6_support
Cleanup project for Rails 7+ support
2 parents 2e244c4 + 42006d6 commit f966bbf

9 files changed

+189
-296
lines changed

jbuilder.gemspec

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

14-
s.required_ruby_version = '>= 2.2.2'
14+
s.required_ruby_version = '>= 3.0.0'
1515

16-
s.add_dependency 'activesupport', '>= 5.0.0'
17-
s.add_dependency 'actionview', '>= 5.0.0'
16+
s.add_dependency 'activesupport', '>= 7.0.0'
17+
s.add_dependency 'actionview', '>= 7.0.0'
1818

1919
if RUBY_ENGINE == 'rbx'
2020
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,39 +1,11 @@
11
# frozen_string_literal: true
22

33
require 'delegate'
4-
require 'active_support/concern'
54
require 'action_view'
6-
7-
begin
8-
require 'action_view/renderer/collection_renderer'
9-
rescue LoadError
10-
require 'action_view/renderer/partial_renderer'
11-
end
5+
require 'action_view/renderer/collection_renderer'
126

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

@@ -42,16 +14,6 @@ def initialize(obj, scope)
4214
@scope = scope
4315
end
4416

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

@@ -62,51 +24,29 @@ def each_with_info
6224
end
6325

6426
private_constant :ScopedIterator
65-
end
66-
67-
if defined?(::ActionView::CollectionRenderer)
68-
# Rails 6.1 support:
69-
class CollectionRenderer < ::ActionView::CollectionRenderer # :nodoc:
70-
include CollectionRenderable
7127

72-
def initialize(lookup_context, options, &scope)
73-
super(lookup_context, options)
74-
@scope = scope
75-
end
76-
77-
private
78-
def collection_with_template(view, template, layout, collection)
79-
super(view, template, layout, ScopedIterator.new(collection, @scope))
80-
end
28+
def initialize(lookup_context, options, &scope)
29+
super(lookup_context, options)
30+
@scope = scope
8131
end
82-
else
83-
# Rails 6.0 support:
84-
class CollectionRenderer < ::ActionView::PartialRenderer # :nodoc:
85-
include CollectionRenderable
8632

87-
def initialize(lookup_context, options, &scope)
88-
super(lookup_context)
89-
@options = options
90-
@scope = scope
91-
end
33+
private
9234

93-
def render_collection_with_partial(collection, partial, context, block)
94-
render(context, @options.merge(collection: collection, partial: partial), block)
35+
def build_rendered_template(content, template, layout = nil)
36+
super(content || json.attributes!, template)
9537
end
9638

97-
private
98-
def collection_without_template(view)
99-
@collection = ScopedIterator.new(@collection, @scope)
100-
101-
super(view)
102-
end
39+
def build_rendered_collection(templates, _spacer)
40+
json.merge!(templates.map(&:body))
41+
end
10342

104-
def collection_with_template(view, template)
105-
@collection = ScopedIterator.new(@collection, @scope)
43+
def json
44+
@options[:locals].fetch(:json)
45+
end
10646

107-
super(view, template)
108-
end
109-
end
47+
def collection_with_template(view, template, layout, collection)
48+
super(view, template, layout, ScopedIterator.new(collection, @scope))
49+
end
11050
end
11151

11252
class EnumerableCompat < ::SimpleDelegator

lib/jbuilder/jbuilder_template.rb

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

149-
if as && options.key?(:collection) && CollectionRenderer.supported?
149+
if as && options.key?(:collection)
150150
collection = options.delete(:collection) || []
151151
partial = options.delete(:partial)
152152
options[:locals].merge!(json: self)
@@ -169,22 +169,6 @@ def _render_partial_with_options(options)
169169
else
170170
array!
171171
end
172-
elsif as && options.key?(:collection) && !CollectionRenderer.supported?
173-
# For Rails <= 5.2:
174-
as = as.to_sym
175-
collection = options.delete(:collection)
176-
177-
if collection.present?
178-
locals = options.delete(:locals)
179-
array! collection do |member|
180-
member_locals = locals.clone
181-
member_locals.merge! collection: collection
182-
member_locals.merge! as => member
183-
_render_partial options.merge(locals: member_locals)
184-
end
185-
else
186-
array!
187-
end
188172
else
189173
_render_partial options
190174
end

lib/jbuilder/railtie.rb

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,24 @@ class Railtie < ::Rails::Railtie
1111
require 'jbuilder/jbuilder_dependency_tracker'
1212
end
1313

14-
if Rails::VERSION::MAJOR >= 5
15-
module ::ActionController
16-
module ApiRendering
17-
include ActionView::Rendering
18-
end
14+
module ::ActionController
15+
module ApiRendering
16+
include ActionView::Rendering
1917
end
18+
end
2019

21-
ActiveSupport.on_load :action_controller do
22-
if name == 'ActionController::API'
23-
include ActionController::Helpers
24-
include ActionController::ImplicitRender
25-
helper_method :combined_fragment_cache_key
26-
helper_method :view_cache_dependencies
27-
end
28-
end
20+
ActiveSupport.on_load :action_controller_api do
21+
include ActionController::Helpers
22+
include ActionController::ImplicitRender
23+
helper_method :combined_fragment_cache_key
24+
helper_method :view_cache_dependencies
2925
end
3026
end
3127

32-
if Rails::VERSION::MAJOR >= 4
33-
generators do |app|
34-
Rails::Generators.configure! app.config.generators
35-
Rails::Generators.hidden_namespaces.uniq!
36-
require 'generators/rails/scaffold_controller_generator'
37-
end
28+
generators do |app|
29+
Rails::Generators.configure! app.config.generators
30+
Rails::Generators.hidden_namespaces.uniq!
31+
require 'generators/rails/scaffold_controller_generator'
3832
end
3933
end
4034
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)