added votes for reports
This commit is contained in:
parent
3f3591c6e7
commit
7a0b3f4af7
|
@ -0,0 +1,43 @@
|
||||||
|
($ document).on "click", "button[name=mod-vote]", ->
|
||||||
|
btn = $(this)
|
||||||
|
id = btn[0].dataset.id
|
||||||
|
action = btn[0].dataset.action
|
||||||
|
upvote = btn[0].dataset.voteType == 'upvote'
|
||||||
|
btn.attr 'disabled', 'disabled'
|
||||||
|
|
||||||
|
target_url = switch action
|
||||||
|
when 'vote'
|
||||||
|
'/ajax/mod/create_vote'
|
||||||
|
when 'unvote'
|
||||||
|
'/ajax/mod/destroy_vote'
|
||||||
|
|
||||||
|
success = false
|
||||||
|
|
||||||
|
$.ajax
|
||||||
|
url: target_url
|
||||||
|
type: 'POST'
|
||||||
|
data:
|
||||||
|
id: id
|
||||||
|
upvote: upvote
|
||||||
|
success: (data, status, jqxhr) ->
|
||||||
|
success = data.success
|
||||||
|
if success
|
||||||
|
($ "span#mod-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) ->
|
||||||
|
btn.removeAttr 'disabled'
|
||||||
|
if success
|
||||||
|
switch action
|
||||||
|
when 'vote'
|
||||||
|
btn[0].dataset.action = 'unvote'
|
||||||
|
other_btn = ($ "button[name=mod-vote][data-id=#{id}][data-action=vote]")
|
||||||
|
other_btn.attr 'disabled', 'disabled'
|
||||||
|
other_btn[0].dataset.action = 'unvote'
|
||||||
|
when 'unvote'
|
||||||
|
btn[0].dataset.action = 'vote'
|
||||||
|
other_btn = ($ "button[name=mod-vote][data-id=#{id}][data-action=unvote]")
|
||||||
|
other_btn.removeAttr 'disabled'
|
||||||
|
other_btn[0].dataset.action = 'vote'
|
|
@ -0,0 +1,43 @@
|
||||||
|
class Ajax::ModerationController < ApplicationController
|
||||||
|
|
||||||
|
def vote
|
||||||
|
params.require :id
|
||||||
|
params.require :upvote
|
||||||
|
|
||||||
|
report = Report.find(params[:id])
|
||||||
|
|
||||||
|
begin
|
||||||
|
current_user.report_vote(report, params[:upvote])
|
||||||
|
rescue
|
||||||
|
@status = :fail
|
||||||
|
@message = "You have already voted on this report."
|
||||||
|
@success = false
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
@count = report.votes
|
||||||
|
@status = :okay
|
||||||
|
@message = "Successfully voted on report."
|
||||||
|
@success = true
|
||||||
|
end
|
||||||
|
|
||||||
|
def destroy_vote
|
||||||
|
params.require :id
|
||||||
|
|
||||||
|
report = Report.find(params[:id])
|
||||||
|
|
||||||
|
begin
|
||||||
|
current_user.report_unvote report
|
||||||
|
rescue
|
||||||
|
@status = :fail
|
||||||
|
@message = "You have not voted on that report."
|
||||||
|
@success = false
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
@count = report.votes
|
||||||
|
@status = :okay
|
||||||
|
@message = "Successfully removed vote from report."
|
||||||
|
@success = true
|
||||||
|
end
|
||||||
|
end
|
|
@ -9,4 +9,8 @@ class Report < ActiveRecord::Base
|
||||||
def target
|
def target
|
||||||
type.sub('Reports::', '').constantize.where(id: target_id).first
|
type.sub('Reports::', '').constantize.where(id: target_id).first
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def votes
|
||||||
|
moderation_votes.where(upvote: true).count - moderation_votes.where(upvote: false).count
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -29,7 +29,11 @@ class User < ActiveRecord::Base
|
||||||
SCREEN_NAME_REGEX = /\A[a-zA-Z0-9_]{1,16}\z/
|
SCREEN_NAME_REGEX = /\A[a-zA-Z0-9_]{1,16}\z/
|
||||||
WEBSITE_REGEX = /https?:\/\/([A-Za-z.\-]+)\/?(?:.*)/i
|
WEBSITE_REGEX = /https?:\/\/([A-Za-z.\-]+)\/?(?:.*)/i
|
||||||
|
|
||||||
validates :screen_name, presence: true, format: { with: SCREEN_NAME_REGEX }, uniqueness: { case_sensitive: false }
|
validates :screen_name, presence: true, format: { with: SCREEN_NAME_REGEX }, uniqueness: { case_sensitive: false }#,
|
||||||
|
#exclusion: { in: %w(justask_admin retrospring_admin admin justask retrospring support about public
|
||||||
|
# notifications inbox sign_in sign_up sidekiq moderation moderator mod administrator
|
||||||
|
# siteadmin site_admin),
|
||||||
|
# message: "%{value} is reserved." }
|
||||||
|
|
||||||
validates :display_name, length: { maximum: 50 }
|
validates :display_name, length: { maximum: 50 }
|
||||||
validates :bio, length: { maximum: 200 }
|
validates :bio, length: { maximum: 200 }
|
||||||
|
@ -85,7 +89,7 @@ class User < ActiveRecord::Base
|
||||||
# smiles an answer
|
# smiles an answer
|
||||||
# @param answer [Answer] the answer to smile
|
# @param answer [Answer] the answer to smile
|
||||||
def smile(answer)
|
def smile(answer)
|
||||||
Smile.create(user: self, answer: answer)
|
Smile.create!(user: self, answer: answer)
|
||||||
end
|
end
|
||||||
|
|
||||||
# unsmile an answer
|
# unsmile an answer
|
||||||
|
@ -118,4 +122,21 @@ class User < ActiveRecord::Base
|
||||||
def report(object)
|
def report(object)
|
||||||
Report.create(type: "Reports::#{object.class}", target_id: object.id, user_id: self.id)
|
Report.create(type: "Reports::#{object.class}", target_id: object.id, user_id: self.id)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# @param upvote [Boolean]
|
||||||
|
def report_vote(report, upvote = false)
|
||||||
|
return unless mod?
|
||||||
|
ModerationVote.create!(user: self, report: report, upvote: upvote)
|
||||||
|
end
|
||||||
|
|
||||||
|
def report_unvote(report)
|
||||||
|
return unless mod?
|
||||||
|
ModerationVote.find_by(user: self, report: report).destroy
|
||||||
|
end
|
||||||
|
|
||||||
|
def report_voted?(report)
|
||||||
|
return false unless mod?
|
||||||
|
report.moderation_votes.each { |s| return true if s.user_id == self.id }
|
||||||
|
false
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
json.partial! 'ajax/shared/status'
|
||||||
|
json.count @count if @count
|
|
@ -0,0 +1,2 @@
|
||||||
|
json.partial! 'ajax/shared/status'
|
||||||
|
json.count @count if @count
|
|
@ -18,12 +18,13 @@
|
||||||
View reported
|
View reported
|
||||||
= report.type.sub('Reports::', '')
|
= report.type.sub('Reports::', '')
|
||||||
.col-md-6.col-sm-8.col-xs-6.text-right
|
.col-md-6.col-sm-8.col-xs-6.text-right
|
||||||
%button.btn.btn-success.btn-sm
|
%span.mod-count{id: "mod-count-#{report.id}"}
|
||||||
%i.fa.fa-thumbs-up
|
= report.votes
|
||||||
%span 0
|
.btn-group
|
||||||
%button.btn.btn-danger.btn-sm
|
%button.btn.btn-success.btn-sm{name: "mod-vote", disabled: current_user.report_voted?(report) ? 'disabled' : nil, data: { id: report.id, action: current_user.report_voted?(report) ? 'unvote' : 'vote', vote_type: 'upvote' }}
|
||||||
%i.fa.fa-thumbs-down
|
%i.fa.fa-thumbs-up
|
||||||
%span 0
|
%button.btn.btn-danger.btn-sm{name: "mod-vote", disabled: current_user.report_voted?(report) ? '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
|
%button.btn.btn-primary.btn-sm
|
||||||
%i.fa.fa-comments
|
%i.fa.fa-comments
|
||||||
%span 0
|
%span 0
|
||||||
|
|
|
@ -14,6 +14,12 @@ Rails.application.routes.draw do
|
||||||
constraints ->(req) { req.env['warden'].authenticate?(scope: :user) &&
|
constraints ->(req) { req.env['warden'].authenticate?(scope: :user) &&
|
||||||
(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
|
||||||
|
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/create_vote', to: 'moderation#vote', via: :post, as: :mod_vote
|
||||||
|
match '/mod/destroy_vote', to: 'moderation#destroy_vote', via: :post, as: :mod_destroy_vote
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
root 'static#index'
|
root 'static#index'
|
||||||
|
|
Loading…
Reference in New Issue