Add get_reported_guid spec for report helper
* two new methods in report model reported_author and item * merge deletion methods in report model Signed-off-by: Lukas Matt <lukas@zauberstuhl.de>
This commit is contained in:
parent
d1e3e568b9
commit
95072d6010
5 changed files with 64 additions and 62 deletions
|
|
@ -1,5 +1,4 @@
|
||||||
#reports {
|
#reports {
|
||||||
padding-top: 2em;
|
|
||||||
.reason {
|
.reason {
|
||||||
padding-bottom: 20px;
|
padding-bottom: 20px;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,22 +3,15 @@
|
||||||
# the COPYRIGHT file.
|
# the COPYRIGHT file.
|
||||||
|
|
||||||
module ReportHelper
|
module ReportHelper
|
||||||
def get_reported_guid(id, type)
|
def report_content(report)
|
||||||
if type == "post"
|
case (item = report.item)
|
||||||
Post.where(id: id).first.author.guid
|
when Post
|
||||||
elsif type == "comment"
|
raw t("report.post_label", title: link_to(post_page_title(item), post_path(item.id)))
|
||||||
Comment.where(id: id).first.author.guid
|
when Comment
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def report_content(id, type)
|
|
||||||
if type == "post" && !(post = Post.find_by_id(id)).nil?
|
|
||||||
raw t("report.post_label", title: link_to(post_page_title(post), post_path(id)))
|
|
||||||
elsif type == "comment" && !(comment = Comment.find_by_id(id)).nil?
|
|
||||||
# comment_message is not html_safe. To prevent
|
|
||||||
# cross-site-scripting we have to escape html
|
|
||||||
raw t("report.comment_label", data: link_to(
|
raw t("report.comment_label", data: link_to(
|
||||||
h(comment_message(comment)), post_path(comment.post.id, anchor: comment.guid)))
|
h(comment_message(item)),
|
||||||
|
post_path(item.post.id, anchor: item.guid)
|
||||||
|
))
|
||||||
else
|
else
|
||||||
raw t("report.not_found")
|
raw t("report.not_found")
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,6 @@
|
||||||
class Report < ActiveRecord::Base
|
class Report < ActiveRecord::Base
|
||||||
|
POST, COMMENT = %w(post comment).map(&:freeze)
|
||||||
|
|
||||||
validates :user_id, presence: true
|
validates :user_id, presence: true
|
||||||
validates :item_id, presence: true
|
validates :item_id, presence: true
|
||||||
validates :item_type, presence: true, :inclusion => { :in => %w(post comment),
|
validates :item_type, presence: true, :inclusion => { :in => %w(post comment),
|
||||||
|
|
@ -14,6 +16,18 @@ class Report < ActiveRecord::Base
|
||||||
|
|
||||||
after_commit :send_report_notification, :on => :create
|
after_commit :send_report_notification, :on => :create
|
||||||
|
|
||||||
|
def item
|
||||||
|
if item_type == POST
|
||||||
|
Post.find_by(id: item_id)
|
||||||
|
elsif item_type == COMMENT
|
||||||
|
Comment.find_by(id: item_id)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def reported_author
|
||||||
|
item.author unless item.nil?
|
||||||
|
end
|
||||||
|
|
||||||
def entry_does_not_exist
|
def entry_does_not_exist
|
||||||
if Report.where(item_id: item_id, item_type: item_type).exists?(user_id: user_id)
|
if Report.where(item_id: item_id, item_type: item_type).exists?(user_id: user_id)
|
||||||
errors[:base] << 'You cannot report the same post twice.'
|
errors[:base] << 'You cannot report the same post twice.'
|
||||||
|
|
@ -27,35 +41,24 @@ class Report < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
def destroy_reported_item
|
def destroy_reported_item
|
||||||
if item_type == 'post'
|
case item
|
||||||
delete_post
|
when Post
|
||||||
elsif item_type == 'comment'
|
if item.author.local?
|
||||||
delete_comment
|
item.author.owner.retract(item)
|
||||||
|
else
|
||||||
|
item.destroy
|
||||||
|
end
|
||||||
|
when Comment
|
||||||
|
if item.author.local?
|
||||||
|
item.author.owner.retract(comment)
|
||||||
|
elsif item.parent.author.local?
|
||||||
|
item.parent.author.owner.retract(comment)
|
||||||
|
else
|
||||||
|
item.destroy
|
||||||
|
end
|
||||||
end
|
end
|
||||||
mark_as_reviewed
|
mark_as_reviewed
|
||||||
end
|
end
|
||||||
|
|
||||||
def delete_post
|
|
||||||
if post = Post.where(id: item_id).first
|
|
||||||
if post.author.local?
|
|
||||||
post.author.owner.retract(post)
|
|
||||||
else
|
|
||||||
post.destroy
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def delete_comment
|
|
||||||
if comment = Comment.where(id: item_id).first
|
|
||||||
if comment.author.local?
|
|
||||||
comment.author.owner.retract(comment)
|
|
||||||
elsif comment.parent.author.local?
|
|
||||||
comment.parent.author.owner.retract(comment)
|
|
||||||
else
|
|
||||||
comment.destroy
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def mark_as_reviewed
|
def mark_as_reviewed
|
||||||
Report.where(item_id: item_id, item_type: item_type).update_all(reviewed: true)
|
Report.where(item_id: item_id, item_type: item_type).update_all(reviewed: true)
|
||||||
|
|
|
||||||
|
|
@ -10,27 +10,24 @@
|
||||||
#reports
|
#reports
|
||||||
%h1
|
%h1
|
||||||
= t("report.title")
|
= t("report.title")
|
||||||
- @reports.each do |r|
|
- @reports.each do |report|
|
||||||
.panel.panel-default
|
.panel.panel-default
|
||||||
- username = User.find_by_id(r.user_id).username
|
- username = report.user.username
|
||||||
.panel-heading
|
.panel-heading
|
||||||
.reporter.pull-right
|
.reporter.pull-right
|
||||||
= raw t("report.reported_label", person: link_to(username, user_profile_path(username)))
|
= raw t("report.reported_label", person: link_to(username, user_profile_path(username)))
|
||||||
.title
|
.title
|
||||||
= report_content(r.item_id, r.item_type)
|
= report_content(report)
|
||||||
.panel-body
|
.panel-body
|
||||||
.reason
|
.reason
|
||||||
= t("report.reason_label", text: r.text)
|
= t("report.reason_label", text: report.text)
|
||||||
|
|
||||||
= button_to t("report.reported_user_details"),
|
= button_to t("report.reported_user_details"),
|
||||||
user_search_path(admins_controller_user_search: {guid: get_reported_guid(r.item_id,
|
user_search_path(admins_controller_user_search: {guid: report.reported_author.guid}),
|
||||||
r.item_type)}), class: "btn pull-left btn-info btn-small",
|
class: "btn pull-left btn-info btn-small", method: :post
|
||||||
method: :post
|
= button_to t("report.review_link"), report_path(report.id, type: report.item_type),
|
||||||
= button_to t("report.review_link"), report_path(r.id, type: r.item_type),
|
class: "btn pull-left btn-info btn-small", method: :put
|
||||||
class: "btn pull-left btn-info btn-small",
|
= button_to t("report.delete_link"), report_path(report.id, type: report.item_type),
|
||||||
method: :put
|
|
||||||
= button_to t("report.delete_link"), report_path(r.id, type: r.item_type),
|
|
||||||
data: {confirm: t("report.confirm_deletion")},
|
data: {confirm: t("report.confirm_deletion")},
|
||||||
class: "btn pull-right btn-danger btn-small",
|
class: "btn pull-right btn-danger btn-small", method: :delete
|
||||||
method: :delete
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,27 @@
|
||||||
require 'spec_helper'
|
require "spec_helper"
|
||||||
|
|
||||||
describe ReportHelper, :type => :helper do
|
describe ReportHelper, type: :helper do
|
||||||
before do
|
before do
|
||||||
@comment = FactoryGirl.create(:comment)
|
@user = bob
|
||||||
@post = @comment.post
|
@post = @user.post(:status_message, text: "hello", to: @user.aspects.first.id)
|
||||||
|
@comment = @user.comment!(@post, "welcome")
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "#get_reported_guid" do
|
||||||
|
it "returns user guid from post" do
|
||||||
|
expect(helper.get_reported_guid(@post, "post")) == @user.guid
|
||||||
|
end
|
||||||
|
it "returns user guid from comment" do
|
||||||
|
expect(helper.get_reported_guid(@comment, "comment")) == @user.guid
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "#report_content" do
|
describe "#report_content" do
|
||||||
it "contains a link to the post" do
|
it "contains a link to the post" do
|
||||||
expect(helper.report_content(@post, 'post')).to include %Q(href="#{post_path(@post)}")
|
expect(helper.report_content(@post, "post")).to include %(href="#{post_path(@post)}")
|
||||||
end
|
end
|
||||||
it "contains an anchor to the comment" do
|
it "contains an anchor to the comment" do
|
||||||
expect(helper.report_content(@comment, 'comment')).to include %Q(href="#{post_path(@post, anchor: @comment.guid)}")
|
expect(helper.report_content(@comment, "comment")).to include %(href="#{post_path(@post, anchor: @comment.guid)}")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue