aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--actioncable/README.md10
-rw-r--r--actioncable/lib/rails/generators/channel/channel_generator.rb4
-rw-r--r--railties/test/generators/channel_generator_test.rb30
3 files changed, 38 insertions, 6 deletions
diff --git a/actioncable/README.md b/actioncable/README.md
index 32f49c215f..6dbc1bfe03 100644
--- a/actioncable/README.md
+++ b/actioncable/README.md
@@ -1,4 +1,4 @@
-# Action Cable –- Integrated WebSockets for Rails
+# Action Cable – Integrated WebSockets for Rails
Action Cable seamlessly integrates WebSockets with the rest of your Rails application.
It allows for real-time features to be written in Ruby in the same style
@@ -364,7 +364,7 @@ Then add the following line to your layout before your JavaScript tag:
And finally, create your consumer like so:
```coffeescript
-App.cable = Cable.createConsumer()
+App.cable = ActionCable.createConsumer()
```
For a full list of all configuration options, see the `ActionCable::Server::Configuration` class.
@@ -399,16 +399,16 @@ The above will start a cable server on port 28080. Remember to point your client
### In app
-If you are using a threaded server like Puma or Thin, the current implementation of ActionCable can run side-along with your Rails application. For example, to listen for WebSocket requests on `/websocket`, match requests on that path:
+If you are using a threaded server like Puma or Thin, the current implementation of ActionCable can run side-along with your Rails application. For example, to listen for WebSocket requests on `/cable`, match requests on that path:
```ruby
# config/routes.rb
Example::Application.routes.draw do
- match "/websocket", :to => ActionCable.server, via: [:get, :post]
+ match "/cable", :to => ActionCable.server, via: [:get, :post]
end
```
-You can use `App.cable = Cable.createConsumer("/websocket")` to connect to the cable server.
+You can use `App.cable = ActionCable.createConsumer()` to connect to the cable server if `action_cable_meta_tag` is included in the layout. A custom path is specified as first argument to `createConsumer` (e.g. `App.cable = ActionCable.createConsumer("/websocket")`).
For every instance of your server you create and for every worker your server spawns, you will also have a new instance of ActionCable, but the use of Redis keeps messages synced across connections.
diff --git a/actioncable/lib/rails/generators/channel/channel_generator.rb b/actioncable/lib/rails/generators/channel/channel_generator.rb
index 2f37d8055b..925dd3d098 100644
--- a/actioncable/lib/rails/generators/channel/channel_generator.rb
+++ b/actioncable/lib/rails/generators/channel/channel_generator.rb
@@ -9,7 +9,9 @@ module Rails
def create_channel_file
template "channel.rb", File.join('app/channels', class_path, "#{file_name}_channel.rb")
- template "assets/channel.coffee", File.join('app/assets/javascripts/channels', class_path, "#{file_name}.coffee")
+ if Rails::Generators.options[:rails][:assets]
+ template "assets/channel.coffee", File.join('app/assets/javascripts/channels', class_path, "#{file_name}.coffee")
+ end
end
protected
diff --git a/railties/test/generators/channel_generator_test.rb b/railties/test/generators/channel_generator_test.rb
new file mode 100644
index 0000000000..5cc1cd83b1
--- /dev/null
+++ b/railties/test/generators/channel_generator_test.rb
@@ -0,0 +1,30 @@
+require 'generators/generators_test_helper'
+require 'rails/generators/channel/channel_generator'
+
+class ChannelGeneratorTest < Rails::Generators::TestCase
+ include GeneratorsTestHelper
+ tests Rails::Generators::ChannelGenerator
+
+ def test_channel_is_created
+ run_generator ['chat']
+
+ assert_file "app/channels/chat_channel.rb" do |channel|
+ assert_match(/class ChatChannel < ApplicationCable::Channel/, channel)
+ end
+
+ assert_file "app/assets/javascripts/channels/chat.coffee" do |channel|
+ assert_match(/App.cable.subscriptions.create "ChatChannel"/, channel)
+ end
+ end
+
+ def test_channel_asset_is_not_created
+ Rails::Generators.options[:rails][:assets] = false
+ run_generator ['chat']
+
+ assert_file "app/channels/chat_channel.rb" do |channel|
+ assert_match(/class ChatChannel < ApplicationCable::Channel/, channel)
+ end
+
+ assert_no_file "app/assets/javascripts/channels/chat.coffee"
+ end
+end