Module: Dommy::Rails::TurboStream

Defined in:
lib/dommy/rails/turbo_stream.rb

Class Method Summary collapse

Class Method Details

.find(body, action:, target:) ⇒ Object



12
13
14
15
16
# File 'lib/dommy/rails/turbo_stream.rb', line 12

def find(body, action:, target:)
  parse(body).find do |stream|
    stream.get_attribute("action") == action.to_s && stream.get_attribute("target") == target.to_s
  end
end

.fragment_document(stream) ⇒ Object

The <template> payload of a stream element, parsed as its own document (nil when the stream has no template).



24
25
26
27
# File 'lib/dommy/rails/turbo_stream.rb', line 24

def fragment_document(stream)
  fragment = stream.query_selector("template")&.inner_html
  fragment ? Dommy.parse(fragment).document : nil
end

.fragment_document_for(body, action:, target:) ⇒ Object



34
35
36
37
# File 'lib/dommy/rails/turbo_stream.rb', line 34

def fragment_document_for(body, action:, target:)
  stream = find(body, action: action, target: target)
  stream ? fragment_document(stream) : nil
end

.fragment_for(body, action:, target:) ⇒ Object



29
30
31
32
# File 'lib/dommy/rails/turbo_stream.rb', line 29

def fragment_for(body, action:, target:)
  stream = find(body, action: action, target: target)
  stream ? stream.query_selector("template")&.inner_html : nil
end

.matches?(body, action:, target:) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/dommy/rails/turbo_stream.rb', line 18

def matches?(body, action:, target:)
  !find(body, action: action, target: target).nil?
end

.parse(body) ⇒ Object



8
9
10
# File 'lib/dommy/rails/turbo_stream.rb', line 8

def parse(body)
  Dommy.parse(body).document.query_selector_all("turbo-stream").to_a
end