aboutsummaryrefslogtreecommitdiffstats
path: root/actioncable/lib/rails/generators/channel/templates
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2015-12-15 21:38:58 +0100
committerDavid Heinemeier Hansson <david@loudthinking.com>2015-12-15 21:39:22 +0100
commit40cc72548f529fbe3a52577dca3d0517f631f1b5 (patch)
tree2828aed91c7c04df439089ad309df6f8dc394ba1 /actioncable/lib/rails/generators/channel/templates
parent649b9d93219b5b38bb2fccba5124c4e013726d9e (diff)
downloadrails-40cc72548f529fbe3a52577dca3d0517f631f1b5.tar.gz
rails-40cc72548f529fbe3a52577dca3d0517f631f1b5.tar.bz2
rails-40cc72548f529fbe3a52577dca3d0517f631f1b5.zip
Basic channel generator
Diffstat (limited to 'actioncable/lib/rails/generators/channel/templates')
-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
5 files changed, 60 insertions, 0 deletions
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 -%>