Class: Hibiki::Rails::Generators::InstallGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
GeneratorHelpers
Defined in:
lib/generators/hibiki/rails/install/install_generator.rb

Overview

Wiring only — the client itself stays vendored in the engine (the data-hibiki-* attribute names are a private Ruby↔JS contract that versions inside this gem, so the client is never copied into the app; the shim it creates only re-exports it). Every action is idempotent by content check, so rerunning is safe.

Constant Summary collapse

REGISTER =

Exactly the lines stimulus:manifest:update emits for the shim, so a later manifest run converges instead of duplicating.

<<~JS

  import HibikiController from "./hibiki_controller"
  application.register("hibiki", HibikiController)
JS
INCLUDE_LINE =
"  include Hibiki::Rails::Helpers\n"
APPLICATION_CABLE =

Stock Rails apps don't have these until the first rails g channel — the hibiki:rails:* generators write their channels directly, so install supplies the boilerplate they inherit.

{
  "channel.rb.tt" => "app/channels/application_cable/channel.rb",
  "connection.rb.tt" => "app/channels/application_cable/connection.rb"
}.freeze
PIN =

Same event: the stock importmap has no @rails/actioncable pin until the first rails g channel adds it. The packaged client imports it, so pin it here (the asset ships in actioncable).

<<~RUBY

  pin "@rails/actioncable", to: "actioncable.esm.js"
RUBY

Constants included from GeneratorHelpers

GeneratorHelpers::APPLICATION_HELPER, GeneratorHelpers::IMPORTMAP, GeneratorHelpers::INDEX_JS, GeneratorHelpers::REGISTER_FRAGMENT, GeneratorHelpers::SHIM

Instance Method Summary collapse

Instance Method Details

#create_application_cableObject

Presence is enough — an app's own ApplicationCable (customized or not) is never touched.



76
77
78
79
80
81
82
# File 'lib/generators/hibiki/rails/install/install_generator.rb', line 76

def create_application_cable
  APPLICATION_CABLE.each do |source, destination|
    next say_status :exist, destination, :blue if exists?(destination)

    template source, destination
  end
end

#create_shim_controllerObject

The registration lives in a file-backed shim so that stimulus:manifest:update (which rewrites index.js wholesale from the *_controller.js files) re-derives it instead of dropping it.



52
53
54
# File 'lib/generators/hibiki/rails/install/install_generator.rb', line 52

def create_shim_controller
  template "hibiki_controller.js.tt", SHIM
end

#include_helpersObject



66
67
68
69
70
71
72
# File 'lib/generators/hibiki/rails/install/install_generator.rb', line 66

def include_helpers
  return say_status :identical, APPLICATION_HELPER, :blue if helpers_included?
  return manual_wiring(APPLICATION_HELPER, INCLUDE_LINE) unless exists?(APPLICATION_HELPER)

  inject_into_file APPLICATION_HELPER, INCLUDE_LINE,
                   after: /module ApplicationHelper\s*\n/
end

#pin_actioncableObject



84
85
86
87
88
89
# File 'lib/generators/hibiki/rails/install/install_generator.rb', line 84

def pin_actioncable
  return bundler_note unless importmap?
  return say_status :identical, IMPORTMAP, :blue if wired?(IMPORTMAP, "@rails/actioncable")

  append_to_file IMPORTMAP, PIN
end

#register_controllerObject

Importmap apps eager-load the shim from the controllers directory; a manifest-style index.js (jsbundling) must name it explicitly.



58
59
60
61
62
63
64
# File 'lib/generators/hibiki/rails/install/install_generator.rb', line 58

def register_controller
  return legacy_registration_hint if importmap?
  return say_status :identical, INDEX_JS, :blue if wired?(INDEX_JS, REGISTER_FRAGMENT)
  return manual_wiring(INDEX_JS, REGISTER) unless exists?(INDEX_JS)

  append_to_file INDEX_JS, REGISTER
end