posting moderation comments works now

This commit is contained in:
nilsding 2014-12-29 00:50:14 +01:00
parent b7b5ee960e
commit a0c0d68e43
8 changed files with 112 additions and 11 deletions

View file

@ -0,0 +1,65 @@
# Toggle button
$(document).on "click", "button[name=mod-comments]", ->
btn = $(this)
id = btn[0].dataset.id
state = btn[0].dataset.state
commentBox = $("#mod-comments-section-#{id}")
switch state
when 'hidden'
commentBox.slideDown()
btn[0].dataset.state = 'shown'
when 'shown'
commentBox.slideUp()
btn[0].dataset.state = 'hidden'
$(document).on "keyup", "input[name=mod-comment-new]", (evt) ->
input = $(this)
id = input[0].dataset.id
ctr = $("span#mod-comment-charcount-#{id}")
cbox = $("div[name=mod-comment-new-group][data-id=#{id}]")
if evt.which == 13 # return key
evt.preventDefault()
return cbox.addClass "has-error" if input.val().length > 160 || input.val().trim().length == 0
input.attr 'disabled', 'disabled'
$.ajax
url: '/ajax/mod/create_comment'
type: 'POST'
data:
id: id
comment: input.val()
dataType: 'json' # jQuery can't guess the datatype correctly here...
success: (data, status, jqxhr) ->
console.log data
if data.success
$("#mod-comments-#{id}").html data.render
input.val ''
ctr.html 160
$("span#mod-comment-count-#{id}").html data.count
showNotification data.message, data.success
error: (jqxhr, status, error) ->
console.log jqxhr, status, error
showNotification "An error occurred, a developer should check the console for details", false
complete: (jqxhr, status) ->
input.removeAttr 'disabled'
# character count
$(document).on "input", "input[name=mod-comment-new]", (evt) ->
input = $(this)
id = input[0].dataset.id
ctr = $("span#mod-comment-charcount-#{id}")
cbox = $("div[name=mod-comment-new-group][data-id=#{id}]")
cbox.removeClass "has-error" if cbox.hasClass "has-error"
ctr.html 160 - input.val().length
if Number(ctr.html()) < 0
ctr.removeClass 'text-muted'
ctr.addClass 'text-danger'
else
ctr.removeClass 'text-danger'
ctr.addClass 'text-muted'

View file

@ -60,4 +60,31 @@ class Ajax::ModerationController < ApplicationController
@message = "WHERE DID IT GO??? OH NO!!!" @message = "WHERE DID IT GO??? OH NO!!!"
@success = true @success = true
end end
def create_comment
params.require :id
params.require :comment
report = Report.find(params[:id])
@success = false
begin
current_user.report_comment(report, params[:comment])
rescue ActiveRecord::RecordInvalid
@status = :rec_inv
@message = "Your comment is too long."
return
end
@status = :okay
@message = "Comment posted successfully."
@success = true
@render = render_to_string(partial: 'moderation/discussion', locals: { report: report })
@count = report.moderation_comments.all.count
end
def destroy_comment
end
end end

View file

@ -146,4 +146,8 @@ class User < ActiveRecord::Base
report.moderation_votes.where(upvote: upvote).each { |s| return true if s.user_id == self.id } report.moderation_votes.where(upvote: upvote).each { |s| return true if s.user_id == self.id }
false false
end end
def report_comment(report, content)
ModerationComment.create!(user: self, report: report, content: content)
end
end end

View file

@ -0,0 +1,3 @@
json.partial! 'ajax/shared/status'
json.render @render if @render
json.count @count if @count

View file

@ -0,0 +1,2 @@
json.partial! 'ajax/shared/status'
json.count @count if @count

View file

@ -15,10 +15,10 @@
%span.caret %span.caret
%ul.dropdown-menu.dropdown-menu-right{role: :menu} %ul.dropdown-menu.dropdown-menu-right{role: :menu}
%li.text-danger %li.text-danger
%a{href: '#', name: 'ab-destroy', data: { a_id: comment.id }} %a{href: '#', name: 'mod-comment-destroy', data: { id: comment.id }}
%i.fa.fa-trash-o %i.fa.fa-trash-o
Delete Delete
%p.comments--content= comment.content %p.comments--content= comment.content
.form-group.has-feedback .form-group.has-feedback{name: 'mod-comment-new-group', data: { id: report.id }}
%input.form-control.comments--box{type: :text, placeholder: 'Comment...'} %input.form-control.comments--box{type: :text, placeholder: 'Comment...', name: 'mod-comment-new', data: { id: report.id }}
%span.text-muted.form-control-feedback.comments--count 160 %span.text-muted.form-control-feedback.comments--count{id: "mod-comment-charcount-#{report.id}"} 160

View file

@ -25,10 +25,10 @@
%i.fa.fa-thumbs-up %i.fa.fa-thumbs-up
%button.btn.btn-danger.btn-sm{type: :button, name: "mod-vote", disabled: current_user.report_x_voted?(report, false) ? 'disabled' : nil, data: { id: report.id, action: current_user.report_voted?(report) ? 'unvote' : 'vote', vote_type: 'downvote' }} %button.btn.btn-danger.btn-sm{type: :button, name: "mod-vote", disabled: current_user.report_x_voted?(report, false) ? 'disabled' : nil, data: { id: report.id, action: current_user.report_voted?(report) ? 'unvote' : 'vote', vote_type: 'downvote' }}
%i.fa.fa-thumbs-down %i.fa.fa-thumbs-down
%button.btn.btn-primary.btn-sm{type: :button} %button.btn.btn-primary.btn-sm{type: :button, name: 'mod-comments', data: { id: report.id, state: :hidden }}
%i.fa.fa-comments %i.fa.fa-comments
%span 0 %span{id: "mod-comment-count-#{report.id}"}= report.moderation_comments.all.count
%button.btn.btn-default.btn-sm{type: :button, name: "mod-delete-report", data: { id: report.id }} %button.btn.btn-default.btn-sm{type: :button, name: "mod-delete-report", data: { id: report.id }}
%i.fa.fa-trash-o %i.fa.fa-trash-o
.panel-footer .panel-footer{id: "mod-comments-section-#{report.id}", style: 'display: none'}
= render 'moderation/discussion', report: report %div{id: "mod-comments-#{report.id}"}= render 'moderation/discussion', report: report

View file

@ -15,10 +15,10 @@ Rails.application.routes.draw do
(req.env['warden'].user.admin? or req.env['warden'].user.moderator?) } do (req.env['warden'].user.admin? or req.env['warden'].user.moderator?) } do
match '/moderation', to: 'moderation#index', via: :get, as: :moderation match '/moderation', to: 'moderation#index', via: :get, as: :moderation
namespace :ajax do namespace :ajax do
match '/mod/create_comment', to: 'moderation#comment', via: :post, as: :mod_comment
match '/mod/destroy_comment', to: 'moderation#destroy_comment', via: :post, as: :mod_destroy_comment
match '/mod/destroy_report', to: 'moderation#destroy_report', via: :post, as: :mod_destroy_report match '/mod/destroy_report', to: 'moderation#destroy_report', via: :post, as: :mod_destroy_report
match '/mod/create_vote', to: 'moderation#vote', via: :post, as: :mod_vote match '/mod/create_comment', to: 'moderation#create_comment', via: :post, as: :mod_create_comment
match '/mod/destroy_comment', to: 'moderation#destroy_comment', via: :post, as: :mod_destroy_comment
match '/mod/create_vote', to: 'moderation#vote', via: :post, as: :mod_create_vote
match '/mod/destroy_vote', to: 'moderation#destroy_vote', via: :post, as: :mod_destroy_vote match '/mod/destroy_vote', to: 'moderation#destroy_vote', via: :post, as: :mod_destroy_vote
end end
end end