Module: Ruact::ViewHelper

Included in:
Controller
Defined in:
lib/ruact/view_helper.rb

Overview

ActionView helper module included in ActionView::Base via Railtie. Provides the __ruact_component__ method that ERB templates call after the preprocessor transforms PascalCase tags into <%= __ruact_component__(...) %>.

Thread-safe: ActionView creates a fresh view context per request, so the render context (set by Ruact::Controller#ruact_render on the controller as @ruact_render_context and copied to the view by Rails's view_assigns plumbing — see Story 7.9 / Bug 7.8-B) is per-request — no shared state.

Instance Method Summary collapse

Instance Method Details

#__ruact_component__(name, props = {}) ⇒ Object

Registers name with props in the per-render RenderContext (set by Ruact::Controller#ruact_render on the controller as @ruact_render_context; Rails copies it to the view via _assigns_for_view_context because the name does not match +DEFAULT_PROTECTED_INSTANCE_VARIABLES+'s /\A@_/ filter) and returns an HTML comment placeholder that HtmlConverter later replaces with a ReactElement node.

The returned string MUST be html_safe so ActionView does not escape the angle brackets — if it were escaped, HtmlConverter would not find the placeholder in the HTML output.

Raises:



26
27
28
29
30
31
32
# File 'lib/ruact/view_helper.rb', line 26

def __ruact_component__(name, props = {})
  ctx = @ruact_render_context
  raise Ruact::Error, "ruact: __ruact_component__ called outside a ruact_render flow" if ctx.nil?

  token = ctx.register(name, props)
  "<!-- #{token} -->".html_safe
end

#ruact_js_assets(flight_payload = nil) ⇒ ActiveSupport::SafeBuffer

Story 14.2 (FR104) — emits ruact's full JavaScript asset block: the dev/prod bootstrap entry <script> tags (re-targeting the virtual entry virtual:ruact/bootstrap — in dev the react-refresh preamble + @vite/client

  • the bootstrap module; in prod the hashed URL read from the Vite manifest) AND, when a Flight payload is given, the __FLIGHT_DATA inline bootstrap <script> the entry reads on boot.

This is the SINGLE implementation of the JS asset markup — Ruact::Controller#ruact_html_shell delegates to it so the controller's generated shell and any view that calls the helper emit byte-identical tags (no drift). Available in every view via the railtie's ActionView::Base.include(Ruact::ViewHelper).

Examples:

In a layout

<%= ruact_js_assets %>

Parameters:

  • flight_payload (String, nil) (defaults to: nil)

    the per-render Flight wire payload to inline as __FLIGHT_DATA; omit (or pass nil) to emit only the entry tags.

Returns:

  • (ActiveSupport::SafeBuffer)

    the asset markup, html_safe



52
53
54
55
56
57
# File 'lib/ruact/view_helper.rb', line 52

def ruact_js_assets(flight_payload = nil)
  parts = []
  parts << ruact_flight_data_script(flight_payload) unless flight_payload.nil?
  parts << ruact_vite_tags
  parts.join("\n").html_safe
end