Module: Plutonium::Helpers::TurboHelper

Defined in:
lib/plutonium/helpers/turbo_helper.rb

Instance Method Summary collapse

Instance Method Details

#current_turbo_frameObject



4
5
6
# File 'lib/plutonium/helpers/turbo_helper.rb', line 4

def current_turbo_frame
  request.headers["Turbo-Frame"]
end

#in_frame?Boolean

True when the request is rendered inside any turbo frame.

Returns:

  • (Boolean)


9
# File 'lib/plutonium/helpers/turbo_helper.rb', line 9

def in_frame? = current_turbo_frame.present?

#in_modal?Boolean

True when the request is rendered inside either modal frame (primary or secondary).

Returns:

  • (Boolean)


13
# File 'lib/plutonium/helpers/turbo_helper.rb', line 13

def in_modal? = Plutonium::MODAL_FRAMES.include?(current_turbo_frame)

#in_secondary_modal?Boolean

True when the request is rendered inside the secondary (stacked) modal frame specifically.

Returns:

  • (Boolean)


17
# File 'lib/plutonium/helpers/turbo_helper.rb', line 17

def in_secondary_modal? = current_turbo_frame == Plutonium::REMOTE_MODAL_SECONDARY_FRAME

#remote_modal_frame_tagObject



19
20
21
# File 'lib/plutonium/helpers/turbo_helper.rb', line 19

def remote_modal_frame_tag(&)
  turbo_frame_tag(Plutonium::REMOTE_MODAL_FRAME, &)
end

#turbo_scoped_dom_id(base) ⇒ String

Returns a turbo-frame-scoped element id. Two identically-named forms can be on the page simultaneously (e.g. a primary modal opens a secondary modal, each rendering an ‘id=“resource-form”`). When the server later replies with `turbo_stream.replace(“resource-form”, …)`, Turbo would pick the FIRST element matching the id — which is rarely the one the user actually submitted. Append a frame suffix so each frame’s form has a unique id and the controller can target precisely.

Parameters:

  • base (String, Symbol)

    the base id

Returns:

  • (String)

    the scoped id (no suffix outside any modal frame)



33
34
35
36
37
38
39
40
# File 'lib/plutonium/helpers/turbo_helper.rb', line 33

def turbo_scoped_dom_id(base)
  base = base.to_s
  case current_turbo_frame
  when Plutonium::REMOTE_MODAL_FRAME then "#{base}-primary"
  when Plutonium::REMOTE_MODAL_SECONDARY_FRAME then "#{base}-secondary"
  else base
  end
end