Class: Hibiki::Rails::Generators::StimulusGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Includes:
GeneratorHelpers
Defined in:
lib/generators/hibiki/rails/stimulus/stimulus_generator.rb

Overview

The ChannelController shape — stock Stimulus vocabulary (data-controller / data-action) over the Turbo-broadcast transport. Emits a working mini-example (one state, one derived, one action, one broadcasting effect) meant to be reshaped in place: a channel, a ChannelController subclass, and a self-contained view partial. Importmap apps need no wiring — the controller eager-loads from the controllers directory; jsbundling apps get the import/register pair appended to controllers/index.js (stimulus:manifest:update format).

Constant Summary

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_channelObject



28
29
30
# File 'lib/generators/hibiki/rails/stimulus/stimulus_generator.rb', line 28

def create_channel
  template "channel.rb.tt", "app/channels/#{file_path}_channel.rb"
end

#create_controllerObject



32
33
34
# File 'lib/generators/hibiki/rails/stimulus/stimulus_generator.rb', line 32

def create_controller
  template "controller.js.tt", "app/javascript/controllers/#{file_path}_controller.js"
end

#create_viewsObject



47
48
49
50
# File 'lib/generators/hibiki/rails/stimulus/stimulus_generator.rb', line 47

def create_views
  template "island.html.erb.tt", "app/views/#{view_dir}/_#{file_name}.html.erb"
  template "display.html.erb.tt", "app/views/#{view_dir}/_#{file_name}_display.html.erb"
end

#post_installObject



52
53
54
55
56
57
58
59
60
# File 'lib/generators/hibiki/rails/stimulus/stimulus_generator.rb', line 52

def post_install
  say <<~MSG

    Render it from any page:

      <%= render "#{view_dir}/#{file_name}" %>

  MSG
end

#register_controllerObject

Without an importmap there is no eager loader, so the manifest-style index.js must name every controller. Mirror the exact lines stimulus:manifest:update would emit for this file.



39
40
41
42
43
44
45
# File 'lib/generators/hibiki/rails/stimulus/stimulus_generator.rb', line 39

def register_controller
  return if importmap?
  return say_status :identical, INDEX_JS, :blue if wired?(INDEX_JS, %(register("#{identifier}")))
  return manual_wiring(INDEX_JS, registration) unless exists?(INDEX_JS)

  append_to_file INDEX_JS, "\n#{registration}"
end