From a0c0d68e43e52d557a8f765e35bda389e67ff2ea Mon Sep 17 00:00:00 2001 From: nilsding Date: Mon, 29 Dec 2014 00:50:14 +0100 Subject: [PATCH] posting moderation comments works now --- .../javascripts/moderation/comment.coffee | 65 +++++++++++++++++++ app/controllers/ajax/moderation_controller.rb | 27 ++++++++ app/models/user.rb | 4 ++ .../moderation/create_comment.json.jbuilder | 3 + .../moderation/destroy_comment.json.jbuilder | 2 + app/views/moderation/_discussion.html.haml | 8 +-- app/views/moderation/_moderationbox.html.haml | 8 +-- config/routes.rb | 6 +- 8 files changed, 112 insertions(+), 11 deletions(-) create mode 100644 app/assets/javascripts/moderation/comment.coffee create mode 100644 app/views/ajax/moderation/create_comment.json.jbuilder create mode 100644 app/views/ajax/moderation/destroy_comment.json.jbuilder diff --git a/app/assets/javascripts/moderation/comment.coffee b/app/assets/javascripts/moderation/comment.coffee new file mode 100644 index 00000000..393b1744 --- /dev/null +++ b/app/assets/javascripts/moderation/comment.coffee @@ -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' diff --git a/app/controllers/ajax/moderation_controller.rb b/app/controllers/ajax/moderation_controller.rb index d4516d33..1b6aff36 100644 --- a/app/controllers/ajax/moderation_controller.rb +++ b/app/controllers/ajax/moderation_controller.rb @@ -60,4 +60,31 @@ class Ajax::ModerationController < ApplicationController @message = "WHERE DID IT GO??? OH NO!!!" @success = true 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 diff --git a/app/models/user.rb b/app/models/user.rb index e2dbd745..4a805a4d 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -146,4 +146,8 @@ class User < ActiveRecord::Base report.moderation_votes.where(upvote: upvote).each { |s| return true if s.user_id == self.id } false end + + def report_comment(report, content) + ModerationComment.create!(user: self, report: report, content: content) + end end diff --git a/app/views/ajax/moderation/create_comment.json.jbuilder b/app/views/ajax/moderation/create_comment.json.jbuilder new file mode 100644 index 00000000..46fa7695 --- /dev/null +++ b/app/views/ajax/moderation/create_comment.json.jbuilder @@ -0,0 +1,3 @@ +json.partial! 'ajax/shared/status' +json.render @render if @render +json.count @count if @count \ No newline at end of file diff --git a/app/views/ajax/moderation/destroy_comment.json.jbuilder b/app/views/ajax/moderation/destroy_comment.json.jbuilder new file mode 100644 index 00000000..46291e5d --- /dev/null +++ b/app/views/ajax/moderation/destroy_comment.json.jbuilder @@ -0,0 +1,2 @@ +json.partial! 'ajax/shared/status' +json.count @count if @count \ No newline at end of file diff --git a/app/views/moderation/_discussion.html.haml b/app/views/moderation/_discussion.html.haml index d334a7b2..f2b8b707 100644 --- a/app/views/moderation/_discussion.html.haml +++ b/app/views/moderation/_discussion.html.haml @@ -15,10 +15,10 @@ %span.caret %ul.dropdown-menu.dropdown-menu-right{role: :menu} %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 Delete %p.comments--content= comment.content -.form-group.has-feedback - %input.form-control.comments--box{type: :text, placeholder: 'Comment...'} - %span.text-muted.form-control-feedback.comments--count 160 \ No newline at end of file +.form-group.has-feedback{name: 'mod-comment-new-group', data: { id: report.id }} + %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{id: "mod-comment-charcount-#{report.id}"} 160 \ No newline at end of file diff --git a/app/views/moderation/_moderationbox.html.haml b/app/views/moderation/_moderationbox.html.haml index 6764eedb..51eae25e 100644 --- a/app/views/moderation/_moderationbox.html.haml +++ b/app/views/moderation/_moderationbox.html.haml @@ -25,10 +25,10 @@ %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' }} %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 - %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 }} %i.fa.fa-trash-o - .panel-footer - = render 'moderation/discussion', report: report \ No newline at end of file + .panel-footer{id: "mod-comments-section-#{report.id}", style: 'display: none'} + %div{id: "mod-comments-#{report.id}"}= render 'moderation/discussion', report: report \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 946fb125..298ee719 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -15,10 +15,10 @@ Rails.application.routes.draw do (req.env['warden'].user.admin? or req.env['warden'].user.moderator?) } do match '/moderation', to: 'moderation#index', via: :get, as: :moderation 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/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 end end