Class: Phlex::Reactive::Generators::InstallGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/phlex/reactive/install/install_generator.rb

Overview

rails g phlex:reactive:install

One-command setup for phlex-reactive:

* registers the generic `reactive` Stimulus controller (eagerly)
* writes a config initializer with the common options commented out

The action endpoint and client auto-pin are provided by the engine, so this generator only wires the bits that live in the host app.

Constant Summary collapse

IMPORT_LINE =
'import ReactiveController from "phlex/reactive/reactive_controller"'
REGISTER_LINE =
'application.register("reactive", ReactiveController)'

Instance Method Summary collapse

Instance Method Details

#create_initializerObject



22
23
24
# File 'lib/generators/phlex/reactive/install/install_generator.rb', line 22

def create_initializer
  template "phlex_reactive.rb.erb", "config/initializers/phlex_reactive.rb"
end

#register_stimulus_controllerObject

Register the controller eagerly in the Stimulus entrypoint. Eager (not lazy) so a click immediately after page load is never missed.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/generators/phlex/reactive/install/install_generator.rb', line 28

def register_stimulus_controller
  index = stimulus_index_path

  unless index
    say_status :skip, "no Stimulus controllers entrypoint found — register manually:\n    " \
                      "#{IMPORT_LINE}\n    #{REGISTER_LINE}", :yellow
    return
  end

  if File.read(index).include?(REGISTER_LINE)
    say_status :identical, relative(index), :blue
    return
  end

  inject_into_file index, after: %r{import \{ application \} from ["']controllers/application["']\n} do
    "#{IMPORT_LINE}\n#{REGISTER_LINE}\n"
  end

  # Fallback: if the standard import line wasn't found, append both lines.
  return if File.read(index).include?(REGISTER_LINE)

  append_to_file index, "\n#{IMPORT_LINE}\n#{REGISTER_LINE}\n"
end

#show_post_installObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/generators/phlex/reactive/install/install_generator.rb', line 52

def show_post_install
  say_status :info, "phlex-reactive installed. Include the mixin in a component:", :green
  say <<~MSG

    class Counter < ApplicationComponent
      include Phlex::Reactive::Component  # pulls in Streamable too
      # ... reactive_state / reactive_record, action :name, on(:name) ...
    end

    Scaffold one with:  rails g phlex:reactive:component Counter

    Then verify the whole install:  bin/rails phlex_reactive:doctor

    Debugging with Claude Code? Install the toolkit (skill + MCP server):
      rails g phlex:reactive:claude
  MSG
end