Class: JsxRosetta::Backend::RailsView

Inherits:
ViewComponent show all
Defined in:
lib/jsx_rosetta/backend/rails_view.rb

Overview

Emits a translated component as a plain Rails view template (‘<snake>.html.erb`) instead of a ViewComponent class + sidecar template. Pages tied to routes are conceptually Rails views, not reusable components — the controller sets `@instance_variables` and the template uses them directly.

Reuses the ViewComponent backend’s IR-rendering pipeline; only the output shape differs (no Ruby class, no sidecar directory). When the source JSX includes inline event handlers, a Stimulus controller is still emitted as a sibling file alongside the .html.erb.

Constant Summary

Constants inherited from ViewComponent

ViewComponent::DEFAULT_HELPERS, ViewComponent::DEFAULT_SLOT_NAME, ViewComponent::VOID_ELEMENTS

Instance Method Summary collapse

Methods inherited from ViewComponent

#erb_path, #initialize, #render_stimulus_controller_js, #stimulus_identifier, #stimulus_method_lines, #stimulus_path

Constructor Details

This class inherits a constructor from JsxRosetta::Backend::ViewComponent

Instance Method Details

#emit(component) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/jsx_rosetta/backend/rails_view.rb', line 18

def emit(component)
  prop_names = component.props.map(&:name)
  prop_names << component.rest_prop_name if component.rest_prop_name
  translator = ExpressionTranslator.new(prop_names: prop_names)

  @stimulus_identifier = component.stimulus_methods.any? ? stimulus_identifier(component) : nil

  files = [
    File.new(
      path: "#{AST::Inflector.underscore(component.name)}.html.erb",
      contents: render_erb_template(component, translator)
    )
  ]
  if component.stimulus_methods.any?
    files << File.new(
      path: "#{AST::Inflector.underscore(component.name)}_controller.js",
      contents: render_stimulus_controller_js(component)
    )
  end
  files
end