Use polymorphic association for the report item

* Adopt pronto suggestions

Signed-off-by: Lukas Matt <lukas@zauberstuhl.de>
This commit is contained in:
Lukas Matt 2015-10-03 16:52:50 +02:00
parent 97ee2cd975
commit 78f9b39e55
12 changed files with 41 additions and 41 deletions

View file

@ -13,7 +13,7 @@
<i class="entypo-trash"></i> <i class="entypo-trash"></i>
<a/> <a/>
{{else}} {{else}}
<a href="#" data-type="comment" class="comment_report" title="{{t "report.name"}}"> <a href="#" data-type="Comment" class="comment_report" title="{{t "report.name"}}">
<i class="entypo-warning"></i> <i class="entypo-warning"></i>
</a> </a>
{{/if}} {{/if}}

View file

@ -7,7 +7,7 @@
<i class="entypo-trash"></i> <i class="entypo-trash"></i>
</a> </a>
{{else}} {{else}}
<a href="#" rel="nofollow" data-type="post" class="post_report" title="{{t "report.name"}}"> <a href="#" rel="nofollow" data-type="Post" class="post_report" title="{{t "report.name"}}">
<i class="entypo-warning"></i> <i class="entypo-warning"></i>
</a> </a>
<a href="#" rel="nofollow" class="block_user" title="{{t "ignore"}}"> <a href="#" rel="nofollow" class="block_user" title="{{t "ignore"}}">

View file

@ -5,7 +5,7 @@
<i class="entypo-trash"></i> <i class="entypo-trash"></i>
</a> </a>
{{else}} {{else}}
<a href="#" data-type="post" class="post_report" title="{{t "report.name"}}"> <a href="#" data-type="Post" class="post_report" title="{{t "report.name"}}">
<i class="entypo-warning"></i> <i class="entypo-warning"></i>
</a> </a>
<a href="#" data-type="post" class="block_user" title="{{t "ignore"}}"> <a href="#" data-type="post" class="block_user" title="{{t "ignore"}}">

View file

@ -13,7 +13,7 @@
<i class="entypo-trash"></i> <i class="entypo-trash"></i>
</a> </a>
{{else}} {{else}}
<a href="#" rel="nofollow" data-type="post" class="post_report" title="{{t "report.name"}}"> <a href="#" rel="nofollow" data-type="Post" class="post_report" title="{{t "report.name"}}">
<i class="entypo-warning"></i> <i class="entypo-warning"></i>
</a> </a>
<a href="#" rel="nofollow" class="block_user" title="{{t "ignore"}}"> <a href="#" rel="nofollow" class="block_user" title="{{t "ignore"}}">

View file

@ -30,6 +30,8 @@ class Comment < ActiveRecord::Base
validates :text, :presence => true, :length => {:maximum => 65535} validates :text, :presence => true, :length => {:maximum => 65535}
validates :parent, :presence => true #should be in relayable (pending on fixing Message) validates :parent, :presence => true #should be in relayable (pending on fixing Message)
has_many :reports, as: :item
scope :including_author, -> { includes(:author => :profile) } scope :including_author, -> { includes(:author => :profile) }
scope :for_a_stream, -> { including_author.merge(order('created_at ASC')) } scope :for_a_stream, -> { including_author.merge(order('created_at ASC')) }

View file

@ -17,6 +17,8 @@ class Post < ActiveRecord::Base
xml_attr :provider_display_name xml_attr :provider_display_name
has_many :reports, as: :item
has_many :mentions, :dependent => :destroy has_many :mentions, :dependent => :destroy
has_many :reshares, :class_name => "Reshare", :foreign_key => :root_guid, :primary_key => :guid has_many :reshares, :class_name => "Reshare", :foreign_key => :root_guid, :primary_key => :guid

View file

@ -1,10 +1,8 @@
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: {
:message => 'Type should match `post` or `comment`!'} in: %w(Post Comment), message: "Type should match `Post` or `Comment`!"}
validates :text, presence: true validates :text, presence: true
validate :entry_does_not_exist, :on => :create validate :entry_does_not_exist, :on => :create
@ -13,17 +11,10 @@ class Report < ActiveRecord::Base
belongs_to :user belongs_to :user
belongs_to :post belongs_to :post
belongs_to :comment belongs_to :comment
belongs_to :item, polymorphic: true
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 def reported_author
item.author if item item.author if item
end end

View file

@ -0,0 +1,7 @@
class UpdateReportItemTypes < ActiveRecord::Migration
def change
Report.all.each do |report|
report.update_attribute :item_type, report[:item_type].capitalize
end
end
end

View file

@ -11,7 +11,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20150828132451) do ActiveRecord::Schema.define(version: 20151003142048) do
create_table "account_deletions", force: :cascade do |t| create_table "account_deletions", force: :cascade do |t|
t.string "diaspora_handle", limit: 255 t.string "diaspora_handle", limit: 255

View file

@ -49,16 +49,16 @@ describe ReportController, type: :controller do
context "report offensive post" do context "report offensive post" do
it "succeeds" do it "succeeds" do
put :create, report: {item_id: @message.id, item_type: "post", text: "offensive content"} put :create, report: {item_id: @message.id, item_type: "Post", text: "offensive content"}
expect(response.status).to eq(200) expect(response.status).to eq(200)
expect(Report.exists?(item_id: @message.id, item_type: "post")).to be true expect(Report.exists?(item_id: @message.id, item_type: "Post")).to be true
end end
end end
context "report offensive comment" do context "report offensive comment" do
it "succeeds" do it "succeeds" do
put :create, report: {item_id: @comment.id, item_type: "comment", text: "offensive content"} put :create, report: {item_id: @comment.id, item_type: "Comment", text: "offensive content"}
expect(response.status).to eq(200) expect(response.status).to eq(200)
expect(Report.exists?(item_id: @comment.id, item_type: "comment")).to be true expect(Report.exists?(item_id: @comment.id, item_type: "Comment")).to be true
end end
end end
end end
@ -68,14 +68,14 @@ describe ReportController, type: :controller do
it "is behind redirect_unless_admin_or_moderator" do it "is behind redirect_unless_admin_or_moderator" do
put :update, id: @message.id, type: "post" put :update, id: @message.id, type: "post"
expect(response).to redirect_to stream_path expect(response).to redirect_to stream_path
expect(Report.where(reviewed: false, item_id: @message.id, item_type: "post")).to be_truthy expect(Report.where(reviewed: false, item_id: @message.id, item_type: "Post")).to be_truthy
end end
end end
context "mark comment report as user" do context "mark comment report as user" do
it "is behind redirect_unless_admin_or_moderator" do it "is behind redirect_unless_admin_or_moderator" do
put :update, id: @comment.id, type: "comment" put :update, id: @comment.id, type: "comment"
expect(response).to redirect_to stream_path expect(response).to redirect_to stream_path
expect(Report.where(reviewed: false, item_id: @comment.id, item_type: "comment")).to be_truthy expect(Report.where(reviewed: false, item_id: @comment.id, item_type: "Comment")).to be_truthy
end end
end end
@ -86,7 +86,7 @@ describe ReportController, type: :controller do
it "succeeds" do it "succeeds" do
put :update, id: @message.id, type: "post" put :update, id: @message.id, type: "post"
expect(response.status).to eq(302) expect(response.status).to eq(302)
expect(Report.where(reviewed: true, item_id: @message.id, item_type: "post")).to be_truthy expect(Report.where(reviewed: true, item_id: @message.id, item_type: "Post")).to be_truthy
end end
end end
context "mark comment report as admin" do context "mark comment report as admin" do
@ -96,7 +96,7 @@ describe ReportController, type: :controller do
it "succeeds" do it "succeeds" do
put :update, id: @comment.id, type: "comment" put :update, id: @comment.id, type: "comment"
expect(response.status).to eq(302) expect(response.status).to eq(302)
expect(Report.where(reviewed: true, item_id: @comment.id, item_type: "comment")).to be_truthy expect(Report.where(reviewed: true, item_id: @comment.id, item_type: "Comment")).to be_truthy
end end
end end
@ -108,7 +108,7 @@ describe ReportController, type: :controller do
it "succeeds" do it "succeeds" do
put :update, id: @message.id, type: "post" put :update, id: @message.id, type: "post"
expect(response.status).to eq(302) expect(response.status).to eq(302)
expect(Report.where(reviewed: true, item_id: @message.id, item_type: "post")).to be_truthy expect(Report.where(reviewed: true, item_id: @message.id, item_type: "Post")).to be_truthy
end end
end end
@ -119,7 +119,7 @@ describe ReportController, type: :controller do
it "succeeds" do it "succeeds" do
put :update, id: @comment.id, type: "comment" put :update, id: @comment.id, type: "comment"
expect(response.status).to eq(302) expect(response.status).to eq(302)
expect(Report.where(reviewed: true, item_id: @comment.id, item_type: "comment")).to be_truthy expect(Report.where(reviewed: true, item_id: @comment.id, item_type: "Comment")).to be_truthy
end end
end end
end end
@ -129,14 +129,14 @@ describe ReportController, type: :controller do
it "is behind redirect_unless_admin_or_moderator" do it "is behind redirect_unless_admin_or_moderator" do
delete :destroy, id: @message.id, type: "post" delete :destroy, id: @message.id, type: "post"
expect(response).to redirect_to stream_path expect(response).to redirect_to stream_path
expect(Report.where(reviewed: false, item_id: @message.id, item_type: "post")).to be_truthy expect(Report.where(reviewed: false, item_id: @message.id, item_type: "Post")).to be_truthy
end end
end end
context "destroy comment as user" do context "destroy comment as user" do
it "is behind redirect_unless_admin_or_moderator" do it "is behind redirect_unless_admin_or_moderator" do
delete :destroy, id: @comment.id, type: "comment" delete :destroy, id: @comment.id, type: "comment"
expect(response).to redirect_to stream_path expect(response).to redirect_to stream_path
expect(Report.where(reviewed: false, item_id: @comment.id, item_type: "comment")).to be_truthy expect(Report.where(reviewed: false, item_id: @comment.id, item_type: "Comment")).to be_truthy
end end
end end
@ -147,7 +147,7 @@ describe ReportController, type: :controller do
it "succeeds" do it "succeeds" do
delete :destroy, id: @message.id, type: "post" delete :destroy, id: @message.id, type: "post"
expect(response.status).to eq(302) expect(response.status).to eq(302)
expect(Report.where(reviewed: true, item_id: @message.id, item_type: "post")).to be_truthy expect(Report.where(reviewed: true, item_id: @message.id, item_type: "Post")).to be_truthy
end end
end end
context "destroy comment as admin" do context "destroy comment as admin" do
@ -157,7 +157,7 @@ describe ReportController, type: :controller do
it "succeeds" do it "succeeds" do
delete :destroy, id: @comment.id, type: "comment" delete :destroy, id: @comment.id, type: "comment"
expect(response.status).to eq(302) expect(response.status).to eq(302)
expect(Report.where(reviewed: true, item_id: @comment.id, item_type: "comment")).to be_truthy expect(Report.where(reviewed: true, item_id: @comment.id, item_type: "Comment")).to be_truthy
end end
end end
@ -168,7 +168,7 @@ describe ReportController, type: :controller do
it "succeeds" do it "succeeds" do
delete :destroy, id: @message.id, type: "post" delete :destroy, id: @message.id, type: "post"
expect(response.status).to eq(302) expect(response.status).to eq(302)
expect(Report.where(reviewed: true, item_id: @message.id, item_type: "post")).to be_truthy expect(Report.where(reviewed: true, item_id: @message.id, item_type: "Post")).to be_truthy
end end
end end
context "destroy comment as moderator" do context "destroy comment as moderator" do
@ -178,7 +178,7 @@ describe ReportController, type: :controller do
it "succeeds" do it "succeeds" do
delete :destroy, id: @comment.id, type: "comment" delete :destroy, id: @comment.id, type: "comment"
expect(response.status).to eq(302) expect(response.status).to eq(302)
expect(Report.where(reviewed: true, item_id: @comment.id, item_type: "comment")).to be_truthy expect(Report.where(reviewed: true, item_id: @comment.id, item_type: "Comment")).to be_truthy
end end
end end
end end

View file

@ -7,11 +7,11 @@ describe ReportHelper, type: :helper do
@comment = @user.comment!(@post, "welcome") @comment = @user.comment!(@post, "welcome")
@post_report = @user.reports.create( @post_report = @user.reports.create(
item_id: @post.id, item_type: "post", item_id: @post.id, item_type: "Post",
text: "offensive content" text: "offensive content"
) )
@comment_report = @user.reports.create( @comment_report = @user.reports.create(
item_id: @comment.id, item_type: "comment", item_id: @comment.id, item_type: "Comment",
text: "offensive content" text: "offensive content"
) )
end end

View file

@ -12,14 +12,12 @@ describe Report, :type => :model do
@bob_comment = @user.comment!(@bob_post, "welcome") @bob_comment = @user.comment!(@bob_post, "welcome")
@valid_post_report = { @valid_post_report = {
:item_id => @bob_post.id, item_id: @bob_post.id, item_type: "Post",
:item_type => 'post', text: "offensive content"
:text => 'offensive content'
} }
@valid_comment_report = { @valid_comment_report = {
:item_id => @bob_comment.id, item_id: @bob_comment.id, item_type: "Comment",
:item_type => 'comment', text: "offensive content"
:text => 'offensive content'
} }
end end