2017-07-16 13:10:15 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-12-15 15:38:58 -05:00
|
|
|
module Rails
|
|
|
|
module Generators
|
|
|
|
class ChannelGenerator < NamedBase
|
2017-05-15 10:17:28 -04:00
|
|
|
source_root File.expand_path("templates", __dir__)
|
2015-12-15 15:38:58 -05:00
|
|
|
|
|
|
|
argument :actions, type: :array, default: [], banner: "method method"
|
|
|
|
|
2015-12-21 15:58:08 -05:00
|
|
|
class_option :assets, type: :boolean
|
|
|
|
|
2015-12-15 15:38:58 -05:00
|
|
|
check_class_collision suffix: "Channel"
|
|
|
|
|
2019-01-13 21:54:31 -05:00
|
|
|
hook_for :test_framework
|
|
|
|
|
2021-09-04 05:53:57 -04:00
|
|
|
def create_channel_files
|
|
|
|
create_shared_channel_files
|
|
|
|
create_channel_file
|
2016-05-08 10:22:43 -04:00
|
|
|
|
2021-09-04 05:53:57 -04:00
|
|
|
if using_javascript?
|
|
|
|
if first_setup_required?
|
|
|
|
create_shared_channel_javascript_files
|
|
|
|
import_channels_in_javascript_entrypoint
|
2021-08-26 04:39:36 -04:00
|
|
|
|
2021-09-04 05:53:57 -04:00
|
|
|
if using_importmap?
|
|
|
|
pin_javascript_dependencies
|
|
|
|
elsif using_node?
|
|
|
|
install_javascript_dependencies
|
|
|
|
end
|
2021-08-26 04:39:36 -04:00
|
|
|
end
|
2016-02-10 17:36:11 -05:00
|
|
|
|
2021-09-04 05:53:57 -04:00
|
|
|
create_channel_javascript_file
|
|
|
|
import_channel_in_javascript_entrypoint
|
|
|
|
end
|
2015-12-15 15:38:58 -05:00
|
|
|
end
|
|
|
|
|
2016-12-22 05:16:38 -05:00
|
|
|
private
|
2021-09-04 05:53:57 -04:00
|
|
|
def create_shared_channel_files
|
|
|
|
return if behavior != :invoke
|
|
|
|
|
|
|
|
copy_file "#{__dir__}/templates/application_cable/channel.rb",
|
|
|
|
"app/channels/application_cable/channel.rb"
|
|
|
|
copy_file "#{__dir__}/templates/application_cable/connection.rb",
|
|
|
|
"app/channels/application_cable/connection.rb"
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_channel_file
|
|
|
|
template "channel.rb",
|
|
|
|
File.join("app/channels", class_path, "#{file_name}_channel.rb")
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_shared_channel_javascript_files
|
|
|
|
template "javascript/index.js", "app/javascript/channels/index.js"
|
|
|
|
template "javascript/consumer.js", "app/javascript/channels/consumer.js"
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_channel_javascript_file
|
|
|
|
channel_js_path = File.join("app/javascript/channels", class_path, "#{file_name}_channel")
|
|
|
|
js_template "javascript/channel", channel_js_path
|
|
|
|
gsub_file "#{channel_js_path}.js", /\.\/consumer/, "channels/consumer" unless using_node?
|
|
|
|
end
|
|
|
|
|
|
|
|
def import_channels_in_javascript_entrypoint
|
2021-09-04 13:47:32 -04:00
|
|
|
append_to_file "app/javascript/application.js",
|
2021-09-04 05:53:57 -04:00
|
|
|
using_node? ? %(import "./channels"\n) : %(import "channels"\n)
|
|
|
|
end
|
|
|
|
|
|
|
|
def import_channel_in_javascript_entrypoint
|
|
|
|
append_to_file "app/javascript/channels/index.js",
|
2021-09-04 14:44:11 -04:00
|
|
|
using_node? ? %(import "./#{file_name}_channel"\n) : %(import "channels/#{file_name}_channel"\n)
|
2021-09-04 05:53:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def install_javascript_dependencies
|
|
|
|
say "Installing JavaScript dependencies", :green
|
|
|
|
run "yarn add @rails/actioncable"
|
|
|
|
end
|
|
|
|
|
|
|
|
def pin_javascript_dependencies
|
|
|
|
append_to_file "config/importmap.rb", <<-RUBY
|
|
|
|
pin "@rails/actioncable", to: "actioncable.esm.js"
|
|
|
|
pin_all_from "app/javascript/channels", under: "channels"
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2015-12-15 15:38:58 -05:00
|
|
|
def file_name
|
2018-04-22 01:30:07 -04:00
|
|
|
@_file_name ||= super.sub(/_channel\z/i, "")
|
2015-12-15 15:38:58 -05:00
|
|
|
end
|
2016-02-10 17:36:11 -05:00
|
|
|
|
2021-09-04 05:53:57 -04:00
|
|
|
def first_setup_required?
|
|
|
|
!root.join("app/javascript/channels/index.js").exist?
|
|
|
|
end
|
2016-02-10 17:36:11 -05:00
|
|
|
|
2021-09-04 05:53:57 -04:00
|
|
|
def using_javascript?
|
|
|
|
@using_javascript ||= options[:assets] && root.join("app/javascript").exist?
|
|
|
|
end
|
2016-02-10 17:36:11 -05:00
|
|
|
|
2021-09-04 05:53:57 -04:00
|
|
|
def using_node?
|
|
|
|
@using_node ||= root.join("package.json").exist?
|
|
|
|
end
|
|
|
|
|
|
|
|
def using_importmap?
|
|
|
|
@using_importmap ||= root.join("config/importmap.rb").exist?
|
|
|
|
end
|
|
|
|
|
|
|
|
def root
|
|
|
|
@root ||= Pathname(destination_root)
|
2016-02-10 17:36:11 -05:00
|
|
|
end
|
2015-12-15 15:38:58 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|