Created
September 23, 2016 02:05
-
-
Save AleksandrKudashkin/f69eea949f1452962c83f35003220ff5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
module Commented | |
extend ActiveSupport::Concern | |
included do | |
before_action :load_commentable, only: :add_comment | |
end | |
def add_comment | |
@comment = @commentable.comments.new(commentable_params) | |
if @comment.save | |
# PrivatePub.publish_to "/#{controller_name.pluralize.downcase}/#{@commentable.id}/comments", | |
# comment: @comment.to_json | |
respond_to_json_comment | |
end | |
end | |
private | |
def model_klass | |
controller_name.classify.constantize | |
end | |
def load_commentable | |
@commentable = model_klass.find(params[:id]) | |
end | |
def commentable_params | |
params.require(:comment).permit(:body) | |
end | |
def respond_to_json_comment | |
respond_to do |format| | |
format.json { render json: @comment.body } | |
end | |
end | |
end |
This file contains hidden or 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
ready = -> | |
$('form#edit-question').hide() | |
$('.edit-question-link').click (e) -> | |
e.preventDefault() | |
$(this).hide() | |
$('form#edit-question').show() | |
$('#new_comment').on 'ajax:success', (e, data, status, xhr) -> | |
console.log(xhr) | |
msg = $.parseJSON(xhr.responseText) | |
console.log(msg) | |
$('input#comment_body.form-control').val('') | |
$('.q-comments').append('<p>' + msg + '</p>') | |
$('a#q-vote-up').bind 'ajax:success', (e, data, status, xhr) -> | |
$('.vote-button-up').hide() | |
$('.vote-button-down').hide() | |
$('.cancel-vote').show() | |
rating = $.parseJSON(xhr.responseText) | |
$('.q-rating').html(rating) | |
$('a#q-vote-down').bind 'ajax:success', (e, data, status, xhr) -> | |
$('.vote-button-up').hide() | |
$('.vote-button-down').hide() | |
$('.cancel-vote').show() | |
rating = $.parseJSON(xhr.responseText) | |
$('.q-rating').html(rating) | |
$('a#q-cancel-vote').bind 'ajax:success', (e, data, status, xhr) -> | |
$('.cancel-vote').hide() | |
$('.vote-button-up').show() | |
$('.vote-button-down').show() | |
rating = $.parseJSON(xhr.responseText) | |
$('.q-rating').html(rating) | |
# commentableId = $('.new-question-comment').data('commentableId') | |
# PrivatePub.subscribe '/questions/' + commentableId + '/comments', (data, channel) -> | |
# comment = $.parseJSON(data['comment']) | |
# html = '<p>' + comment.body + '</p>' | |
# $('.q-comments').append(html) | |
PrivatePub.subscribe '/questions/index', (data, channel) -> | |
question = $.parseJSON(data['question']) | |
html = '<h3><i class="fa fa-question-circle"></i><a href=/questions/' + question.id + '>' + question.title + '</a></h3><p>' + question.body + '</p>' | |
$('.questions').append(html) | |
$('#new_comment').find("comment_body").val() | |
document.addEventListener('turbolinks:load', ready) |
This file contains hidden or 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
.question | |
= render @question | |
.row | |
.col-md-1 | |
.col-md-11 | |
em Comments: | |
hr | |
.q-comments | |
= render 'comments/comment', commentable: @commentable | |
= subscribe_to "/questions/#{@question.id}/comments" | |
.q-comments-errors | |
- if user_signed_in? | |
= form_for(@commentable.comments.new, | |
url: add_comment_question_path, | |
html: { class: "form-inline new-question-comment" }, | |
remote: true, | |
data: { type: :json, commentable_id: @commentable.id }) do |f| | |
.form-group | |
= f.label :body, 'Enter a comment', class: "sr-only" | |
= f.text_field :body, class: "form-control" | |
= f.submit "Send", class: "btn btn-xs btn-primary" | |
br | |
h4 Answers: | |
hr | |
.answers | |
- if @question.answers.any? | |
= render @answers | |
= subscribe_to "/questions/#{@question.id}/answers" | |
- if user_signed_in? | |
hr | |
.new_answer_info | |
= form_for [@question, @answer], remote: true do |f| | |
.form-group | |
= f.label :body, 'Your answer:' | |
= f.text_area :body, size: "60x12", class: "form-control" | |
p | |
= render 'attachments', f:f | |
br | |
br | |
= link_to "To questions index", questions_path, class: "btn btn-md btn-default" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment