forked from rails/actioncable-examples
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
173 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
App.comments = App.cable.subscriptions.create "CommentsChannel", | ||
collection: -> $("[data-channel='comments']") | ||
|
||
connected: -> | ||
# FIXME: While we wait for cable subscriptions to always be finalized before sending messages | ||
setTimeout => | ||
@followCurrentMessage() | ||
@installPageChangeCallback() | ||
, 1000 | ||
|
||
received: (data) -> | ||
@collection().append(data.comment) unless @userIsCurrentUser(data.comment) | ||
|
||
userIsCurrentUser: (comment) -> | ||
$(comment).attr('data-user-id') is $('meta[name=current-user]').attr('id') | ||
|
||
followCurrentMessage: -> | ||
if messageId = @collection().data('message-id') | ||
@perform 'follow', message_id: messageId | ||
else | ||
@perform 'unfollow' | ||
|
||
installPageChangeCallback: -> | ||
unless @installedPageChangeCallback | ||
@installedPageChangeCallback = true | ||
$(document).on 'page:change', -> App.comments.followCurrentMessage() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class CommentsChannel < ApplicationCable::Channel | ||
def follow(data) | ||
stop_all_streams | ||
stream_from "messages:#{data['message_id'].to_i}:comments" | ||
end | ||
|
||
def unfollow | ||
stop_all_streams | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class CommentsController < ApplicationController | ||
before_action :set_message | ||
|
||
def create | ||
@comment = Comment.create! content: params[:comment][:content], message: @message, user: @current_user | ||
end | ||
|
||
private | ||
def set_message | ||
@message = Message.find(params[:message_id]) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
class ExamplesController < ApplicationController | ||
before_action :ensure_authenticated_user | ||
|
||
def index | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class MessagesController < ApplicationController | ||
def index | ||
@messages = Message.all | ||
end | ||
|
||
def show | ||
@message = Message.find(params[:id]) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class CommentRelayJob < ApplicationJob | ||
def perform(comment) | ||
ActionCable.server.broadcast "messages:#{comment.message_id}:comments", | ||
comment: CommentsController.render(partial: 'comments/comment', locals: { comment: comment }) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class Comment < ActiveRecord::Base | ||
belongs_to :message | ||
belongs_to :user | ||
|
||
after_commit { CommentRelayJob.perform_later(self) } | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
class Message < ActiveRecord::Base | ||
belongs_to :user | ||
has_many :comments | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
class User < ActiveRecord::Base | ||
has_many :messages | ||
has_many :comments | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<article data-user-id="<%= comment.user.id %>"> | ||
<h3>Comment by <%= comment.user.name %></h3> | ||
<p><%= comment.content %></p> | ||
</article> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<section id="comments" data-channel="comments" data-message-id="<%= message.id %>"> | ||
<%= render message.comments %> | ||
</section> | ||
|
||
<%= render 'comments/new', message: message %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<%= form_for [ message, Comment.new ], remote: true do |form| %> | ||
<%= form.text_area :content, size: '100x20' %><br> | ||
<%= form.submit 'Post comment' %> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
$('#comments').append('<%=j render @comment %>'); | ||
$('#new_comment').replaceWith('<%=j render 'comments/new', message: @message %>'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
<h1>Action Cable Examples</h1> | ||
|
||
<p>Hello <%= @current_user.name %> (<%= link_to 'Logout', session_url, method: :delete %>)!</p> | ||
<p> | ||
Hello <%= @current_user.name %> | ||
(<%= link_to 'Logout', session_path, method: :delete %>)! | ||
</p> | ||
|
||
<p> | ||
<ul> | ||
<li>Appearance</li> | ||
<li>Comments</li> | ||
<li><%= link_to 'Messages with live comments', messages_path %></li> | ||
</ul> | ||
</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<h1>Messages</h1> | ||
|
||
<p> | ||
<ul> | ||
<% @messages.each do |message| %> | ||
<li><%= link_to message.title, message %></li> | ||
<% end %> | ||
</ul> | ||
</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<h1><%= @message.title %></h1> | ||
|
||
<p><%= @message.content %></p> | ||
|
||
<%= render 'comments/comments', message: @message %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,9 @@ | |
resource :session | ||
resources :examples | ||
|
||
resources :messages do | ||
resources :comments | ||
end | ||
|
||
root 'examples#index' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class CreateMessages < ActiveRecord::Migration | ||
def change | ||
create_table :messages do |t| | ||
t.references :user, index: true, foreign_key: true | ||
|
||
t.string :title | ||
t.text :content | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class CreateComments < ActiveRecord::Migration | ||
def change | ||
create_table :comments do |t| | ||
t.references :message, index: true, foreign_key: true | ||
t.references :user, index: true, foreign_key: true | ||
|
||
t.text :content | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
# This file should contain all the record creation needed to seed the database with its default values. | ||
# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup). | ||
|
||
User.create! name: 'Funkmaster Flex' | ||
User.create! name: 'The Notorious B.I.G.' | ||
User.create! name: 'Snoop Dogg' | ||
User.create! name: 'Ice Cube' | ||
big = User.create! name: 'The Notorious B.I.G.' | ||
snoop = User.create! name: 'Snoop Dogg' | ||
flex = User.create! name: 'Funkmaster Flex' | ||
ice = User.create! name: 'Ice Cube' | ||
|
||
Message.create! title: 'Tha Shiznit', content: 'Poppin, stoppin, hoppin like a rabbit', user: snoop | ||
Message.create! title: 'Hypnotize ', content: 'Hah, sicker than your average Poppa', user: big |