Skip to content

Commit 83921b8

Browse files
committed
Generate ApplicationCable files if they do not already exist
1 parent 1813b29 commit 83921b8

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

actioncable/lib/rails/generators/channel/channel_generator.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,28 @@ def create_channel_file
1515
if options[:assets]
1616
template "assets/channel.coffee", File.join('app/assets/javascripts/channels', class_path, "#{file_name}.coffee")
1717
end
18+
19+
generate_application_cable_files
1820
end
1921

2022
protected
2123
def file_name
2224
@_file_name ||= super.gsub(/\_channel/i, '')
2325
end
26+
27+
def generate_application_cable_files
28+
return if self.behavior != :invoke
29+
30+
files = [
31+
'application_cable/channel.rb',
32+
'application_cable/connection.rb'
33+
]
34+
35+
files.each do |name|
36+
path = File.join('app/channels/', name)
37+
template(name, path) if !File.exist?(path)
38+
end
39+
end
2440
end
2541
end
2642
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Be sure to restart your server when you modify this file. Action Cable runs in a loop that does not support auto reloading.
2+
module ApplicationCable
3+
class Channel < ActionCable::Channel::Base
4+
end
5+
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Be sure to restart your server when you modify this file. Action Cable runs in a loop that does not support auto reloading.
2+
module ApplicationCable
3+
class Connection < ActionCable::Connection::Base
4+
end
5+
end

railties/test/generators/channel_generator_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@ class ChannelGeneratorTest < Rails::Generators::TestCase
55
include GeneratorsTestHelper
66
tests Rails::Generators::ChannelGenerator
77

8+
def test_application_cable_skeleton_is_created
9+
run_generator ['books']
10+
11+
assert_file "app/channels/application_cable/channel.rb" do |cable|
12+
assert_match(/module ApplicationCable\n class Channel < ActionCable::Channel::Base\n/, cable)
13+
end
14+
15+
assert_file "app/channels/application_cable/connection.rb" do |cable|
16+
assert_match(/module ApplicationCable\n class Connection < ActionCable::Connection::Base\n/, cable)
17+
end
18+
end
19+
820
def test_channel_is_created
921
run_generator ['chat']
1022

0 commit comments

Comments
 (0)