Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
class ReportController < ApplicationController
before_filter :authenticate_user!
before_filter :redirect_unless_admin, :except => [:create]
def index
@report = Report.where(reviewed: false).all
end
def update
if Report.where(post_type: params[:type]).exists?(post_id: params[:id])
mark_as_reviewed
end
redirect_to :action => :index and return
end
def destroy
if (params[:type].eql? "post")
if Post.exists?(params[:id])
delete_post
end
elsif (params[:type].eql? "comment")
if Comment.exists?(params[:id])
delete_comment
end
end
redirect_to :action => :index and return
end
def create
code = 400
username = current_user.username
post = Report.new(
:post_id => params[:id],
:post_type => params[:type],
:user_id => username,
:text => params[:text])
unless Report.where("post_id = ? AND post_type = ?", params[:id], params[:type]).exists?(user_id: username)
result = post.save
code = 200 if result
end
render :nothing => true, :status => code
end
private
def delete_post
post = Post.find(params[:id])
current_user.retract(post)
mark_as_reviewed
flash[:notice] = I18n.t 'report.status.destroyed'
end
def delete_comment
comment = Comment.find(params[:id])
#current_user.retract(comment)
comment.destroy
mark_as_reviewed
flash[:notice] = I18n.t 'report.status.destroyed'
end
def mark_as_reviewed
posts = Report.where("post_id = ? AND post_type = ?", params[:id], params[:type])
posts.each do |post|
post.update_attributes(reviewed: true)
end
flash[:notice] = I18n.t 'report.status.marked'
end
end