Class: PolylingoChat::Generators::InstallGenerator

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.next_migration_number(dirname) ⇒ Object



13
14
15
16
# File 'lib/generators/polylingo_chat/install/install_generator.rb', line 13

def self.next_migration_number(dirname)
  next_migration_number = current_migration_number(dirname) + 1
  ActiveRecord::Migration.next_migration_number(next_migration_number)
end

Instance Method Details

#add_routesObject



63
64
65
66
67
68
69
70
# File 'lib/generators/polylingo_chat/install/install_generator.rb', line 63

def add_routes
  route 'mount ActionCable.server => "/cable"'
  route "    resources :conversations do\n      resources :messages, only: [:create]\n    end\n  RUBY\nend\n"

#configure_cableObject



44
45
46
47
48
49
50
# File 'lib/generators/polylingo_chat/install/install_generator.rb', line 44

def configure_cable
  if File.exist?("config/cable.yml")
    gsub_file "config/cable.yml", /^development:\s*\n\s+adapter:\s+\w+.*$/m do |match|
      "development:\n  adapter: solid_cable\n  polling_interval: 0.1.seconds\n  message_retention: 1.day"
    end
  end
end

#copy_migrationsObject



18
19
20
21
22
# File 'lib/generators/polylingo_chat/install/install_generator.rb', line 18

def copy_migrations
  migration_template "create_conversations.rb", "db/migrate/create_conversations.rb"
  migration_template "create_participants.rb", "db/migrate/create_participants.rb"
  migration_template "create_messages.rb", "db/migrate/create_messages.rb"
end

#create_channelsObject



30
31
32
33
34
35
# File 'lib/generators/polylingo_chat/install/install_generator.rb', line 30

def create_channels
  empty_directory "app/channels/application_cable"
  template "channels/application_cable/channel.rb", "app/channels/application_cable/channel.rb"
  template "channels/application_cable/connection.rb", "app/channels/application_cable/connection.rb"
  template "channels/polylingo_chat_channel.rb", "app/channels/polylingo_chat_channel.rb"
end

#create_initializerObject



98
99
100
# File 'lib/generators/polylingo_chat/install/install_generator.rb', line 98

def create_initializer
  template "initializer.rb", "config/initializers/polylingo_chat.rb"
end

#create_javascript_filesObject



37
38
39
40
41
42
# File 'lib/generators/polylingo_chat/install/install_generator.rb', line 37

def create_javascript_files
  empty_directory "app/javascript/channels"
  template "javascript/chat.js", "app/javascript/chat.js"
  template "javascript/channels/consumer.js", "app/javascript/channels/consumer.js"
  template "javascript/channels/index.js", "app/javascript/channels/index.js"
end

#create_modelsObject



24
25
26
27
28
# File 'lib/generators/polylingo_chat/install/install_generator.rb', line 24

def create_models
  template "models/conversation.rb", "app/models/conversation.rb"
  template "models/participant.rb", "app/models/participant.rb"
  template "models/message.rb", "app/models/message.rb"
end

#download_actioncableObject



57
58
59
60
61
# File 'lib/generators/polylingo_chat/install/install_generator.rb', line 57

def download_actioncable
  empty_directory "vendor/javascript"
  say "Downloading ActionCable ESM module...", :green
  run "curl -o vendor/javascript/@rails--actioncable.js https://ga.jspm.io/npm:@rails/[email protected]/app/assets/javascripts/actioncable.esm.js"
end

#setup_solid_cableObject



52
53
54
55
# File 'lib/generators/polylingo_chat/install/install_generator.rb', line 52

def setup_solid_cable
  say "Setting up Solid Cable...", :green
  rails_command "solid_cable:install", abort_on_failure: false
end

#show_readmeObject



102
103
104
# File 'lib/generators/polylingo_chat/install/install_generator.rb', line 102

def show_readme
  readme "README" if behavior == :invoke
end

#update_application_jsObject



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/generators/polylingo_chat/install/install_generator.rb', line 86

def update_application_js
  if File.exist?("app/javascript/application.js")
    append_to_file "app/javascript/application.js" do
      "\n        // PolylingoChat real-time chat\n        import \"chat\"\n      JS\n    end\n  end\nend\n"

#update_importmapObject



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/generators/polylingo_chat/install/install_generator.rb', line 72

def update_importmap
  if File.exist?("config/importmap.rb")
    append_to_file "config/importmap.rb" do
      "\n        # PolylingoChat real-time chat\n        pin \"@rails/actioncable\", to: \"@rails--actioncable.js\"\n        pin_all_from \"app/javascript/channels\", under: \"channels\"\n        pin \"chat\", to: \"chat.js\"\n      RUBY\n    end\n  end\nend\n"