File tree Expand file tree Collapse file tree 6 files changed +41
-35
lines changed Expand file tree Collapse file tree 6 files changed +41
-35
lines changed Original file line number Diff line number Diff line change 11
11
appraise "rails edge" do
12
12
gem "rails" , github : "rails/rails"
13
13
end
14
-
15
- appraise "phlex 2.1" do
16
- gem "phlex" , "~> 2.1.0"
17
- end
Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+
3
+ class Components ::Cache < Phlex ::HTML
4
+ def initialize ( text )
5
+ @text = text
6
+ end
7
+
8
+ def cache_store = Rails . cache
9
+
10
+ def view_template
11
+ cache do
12
+ h1 { @text }
13
+ end
14
+ end
15
+ end
Original file line number Diff line number Diff line change @@ -136,27 +136,6 @@ def set_original_view_context(view_context)
136
136
# no-op (see https://github.com/ViewComponent/view_component/issues/2207)
137
137
end
138
138
139
- def vanish ( ...)
140
- # Quick fix because we need rails to handle the capture now
141
- capture ( ...)
142
- end
143
-
144
- def capture ( *args , &block )
145
- if capture_context
146
- return "" unless block
147
-
148
- @_state . around_capture do
149
- if args . length == 0
150
- capture_context . capture { __yield_content__ ( &block ) }
151
- else
152
- capture_context . capture ( *args ) { __yield_content_with_args__ ( *args , &block ) }
153
- end
154
- end
155
- else
156
- super
157
- end
158
- end
159
-
160
139
def enable_cache_reloading?
161
140
Rails . env . development?
162
141
end
Original file line number Diff line number Diff line change @@ -5,16 +5,21 @@ def buffer
5
5
@user_context [ :capture_context ] &.output_buffer &.raw_buffer || super
6
6
end
7
7
8
- def around_capture
9
- original_capturing = @capturing
10
- original_fragments = @fragments
8
+ def capture
9
+ if ( capture_context = @user_context [ :capture_context ] )
10
+ original_capturing = @capturing
11
+ original_fragments = @fragments
11
12
12
- begin
13
- @capturing = true
14
- yield
15
- ensure
16
- @capturing = original_capturing
17
- @fragments = original_fragments
13
+ capture_context . capture do
14
+ @capturing = true
15
+ @fragments = nil
16
+ yield
17
+ ensure
18
+ @capturing = original_capturing
19
+ @fragments = original_fragments
20
+ end
21
+ else
22
+ super
18
23
end
19
24
end
20
25
end
Original file line number Diff line number Diff line change @@ -28,6 +28,6 @@ Gem::Specification.new do |spec|
28
28
29
29
spec . require_paths = [ "lib" ]
30
30
31
- spec . add_dependency "phlex" , "~> 2.1.0 "
31
+ spec . add_dependency "phlex" , "~> 2.1.2 "
32
32
spec . add_dependency "railties" , ">= 7.1" , "< 9"
33
33
end
Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+
3
+ test "basic caching test" do
4
+ assert_equal_html render ( Components ::Cache . new ( "Original" ) ) , <<~HTML . strip
5
+ < h1 > Original</ h1 >
6
+ HTML
7
+
8
+ assert_equal_html render ( Components ::Cache . new ( "Something else" ) ) , <<~HTML . strip
9
+ < h1 > Original</ h1 >
10
+ HTML
11
+ end
You can’t perform that action at this time.
0 commit comments