From 997a7c676e7ca13e143daa18b17b36f12f95a71a Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Tue, 18 Feb 2014 15:18:31 -0500 Subject: [PATCH] If flagging a topic with only one post, flag the post instead --- app/models/post_action.rb | 7 ++++++- spec/models/post_action_spec.rb | 15 +++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/app/models/post_action.rb b/app/models/post_action.rb index 9f15a7f31..ce878595e 100644 --- a/app/models/post_action.rb +++ b/app/models/post_action.rb @@ -124,13 +124,18 @@ class PostAction < ActiveRecord::Base related_post_id = create_message_for_post_action(user,post,post_action_type_id,opts) + targets_topic = if opts[:flag_topic] and post.topic + post.topic.reload + post.topic.posts_count != 1 + end + create( post_id: post.id, user_id: user.id, post_action_type_id: post_action_type_id, message: opts[:message], staff_took_action: opts[:take_action] || false, related_post_id: related_post_id, - targets_topic: !!opts[:flag_topic] ) + targets_topic: !!targets_topic ) rescue ActiveRecord::RecordNotUnique # can happen despite being .create # since already bookmarked diff --git a/spec/models/post_action_spec.rb b/spec/models/post_action_spec.rb index f26f0689d..94f15074b 100644 --- a/spec/models/post_action_spec.rb +++ b/spec/models/post_action_spec.rb @@ -269,6 +269,21 @@ describe PostAction do post.hidden.should be_true post.hidden_reason_id.should == Post.hidden_reasons[:flag_threshold_reached_again] end + + it "can flag the topic instead of a post" do + post1 = create_post + post2 = create_post(topic: post1.topic) + post_action = PostAction.act(Fabricate(:user), post1, PostActionType.types[:spam], {flag_topic: true}) + post_action.targets_topic.should == true + end + + it "will flag the first post if you flag a topic but there is only one post in the topic" do + post = create_post + post_action = PostAction.act(Fabricate(:user), post, PostActionType.types[:spam], {flag_topic: true}) + post_action.targets_topic.should == false + post_action.post_id.should == post.id + end + end it "prevents user to act twice at the same time" do