Module: ExpoTurbo::Rails::Controller
- Extended by:
- ActiveSupport::Concern
- Includes:
- ActionController::Helpers, ActionView::Rendering
- Defined in:
- lib/expo_turbo/rails/controller.rb
Instance Method Summary collapse
- #broadcast_expo_turbo_refresh_later_to(*streamables, request_id: ::Turbo.current_request_id, **attributes) ⇒ Object
- #broadcast_expo_turbo_refresh_to(*streamables, request_id: ::Turbo.current_request_id, **attributes) ⇒ Object
- #broadcast_expo_turbo_stream_later_to(*streamables, content: nil) ⇒ Object
- #broadcast_expo_turbo_stream_to(*streamables, content: nil) ⇒ Object
- #expo_turbo_cache_key(*keys) ⇒ Object
- #expo_turbo_cache_variant ⇒ Object
- #expo_turbo_frame_request? ⇒ Boolean
- #expo_turbo_frame_request_id ⇒ Object
- #expo_turbo_stream ⇒ Object
- #expo_turbo_vary_by_frame! ⇒ Object
- #render_expo_turbo(template, locals: {}, status: :ok) ⇒ Object
- #render_expo_turbo_stream(*streams, status: :ok) ⇒ Object
Instance Method Details
#broadcast_expo_turbo_refresh_later_to(*streamables, request_id: ::Turbo.current_request_id, **attributes) ⇒ Object
127 128 129 |
# File 'lib/expo_turbo/rails/controller.rb', line 127 def broadcast_expo_turbo_refresh_later_to(*streamables, request_id: ::Turbo.current_request_id, **attributes) ExpoTurbo::Rails::Streams.broadcast_refresh_later_to(*streamables, request_id:, **attributes) end |
#broadcast_expo_turbo_refresh_to(*streamables, request_id: ::Turbo.current_request_id, **attributes) ⇒ Object
123 124 125 |
# File 'lib/expo_turbo/rails/controller.rb', line 123 def broadcast_expo_turbo_refresh_to(*streamables, request_id: ::Turbo.current_request_id, **attributes) ExpoTurbo::Rails::Streams.broadcast_refresh_to(*streamables, request_id:, **attributes) end |
#broadcast_expo_turbo_stream_later_to(*streamables, content: nil) ⇒ Object
115 116 117 118 119 120 121 |
# File 'lib/expo_turbo/rails/controller.rb', line 115 def broadcast_expo_turbo_stream_later_to(*streamables, content: nil) raise ArgumentError, "provide content or a block, not both" if block_given? && !content.nil? content = yield(expo_turbo_stream) if block_given? expo_turbo_validate_broadcast_stream!(content) ExpoTurbo::Rails::Streams.broadcast_later_to(*streamables, content: content) end |
#broadcast_expo_turbo_stream_to(*streamables, content: nil) ⇒ Object
107 108 109 110 111 112 113 |
# File 'lib/expo_turbo/rails/controller.rb', line 107 def broadcast_expo_turbo_stream_to(*streamables, content: nil) raise ArgumentError, "provide content or a block, not both" if block_given? && !content.nil? content = yield(expo_turbo_stream) if block_given? expo_turbo_validate_broadcast_stream!(content) ExpoTurbo::Rails::Streams.broadcast_to(*streamables, content: content) end |
#expo_turbo_cache_key(*keys) ⇒ Object
85 86 87 88 |
# File 'lib/expo_turbo/rails/controller.rb', line 85 def expo_turbo_cache_key(*keys) expo_turbo_vary_by_frame! [*keys, *expo_turbo_cache_variant] end |
#expo_turbo_cache_variant ⇒ Object
72 73 74 |
# File 'lib/expo_turbo/rails/controller.rb', line 72 def expo_turbo_cache_variant Frames.cache_variant(expo_turbo_frame_request_id) end |
#expo_turbo_frame_request? ⇒ Boolean
62 63 64 |
# File 'lib/expo_turbo/rails/controller.rb', line 62 def expo_turbo_frame_request? expo_turbo_frame_request_id.present? end |
#expo_turbo_frame_request_id ⇒ Object
66 67 68 69 70 |
# File 'lib/expo_turbo/rails/controller.rb', line 66 def expo_turbo_frame_request_id frame_id = request.get_header("HTTP_TURBO_FRAME") frame_id = frame_id.dup.force_encoding(Encoding::UTF_8) if frame_id.is_a?(String) Frames.valid_id?(frame_id) ? frame_id : nil end |
#expo_turbo_stream ⇒ Object
90 91 92 |
# File 'lib/expo_turbo/rails/controller.rb', line 90 def expo_turbo_stream view_context.expo_turbo_stream end |
#expo_turbo_vary_by_frame! ⇒ Object
76 77 78 79 80 81 82 83 |
# File 'lib/expo_turbo/rails/controller.rb', line 76 def expo_turbo_vary_by_frame! values = response.headers["Vary"].to_s.split(",").map(&:strip).reject(&:blank?) return response.headers["Vary"] if values.include?("*") values << "Accept" if request.should_apply_vary_header? && values.none? { |value| value.casecmp?("Accept") } values << "Turbo-Frame" if values.none? { |value| value.casecmp?("Turbo-Frame") } response.set_header "Vary", values.join(", ") end |
#render_expo_turbo(template, locals: {}, status: :ok) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/expo_turbo/rails/controller.rb', line 41 def render_expo_turbo(template, locals: {}, status: :ok) body = render_to_string( inline: File.read(expo_turbo_template_file(template)), type: :erb, formats: [:xml], layout: false, locals: locals ) raise TemplateError, "Expo Turbo templates must render valid UTF-8" unless body.encoding == Encoding::UTF_8 && body.valid_encoding? document = XmlFragments.parse_document(body) XmlFragments.validate_document_ids!(document) expo_turbo_validate_document!(document) render plain: body, content_type: MIME_TYPE, status: status rescue XmlFragments::DocumentIdError raise TemplateError, "Expo Turbo templates must use unique nonblank literal ids" rescue XmlFragments::ParseError raise TemplateError, "Expo Turbo templates must render well-formed UTF-8 XML" end |
#render_expo_turbo_stream(*streams, status: :ok) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/expo_turbo/rails/controller.rb', line 94 def render_expo_turbo_stream(*streams, status: :ok) streams << yield(expo_turbo_stream) if block_given? body = streams.flatten.compact.join raise TemplateError, "Expo Turbo Stream responses must render valid UTF-8" unless body.encoding == Encoding::UTF_8 && body.valid_encoding? document = XmlFragments.parse_stream_fragment(body) expo_turbo_validate_stream_fragment!(document) render plain: body, content_type: TURBO_STREAM_MIME_TYPE, status: status rescue XmlFragments::ParseError raise TemplateError, "Expo Turbo Stream responses must contain well-formed XML Stream fragments" end |