@@ -5,56 +5,112 @@ class Template
5
5
DataWithSource = Struct . new ( :format , :identifier , :short_identifier , :type , keyword_init : true )
6
6
DataNoSource = Struct . new ( :source , :identifier , :type , keyword_init : true )
7
7
8
- attr_reader :variant , :this_format , :type
8
+ attr_reader :details
9
+
10
+ delegate :format , :variant , to : :details
9
11
10
12
def initialize (
11
13
component :,
12
- type :,
13
- this_format : nil ,
14
- variant : nil ,
14
+ details :,
15
15
lineno : nil ,
16
16
path : nil ,
17
- extension : nil ,
18
- source : nil ,
19
- method_name : nil ,
20
- defined_on_self : true
17
+ method_name : nil
21
18
)
22
19
@component = component
23
- @type = type
24
- @this_format = this_format
25
- @variant = variant &.to_sym
20
+ @details = details
26
21
@lineno = lineno
27
22
@path = path
28
- @extension = extension
29
- @source = source
30
23
@method_name = method_name
31
- @defined_on_self = defined_on_self
32
-
33
- @source_originally_nil = @source . nil?
34
24
35
25
@call_method_name =
36
26
if @method_name
37
27
@method_name
38
28
else
39
29
out = +"call"
40
- out << "_#{ normalized_variant_name } " if @ variant. present?
41
- out << "_#{ @this_format } " if @this_format . present? && @this_format != ViewComponent ::Base ::VC_INTERNAL_DEFAULT_FORMAT
30
+ out << "_#{ normalized_variant_name } " if variant . present?
31
+ out << "_#{ format } " if format . present? && format != ViewComponent ::Base ::VC_INTERNAL_DEFAULT_FORMAT
42
32
out
43
33
end
44
34
end
45
35
36
+ class File < Template
37
+ def initialize ( component :, details :, path :)
38
+ super (
39
+ component : component ,
40
+ details : details ,
41
+ path : path ,
42
+ lineno : 0
43
+ )
44
+ end
45
+
46
+ def type
47
+ :file
48
+ end
49
+
50
+ # Load file each time we look up #source in case the file has been modified
51
+ def source
52
+ ::File . read ( @path )
53
+ end
54
+ end
55
+
56
+ class Inline < Template
57
+ attr_reader :source
58
+
59
+ def initialize ( component :, inline_template :)
60
+ details = ActionView ::TemplateDetails . new ( nil , inline_template . language . to_sym , nil , nil )
61
+
62
+ super (
63
+ component : component ,
64
+ details : details ,
65
+ path : inline_template . path ,
66
+ lineno : inline_template . lineno ,
67
+ )
68
+
69
+ @source = inline_template . source . dup
70
+ end
71
+
72
+ def type
73
+ :inline
74
+ end
75
+ end
76
+
77
+ class InlineCall < Template
78
+ def initialize ( component :, method_name :, defined_on_self :)
79
+ variant = method_name . to_s . include? ( "call_" ) ? method_name . to_s . sub ( "call_" , "" ) . to_sym : nil
80
+ details = ActionView ::TemplateDetails . new ( nil , nil , nil , variant )
81
+
82
+ super (
83
+ component : component ,
84
+ details : details ,
85
+ method_name : method_name
86
+ )
87
+
88
+ @defined_on_self = defined_on_self
89
+ end
90
+
91
+ def type
92
+ :inline_call
93
+ end
94
+
95
+ def compile_to_component
96
+ @component . define_method ( safe_method_name , @component . instance_method ( @call_method_name ) )
97
+ end
98
+
99
+ def defined_on_self?
100
+ @defined_on_self
101
+ end
102
+ end
103
+
46
104
def compile_to_component
47
- if !inline_call?
48
- @component . silence_redefinition_of_method ( @call_method_name )
105
+ @component . silence_redefinition_of_method ( @call_method_name )
49
106
50
- # rubocop:disable Style/EvalWithLocation
51
- @component . class_eval <<-RUBY , @path , @lineno
52
- def #{ @call_method_name }
53
- #{ compiled_source }
54
- end
55
- RUBY
56
- # rubocop:enable Style/EvalWithLocation
107
+ # rubocop:disable Style/EvalWithLocation
108
+ @component . class_eval <<-RUBY , @path , @lineno
109
+ def #{ @call_method_name }
110
+ #{ compiled_source }
57
111
end
112
+ RUBY
113
+ # rubocop:enable Style/EvalWithLocation
58
114
59
115
@component . define_method ( safe_method_name , @component . instance_method ( @call_method_name ) )
60
116
end
@@ -72,55 +128,39 @@ def requires_compiled_superclass?
72
128
end
73
129
74
130
def inline_call?
75
- @ type == :inline_call
131
+ type == :inline_call
76
132
end
77
133
78
134
def inline?
79
- @ type == :inline
135
+ type == :inline
80
136
end
81
137
82
138
def default_format?
83
- @this_format == ViewComponent ::Base ::VC_INTERNAL_DEFAULT_FORMAT
84
- end
85
-
86
- def format
87
- @this_format
139
+ format . nil? || format == ViewComponent ::Base ::VC_INTERNAL_DEFAULT_FORMAT
88
140
end
89
141
90
142
def safe_method_name
91
143
"_#{ @call_method_name } _#{ @component . name . underscore . gsub ( "/" , "__" ) } "
92
144
end
93
145
94
146
def normalized_variant_name
95
- @variant . to_s . gsub ( "-" , "__" ) . gsub ( "." , "___" )
96
- end
97
-
98
- def defined_on_self?
99
- @defined_on_self
147
+ variant . to_s . gsub ( "-" , "__" )
100
148
end
101
149
102
150
private
103
151
104
- def source
105
- if @source_originally_nil
106
- # Load file each time we look up #source in case the file has been modified
107
- File . read ( @path )
108
- else
109
- @source
110
- end
111
- end
112
-
113
152
def compiled_source
114
- handler = ActionView :: Template . handler_for_extension ( @extension )
153
+ handler = details . handler_class
115
154
this_source = source
116
155
this_source . rstrip! if @component . strip_trailing_whitespace?
117
156
118
157
short_identifier = defined? ( Rails . root ) ? @path . sub ( "#{ Rails . root } /" , "" ) : @path
119
- type = ActionView ::Template ::Types [ @this_format ]
158
+ format = self . format || ViewComponent ::Base ::VC_INTERNAL_DEFAULT_FORMAT
159
+ type = ActionView ::Template ::Types [ format ]
120
160
121
161
if handler . method ( :call ) . parameters . length > 1
122
162
handler . call (
123
- DataWithSource . new ( format : @this_format , identifier : @path , short_identifier : short_identifier , type : type ) ,
163
+ DataWithSource . new ( format : format , identifier : @path , short_identifier : short_identifier , type : type ) ,
124
164
this_source
125
165
)
126
166
# :nocov:
0 commit comments