aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--actioncable/lib/rails/generators/channel/USAGE14
-rw-r--r--actioncable/lib/rails/generators/channel/channel_generator.rb27
-rw-r--r--actioncable/lib/rails/generators/channel/templates/application_cable/channel.rb5
-rw-r--r--actioncable/lib/rails/generators/channel/templates/application_cable/connection.rb22
-rw-r--r--actioncable/lib/rails/generators/channel/templates/assets/channel.coffee11
-rw-r--r--actioncable/lib/rails/generators/channel/templates/assets/consumer.coffee6
-rw-r--r--actioncable/lib/rails/generators/channel/templates/channel.rb16
7 files changed, 101 insertions, 0 deletions
diff --git a/actioncable/lib/rails/generators/channel/USAGE b/actioncable/lib/rails/generators/channel/USAGE
new file mode 100644
index 0000000000..c061196d43
--- /dev/null
+++ b/actioncable/lib/rails/generators/channel/USAGE
@@ -0,0 +1,14 @@
+Description:
+============
+ Stubs out a new cable channel and its CoffeeScript assets. Passes the chanel name, either
+ CamelCased or under_scored, and an optional list of channel actions as arguments.
+
+ This generates a channel class in app/channels.
+
+Example:
+========
+ rails generate channel Chat speak
+
+ creates a Chat channel class and CoffeeScript asset:
+ Channel: app/channels/chat_channel.rb
+ Assets: app/assets/javascript/channels/chat.coffee
diff --git a/actioncable/lib/rails/generators/channel/channel_generator.rb b/actioncable/lib/rails/generators/channel/channel_generator.rb
new file mode 100644
index 0000000000..8f1c812c27
--- /dev/null
+++ b/actioncable/lib/rails/generators/channel/channel_generator.rb
@@ -0,0 +1,27 @@
+module Rails
+ module Generators
+ class ChannelGenerator < NamedBase
+ source_root File.expand_path("../templates", __FILE__)
+
+ argument :actions, type: :array, default: [], banner: "method method"
+
+ check_class_collision suffix: "Channel"
+
+ 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/cable/channels', class_path, "#{file_name}.coffee")
+
+ if self.behavior == :invoke
+ template "application_cable/connection.rb", 'app/channels/application_cable/connection.rb'
+ template "application_cable/channel.rb", 'app/channels/application_cable/channel.rb'
+ template "assets/consumer.coffee", 'app/assets/javascripts/cable/index.coffee'
+ end
+ end
+
+ protected
+ def file_name
+ @_file_name ||= super.gsub(/\_channel/i, '')
+ end
+ end
+ end
+end
diff --git a/actioncable/lib/rails/generators/channel/templates/application_cable/channel.rb b/actioncable/lib/rails/generators/channel/templates/application_cable/channel.rb
new file mode 100644
index 0000000000..b44bbb4aab
--- /dev/null
+++ b/actioncable/lib/rails/generators/channel/templates/application_cable/channel.rb
@@ -0,0 +1,5 @@
+module ApplicationCable
+ class Channel < ActionCable::Channel::Base
+ # Logic shared between all channels
+ end
+end \ No newline at end of file
diff --git a/actioncable/lib/rails/generators/channel/templates/application_cable/connection.rb b/actioncable/lib/rails/generators/channel/templates/application_cable/connection.rb
new file mode 100644
index 0000000000..be6fdf7ce4
--- /dev/null
+++ b/actioncable/lib/rails/generators/channel/templates/application_cable/connection.rb
@@ -0,0 +1,22 @@
+module ApplicationCable
+ class Connection < ActionCable::Connection::Base
+ identified_by :current_user
+
+ def connect
+ self.current_user = find_verified_user
+ end
+
+ protected
+ def find_verified_user
+ if current_user = authenticate_with_cookies
+ current_user
+ else
+ reject_unauthorized_connection
+ end
+ end
+
+ def authenticate_with_cookies
+ # User.find(cookies.signed[:user_id])
+ end
+ end
+end
diff --git a/actioncable/lib/rails/generators/channel/templates/assets/channel.coffee b/actioncable/lib/rails/generators/channel/templates/assets/channel.coffee
new file mode 100644
index 0000000000..69f945ca3f
--- /dev/null
+++ b/actioncable/lib/rails/generators/channel/templates/assets/channel.coffee
@@ -0,0 +1,11 @@
+App.<%= class_name.underscore %> = App.cable.subscriptions.create "<%= class_name %>Channel",
+ connected: ->
+ # Called when the subscription is ready for use on the server
+
+ disconnected: ->
+ # Called when the subscription has been terminated by the server
+
+<% actions.each do |action| -%>
+ <%= action %>: ->
+ @perform '<%= action %>'
+<% end -%>
diff --git a/actioncable/lib/rails/generators/channel/templates/assets/consumer.coffee b/actioncable/lib/rails/generators/channel/templates/assets/consumer.coffee
new file mode 100644
index 0000000000..5bb6ab8296
--- /dev/null
+++ b/actioncable/lib/rails/generators/channel/templates/assets/consumer.coffee
@@ -0,0 +1,6 @@
+#= require cable
+#= require_self
+#= require ./channels
+
+@App = {}
+App.cable = Cable.createConsumer()
diff --git a/actioncable/lib/rails/generators/channel/templates/channel.rb b/actioncable/lib/rails/generators/channel/templates/channel.rb
new file mode 100644
index 0000000000..5ca80dcd9d
--- /dev/null
+++ b/actioncable/lib/rails/generators/channel/templates/channel.rb
@@ -0,0 +1,16 @@
+<% module_namespacing do -%>
+class <%= class_name %>Channel < ApplicationChannel
+ def subscribed
+ # stream_from "some_channel"
+ end
+
+ def unsubscribed
+ # Any cleanup needed when channel is unsubscribed
+ end
+<% actions.each do |action| -%>
+
+ def <%= action %>
+ end
+<% end -%>
+end
+<% end -%>