Class: LexxyRealtime::Generators::InstallGenerator

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

Overview

Installs the document channel, the storage migration (via yrby's generator), and the Action Cable boilerplate when missing.

Instance Method Summary collapse

Instance Method Details

#add_importmap_pinsObject

Import-map apps get pins to the assets this gem ships. The Lexxy pin must point at this gem's build (Lexxy's own asset bundles a second copy of lexical, which breaks collaboration), so an existing @37signals/lexxy pin is left for the app to resolve.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/generators/lexxy_realtime/install/install_generator.rb', line 46

def add_importmap_pins
  return unless File.exist?(File.join(destination_root, "config/importmap.rb"))
  return if File.read(File.join(destination_root, "config/importmap.rb")).include?("lexxy_realtime/")

  append_to_file "config/importmap.rb", <<~RUBY

    # lexxy-realtime. lexical is shared between the Lexxy and
    # lexxy-realtime bundles; @37signals/lexxy must point at the
    # lexxy_realtime build, not Lexxy's own asset.
    pin "lexical", to: "lexxy_realtime/lexical.js"
    pin "@37signals/lexxy", to: "lexxy_realtime/lexxy.js"
    pin "lexxy-realtime", to: "lexxy_realtime/lexxy-realtime.js"
    pin "@rails/activestorage", to: "activestorage.esm.js"
  RUBY
end

#check_action_cableObject

rails new --skip-action-cable leaves nothing for the channel to inherit from.

Raises:

  • (Thor::Error)


15
16
17
18
19
20
21
22
# File 'lib/generators/lexxy_realtime/install/install_generator.rb', line 15

def check_action_cable
  return if defined?(ActionCable)

  say "Action Cable is not loaded (rails new --skip-action-cable?). " \
      'Add `require "action_cable/engine"` to config/application.rb, ' \
      "create config/cable.yml, and re-run this generator.", :red
  raise Thor::Error, "lexxy_realtime:install requires Action Cable"
end

#create_application_cableObject



24
25
26
27
28
29
30
31
# File 'lib/generators/lexxy_realtime/install/install_generator.rb', line 24

def create_application_cable
  %w[connection channel].each do |file|
    destination = "app/channels/application_cable/#{file}.rb"
    next if File.exist?(File.join(destination_root, destination))

    template "application_cable_#{file}.rb", destination
  end
end

#create_channelObject



33
34
35
# File 'lib/generators/lexxy_realtime/install/install_generator.rb', line 33

def create_channel
  template "document_channel.rb", "app/channels/document_channel.rb"
end

#create_tablesObject

yrby owns the models and their migration.



38
39
40
# File 'lib/generators/lexxy_realtime/install/install_generator.rb', line 38

def create_tables
  invoke "yrby:tables"
end

#show_next_stepsObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/generators/lexxy_realtime/install/install_generator.rb', line 62

def show_next_steps
  say <<~NEXT

    lexxy-realtime is installed. Lexxy itself (the gem and its editor
    JS) must already be installed and working. Next steps:

      1. bin/rails db:migrate
      2. Wire up the JavaScript. With import maps, the generator
         added pins; import "@37signals/lexxy" and "lexxy-realtime"
         from your entrypoint, and remove any pin for Lexxy's own
         asset. With a bundler, install the lexxy-realtime npm
         package and import it.
      3. Declare `has_collaborative_rich_text :body` on a model and
         render it with `<%= form.collaborative_rich_textarea :body %>`.
      4. Update `authorized?` in app/channels/document_channel.rb
         to check the current user.

    Optional: set cursor names with `LexxyRealtime.identity`.
  NEXT
end