1
1
require 'delegate'
2
- require 'active_support/concern'
3
2
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'
10
4
11
5
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:
35
7
class ScopedIterator < ::SimpleDelegator # :nodoc:
36
8
include Enumerable
37
9
@@ -40,16 +12,6 @@ def initialize(obj, scope)
40
12
@scope = scope
41
13
end
42
14
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:
53
15
def each_with_info
54
16
return enum_for ( :each_with_info ) unless block_given?
55
17
@@ -60,51 +22,29 @@ def each_with_info
60
22
end
61
23
62
24
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
69
25
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
79
29
end
80
- else
81
- # Rails 6.0 support:
82
- class CollectionRenderer < ::ActionView ::PartialRenderer # :nodoc:
83
- include CollectionRenderable
84
30
85
- def initialize ( lookup_context , options , &scope )
86
- super ( lookup_context )
87
- @options = options
88
- @scope = scope
89
- end
31
+ private
90
32
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 )
93
35
end
94
36
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
101
40
102
- def collection_with_template ( view , template )
103
- @collection = ScopedIterator . new ( @collection , @scope )
41
+ def json
42
+ @options [ :locals ] . fetch ( :json )
43
+ end
104
44
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
108
48
end
109
49
110
50
class EnumerableCompat < ::SimpleDelegator
0 commit comments