reporting and deleting comments works now

This commit is contained in:
nilsding 2014-12-28 21:14:01 +01:00
parent 27afd18d25
commit b3f165b12b
8 changed files with 72 additions and 8 deletions

View File

@ -0,0 +1,18 @@
$(document).on "click", "a[data-action=ab-comment-destroy]", (ev) ->
ev.preventDefault()
if confirm 'Are you sure?'
btn = $(this)
cid = btn[0].dataset.cId
$.ajax
url: '/ajax/destroy_comment'
type: 'POST'
data:
comment: cid
success: (data, status, jqxhr) ->
if data.success
$("li[data-comment-id=#{cid}]").slideUp()
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) ->

View File

@ -0,0 +1,18 @@
$(document).on "click", "a[data-action=ab-comment-report]", (ev) ->
ev.preventDefault()
if confirm 'Are you sure you want to report this comment?'
btn = $(this)
cid = btn[0].dataset.cId
$.ajax
url: '/ajax/report'
type: 'POST'
data:
id: cid
type: 'comment'
success: (data, status, jqxhr) ->
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) ->
btn.button "reset"

View File

@ -4,9 +4,9 @@ class Ajax::AnswerController < ApplicationController
answer = Answer.find(params[:answer]) answer = Answer.find(params[:answer])
unless privileged? answer.user unless (current_user == answer.user) or (privileged? answer.user)
@status = :nopriv @status = :nopriv
@message = "check yuor privlegs" @message = "can't delete other people's answers"
@success = false @success = false
return return
end end

View File

@ -20,4 +20,29 @@ class Ajax::CommentController < ApplicationController
@render = render_to_string(partial: 'shared/comments', locals: { a: answer }) @render = render_to_string(partial: 'shared/comments', locals: { a: answer })
@count = answer.comment_count @count = answer.comment_count
end end
def destroy
params.require :comment
@status = :err
@success = false
comment = Comment.find(params[:comment])
unless (current_user == comment.user) or (current_user == comment.answer.user) or (privileged? comment.user)
@status = :nopriv
@message = "can't delete other people's comments"
@success = false
return
end
comment.user.decrement! :commented_count
comment.answer.decrement! :comment_count
Notification.denotify comment.answer.user, comment
@count = comment.answer.comment_count
comment.destroy
@status = :okay
@message = "Successfully deleted comment."
@success = true
end
end end

View File

@ -56,7 +56,7 @@ module ApplicationHelper
end end
def privileged?(user) def privileged?(user)
(current_user && (current_user == user || current_user.admin?)) ? true : false ((!current_user.nil?) && ((current_user == user) || current_user.mod?)) ? true : false
end end
def gravatar_url(user) def gravatar_url(user)

View File

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

View File

@ -15,17 +15,17 @@
%button.btn.btn-link.btn-sm.dropdown-toggle{data: { toggle: :dropdown }, aria: { expanded: :false }} %button.btn.btn-link.btn-sm.dropdown-toggle{data: { toggle: :dropdown }, aria: { expanded: :false }}
%span.caret %span.caret
%ul.dropdown-menu.dropdown-menu-right{role: :menu} %ul.dropdown-menu.dropdown-menu-right{role: :menu}
- if privileged? a.user - if privileged?(comment.user) or privileged?(a.user)
%li.text-danger %li.text-danger
%a{href: '#', name: 'ab-destroy', data: { a_id: comment.id }} %a{href: '#', data: { action: 'ab-comment-destroy', c_id: comment.id }}
%i.fa.fa-trash-o %i.fa.fa-trash-o
Delete Delete
%li %li
%a{href: '#', name: 'ab-report', data: { a_id: comment.id }} %a{href: '#', data: { action: 'ab-comment-report', c_id: comment.id }}
%i.fa.fa-exclamation-triangle %i.fa.fa-exclamation-triangle
Report Report
%p.comments--content= comment.content %p.comments--content= comment.content
- if user_signed_in? - if user_signed_in?
.form-group.has-feedback{name: 'ab-comment-new-group', 'data-a-id' => a.id} .form-group.has-feedback{name: 'ab-comment-new-group', data: { a_id: a.id }}
%input.form-control.comments--box{type: :text, placeholder: 'Comment...', name: 'ab-comment-new', data: {a_id: a.id }} %input.form-control.comments--box{type: :text, placeholder: 'Comment...', name: 'ab-comment-new', data: {a_id: a.id }}
%span.text-muted.form-control-feedback.comments--count{id: "ab-comment-charcount-#{a.id}"} 160 %span.text-muted.form-control-feedback.comments--count{id: "ab-comment-charcount-#{a.id}"} 160

View File

@ -62,6 +62,7 @@ Rails.application.routes.draw do
match '/create_smile', to: 'smile#create', via: :post, as: :create_smile match '/create_smile', to: 'smile#create', via: :post, as: :create_smile
match '/destroy_smile', to: 'smile#destroy', via: :post, as: :destroy_smile match '/destroy_smile', to: 'smile#destroy', via: :post, as: :destroy_smile
match '/create_comment', to: 'comment#create', via: :post, as: :create_comment match '/create_comment', to: 'comment#create', via: :post, as: :create_comment
match '/destroy_comment', to: 'comment#destroy', via: :post, as: :destroy_comment
match '/report', to: 'report#create', via: :post, as: :report match '/report', to: 'report#create', via: :post, as: :report
end end