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:
Lukas Matt 2015-09-03 17:43:56 +02:00
parent d1e3e568b9
commit 95072d6010
5 changed files with 64 additions and 62 deletions

View file

@ -1,5 +1,4 @@
#reports {
padding-top: 2em;
.reason {
padding-bottom: 20px;
}

View file

@ -3,22 +3,15 @@
# the COPYRIGHT file.
module ReportHelper
def get_reported_guid(id, type)
if type == "post"
Post.where(id: id).first.author.guid
elsif type == "comment"
Comment.where(id: id).first.author.guid
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
def report_content(report)
case (item = report.item)
when Post
raw t("report.post_label", title: link_to(post_page_title(item), post_path(item.id)))
when Comment
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
raw t("report.not_found")
end

View file

@ -1,4 +1,6 @@
class Report < ActiveRecord::Base
POST, COMMENT = %w(post comment).map(&:freeze)
validates :user_id, presence: true
validates :item_id, presence: true
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
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
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.'
@ -27,35 +41,24 @@ class Report < ActiveRecord::Base
end
def destroy_reported_item
if item_type == 'post'
delete_post
elsif item_type == 'comment'
delete_comment
case item
when Post
if item.author.local?
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
mark_as_reviewed
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
Report.where(item_id: item_id, item_type: item_type).update_all(reviewed: true)

View file

@ -10,27 +10,24 @@
#reports
%h1
= t("report.title")
- @reports.each do |r|
- @reports.each do |report|
.panel.panel-default
- username = User.find_by_id(r.user_id).username
- username = report.user.username
.panel-heading
.reporter.pull-right
= raw t("report.reported_label", person: link_to(username, user_profile_path(username)))
.title
= report_content(r.item_id, r.item_type)
= report_content(report)
.panel-body
.reason
= t("report.reason_label", text: r.text)
= t("report.reason_label", text: report.text)
= button_to t("report.reported_user_details"),
user_search_path(admins_controller_user_search: {guid: get_reported_guid(r.item_id,
r.item_type)}), class: "btn pull-left btn-info btn-small",
method: :post
= button_to t("report.review_link"), report_path(r.id, type: r.item_type),
class: "btn pull-left btn-info btn-small",
method: :put
= button_to t("report.delete_link"), report_path(r.id, type: r.item_type),
user_search_path(admins_controller_user_search: {guid: report.reported_author.guid}),
class: "btn pull-left btn-info btn-small", method: :post
= button_to t("report.review_link"), report_path(report.id, type: report.item_type),
class: "btn pull-left btn-info btn-small", method: :put
= button_to t("report.delete_link"), report_path(report.id, type: report.item_type),
data: {confirm: t("report.confirm_deletion")},
class: "btn pull-right btn-danger btn-small",
method: :delete
class: "btn pull-right btn-danger btn-small", method: :delete

View file

@ -1,17 +1,27 @@
require 'spec_helper'
require "spec_helper"
describe ReportHelper, :type => :helper do
describe ReportHelper, type: :helper do
before do
@comment = FactoryGirl.create(:comment)
@post = @comment.post
@user = bob
@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
describe "#report_content" 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
it "contains an anchor to the comment" do
expect(helper.report_content(@comment, 'comment')).to include %Q(href="#{post_path(@post, anchor: @comment.guid)}")
it "contains an anchor to the comment" do
expect(helper.report_content(@comment, "comment")).to include %(href="#{post_path(@post, anchor: @comment.guid)}")
end
end
end