Class: Ui::ComponentGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Defined in:
lib/generators/ui/component/component_generator.rb

Instance Method Summary collapse

Instance Method Details

#add_import_to_turbo_mountObject



13
14
15
16
17
18
19
20
21
# File 'lib/generators/ui/component/component_generator.rb', line 13

def add_import_to_turbo_mount
  # Inject an import statement into turbo-mount.js after the registerComponent import
  inject_into_file(
    "app/javascript/entrypoints/turbo-mount.js",
    after: 'import { registerComponent } from "turbo-mount/react";'
  ) do
    "\nimport { #{class_name} } from \"@/components/#{class_name}\";"
  end
end

#create_component_fileObject



8
9
10
11
# File 'lib/generators/ui/component/component_generator.rb', line 8

def create_component_file
  # Create a new TSX file in app/javascript/components
  template "component.tsx.erb", "app/javascript/components/#{class_name}.tsx"
end

#register_component_in_turbo_mountObject



23
24
25
26
27
28
29
# File 'lib/generators/ui/component/component_generator.rb', line 23

def register_component_in_turbo_mount
  # Append a registerComponent call at the bottom of turbo-mount.js
  append_to_file "app/javascript/entrypoints/turbo-mount.js", "\n    registerComponent(turboMount, \"\#{class_name}\", \#{class_name});\n  JS\nend\n"