Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from sad16/11-action-cable
action cable
- Loading branch information
Showing
50 changed files
with
690 additions
and
23 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
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,8 +1,31 @@ | ||
$(document).on('turbolinks:load', function(){ | ||
$('.answers').on('click', '.edit-answer-link', function(e) { | ||
$(document).on('turbolinks:load', function() { | ||
var answerList = $('.answers'); | ||
|
||
answerList.on('click', '.edit-answer-link', function(e) { | ||
e.preventDefault(); | ||
$(this).addClass('hidden'); | ||
var answerId = $(this).data('answerId'); | ||
$('form#edit-answer-form-' + answerId).removeClass('hidden'); | ||
}) | ||
|
||
App.cable.subscriptions.create('AnswersChannel', { | ||
connected() { | ||
if (gon.question_id) { | ||
this.perform('follow', { question_id: gon.question_id }) | ||
} | ||
}, | ||
received(answer) { | ||
if (answer.author_id !== gon.current_user_id) { | ||
var answer_template = answer.templates.answer; | ||
answerList.append(answer_template); | ||
|
||
if (gon.current_user_id) { | ||
$(`#answer-id-${answer.id} .vote-actions`).append(answer.templates.vote_links); | ||
onAjaxSuccessVoteLink($(`#answer-id-${answer.id} .vote-actions .vote-link`)); | ||
} | ||
|
||
loadGists(); | ||
} | ||
} | ||
}); | ||
}); |
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,62 @@ | ||
$(document).on('turbolinks:load', function() { | ||
onAjaxCommentForm($('.new-comment-form form')); | ||
|
||
App.cable.subscriptions.create('CommentsChannel', { | ||
connected() { | ||
if (gon.question_id) { | ||
this.perform('follow', { question_id: gon.question_id }) | ||
} | ||
}, | ||
received(comment) { | ||
if (comment.author_id !== gon.current_user_id) { | ||
var comment_template = comment.template; | ||
|
||
if (comment.answer_id) { | ||
$(`#answer-id-${comment.answer_id}`).find('.answer-comments').append(comment_template); | ||
} else { | ||
$('.question-comments').append(comment_template); | ||
} | ||
} | ||
} | ||
}); | ||
}); | ||
|
||
function onAjaxCommentForm(elem) { | ||
$(elem) | ||
.on('ajax:success', function(response) { | ||
clearCommentError(response.currentTarget); | ||
clearCommentForm(response.currentTarget); | ||
showComment(response.detail[0]); | ||
}) | ||
.on('ajax:error', function(response) { | ||
clearCommentError(response.currentTarget); | ||
|
||
var error_block = $(response.currentTarget).closest('.new-comment-form').find('.comment-errors'); | ||
$.each(response.detail[0].errors, function(index, error) { | ||
error_block.append(`<p>${error}</p>`) | ||
}); | ||
}) | ||
} | ||
|
||
function clearCommentError(form) { | ||
var error_block = $(form).closest('.new-comment-form').find('.comment-errors'); | ||
error_block.html(''); | ||
} | ||
|
||
function clearCommentForm(form) { | ||
var inputs = $(form).find('input') | ||
inputs.val(''); | ||
} | ||
|
||
function showComment(comment) { | ||
var template = `<div className="comment" id="comment-id-${comment.id}"><p>${comment.text}</p></div>` | ||
|
||
switch (comment.commentable_type) { | ||
case 'Question': | ||
$('.question-comments').append(template); | ||
break; | ||
case 'Answer': | ||
$(`#answer-id-${comment.commentable_id}`).find('.answer-comments').append(template); | ||
break; | ||
} | ||
} |
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
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 @@ | ||
class AnswersChannel < ApplicationCable::Channel | ||
def follow(params) | ||
stream_from "questions/#{params['question_id']}/answers" | ||
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,5 @@ | ||
class CommentsChannel < ApplicationCable::Channel | ||
def follow(params) | ||
stream_from "questions/#{params['question_id']}/comments" | ||
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,5 @@ | ||
class QuestionsChannel < ApplicationCable::Channel | ||
def follow | ||
stream_from 'questions' | ||
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,3 +1,9 @@ | ||
class ApplicationController < ActionController::Base | ||
include ApplicationHelper | ||
|
||
private | ||
|
||
def render_template(options) | ||
ApplicationController.render(options) | ||
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,42 @@ | ||
class CommentsController < ApplicationController | ||
before_action :authenticate_user! | ||
before_action :set_commentable, only: :create | ||
|
||
after_action :publish_comment, only: [:create] | ||
|
||
def create | ||
@comment = @commentable.comments.new(user: current_user, text: comment_params[:text]) | ||
|
||
if @comment.save | ||
render json: @comment | ||
else | ||
render json: { errors: @comment.errors.full_messages }, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
private | ||
|
||
def set_commentable | ||
@commentable = params[:commentable_type].classify.constantize.find(params[:commentable_id]) | ||
end | ||
|
||
def comment_params | ||
params.require(:comment) | ||
end | ||
|
||
def publish_comment | ||
return if @comment.errors.present? | ||
|
||
ActionCable.server.broadcast( | ||
@comment.ws_stream_name, | ||
{ | ||
author_id: @comment.user_id, | ||
answer_id: @comment.commentable_type == 'Answer' ? @comment.commentable_id : nil, | ||
template: render_template( | ||
partial: 'websockets/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
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,14 @@ | ||
class Comment < ApplicationRecord | ||
belongs_to :user | ||
belongs_to :commentable, polymorphic: true | ||
|
||
validates :text, presence: true | ||
|
||
def ws_stream_name | ||
case commentable_type | ||
when 'Question' then "questions/#{commentable_id}/comments" | ||
when 'Answer' then "questions/#{commentable.question_id}/comments" | ||
else nil | ||
end | ||
end | ||
end |
Oops, something went wrong.