1
1
# frozen_string_literal: true
2
2
3
3
require 'delegate'
4
- require 'active_support/concern'
5
4
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'
12
6
13
7
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:
37
9
class ScopedIterator < ::SimpleDelegator # :nodoc:
38
10
include Enumerable
39
11
@@ -42,16 +14,6 @@ def initialize(obj, scope)
42
14
@scope = scope
43
15
end
44
16
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:
55
17
def each_with_info
56
18
return enum_for ( :each_with_info ) unless block_given?
57
19
@@ -62,51 +24,29 @@ def each_with_info
62
24
end
63
25
64
26
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
71
27
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
81
31
end
82
- else
83
- # Rails 6.0 support:
84
- class CollectionRenderer < ::ActionView ::PartialRenderer # :nodoc:
85
- include CollectionRenderable
86
32
87
- def initialize ( lookup_context , options , &scope )
88
- super ( lookup_context )
89
- @options = options
90
- @scope = scope
91
- end
33
+ private
92
34
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 )
95
37
end
96
38
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
103
42
104
- def collection_with_template ( view , template )
105
- @collection = ScopedIterator . new ( @collection , @scope )
43
+ def json
44
+ @options [ :locals ] . fetch ( :json )
45
+ end
106
46
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
110
50
end
111
51
112
52
class EnumerableCompat < ::SimpleDelegator
0 commit comments