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"
|
|
|
|
|
|
|
|
def create_channel_file
|
2016-08-06 13:13:46 -04:00
|
|
|
template "channel.rb", File.join("app/channels", class_path, "#{file_name}_channel.rb")
|
2015-12-21 15:58:08 -05:00
|
|
|
|
|
|
|
if options[:assets]
|
2017-01-05 03:20:57 -05:00
|
|
|
if behavior == :invoke
|
2016-04-16 02:19:41 -04:00
|
|
|
template "assets/cable.js", "app/assets/javascripts/cable.js"
|
|
|
|
end
|
2016-05-08 10:22:43 -04:00
|
|
|
|
2016-08-06 13:13:46 -04:00
|
|
|
js_template "assets/channel", File.join("app/assets/javascripts/channels", class_path, "#{file_name}")
|
2015-12-18 23:43:11 -05:00
|
|
|
end
|
2016-02-10 17:36:11 -05:00
|
|
|
|
|
|
|
generate_application_cable_files
|
2015-12-15 15:38:58 -05:00
|
|
|
end
|
|
|
|
|
2016-12-22 05:16:38 -05:00
|
|
|
private
|
2015-12-15 15:38:58 -05:00
|
|
|
def file_name
|
2016-08-06 13:13:46 -04:00
|
|
|
@_file_name ||= super.gsub(/_channel/i, "")
|
2015-12-15 15:38:58 -05:00
|
|
|
end
|
2016-02-10 17:36:11 -05:00
|
|
|
|
2016-02-10 18:10:02 -05:00
|
|
|
# FIXME: Change these files to symlinks once RubyGems 2.5.0 is required.
|
2016-02-10 17:36:11 -05:00
|
|
|
def generate_application_cable_files
|
2017-01-05 03:20:57 -05:00
|
|
|
return if behavior != :invoke
|
2016-02-10 17:36:11 -05:00
|
|
|
|
|
|
|
files = [
|
2016-08-06 13:13:46 -04:00
|
|
|
"application_cable/channel.rb",
|
|
|
|
"application_cable/connection.rb"
|
2016-02-10 17:36:11 -05:00
|
|
|
]
|
|
|
|
|
|
|
|
files.each do |name|
|
2016-08-06 13:13:46 -04:00
|
|
|
path = File.join("app/channels/", name)
|
2016-02-10 17:36:11 -05:00
|
|
|
template(name, path) if !File.exist?(path)
|
|
|
|
end
|
|
|
|
end
|
2015-12-15 15:38:58 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|