11require 'delegate'
2- require 'active_support/concern'
32require '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
115class 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
0 commit comments