added post flag test
This commit is contained in:
@@ -216,7 +216,7 @@ class Post < ActiveRecord::Base
|
||||
|
||||
module ApprovalMethods
|
||||
def is_approvable?
|
||||
(is_pending? || is_flagged?) && approver_string != "approver:#{CurrentUser.name}"
|
||||
(is_pending? || is_flagged? || is_deleted?) && approver_string != "approver:#{CurrentUser.name}"
|
||||
end
|
||||
|
||||
def flag!(reason)
|
||||
@@ -240,8 +240,10 @@ class Post < ActiveRecord::Base
|
||||
def approve!
|
||||
raise ApprovalError.new("You have previously approved this post and cannot approve it again") if approver_string == "approver:#{CurrentUser.name}"
|
||||
|
||||
flags.each {|x| x.resolve!}
|
||||
self.is_flagged = false
|
||||
self.is_pending = false
|
||||
self.is_deleted = false
|
||||
self.approver_string = "approver:#{CurrentUser.name}"
|
||||
save!
|
||||
end
|
||||
|
||||
@@ -7,15 +7,17 @@ class PostFlag < ActiveRecord::Base
|
||||
validate :validate_creator_is_not_limited
|
||||
validate :validate_post_is_active
|
||||
before_validation :initialize_creator, :on => :create
|
||||
validates_uniqueness_of :creator_id, :scope => :post_id
|
||||
validates_uniqueness_of :creator_id, :scope => :post_id, :message => "has already flagged this post"
|
||||
before_save :update_post
|
||||
scope :resolved, where(["is_resolved = ?", true])
|
||||
scope :unresolved, where(["is_resolved = ?", false])
|
||||
|
||||
def update_post
|
||||
post.update_attribute(:is_flagged, true)
|
||||
end
|
||||
|
||||
def validate_creator_is_not_limited
|
||||
if PostAppeal.for_user(creator_id).recent.count >= 10
|
||||
if flag_count_for_creator >= 10
|
||||
errors[:creator] << "can flag 10 posts a day"
|
||||
false
|
||||
else
|
||||
@@ -36,4 +38,12 @@ class PostFlag < ActiveRecord::Base
|
||||
self.creator_id = CurrentUser.id
|
||||
self.creator_ip_addr = CurrentUser.ip_addr
|
||||
end
|
||||
|
||||
def resolve!
|
||||
update_attribute(:is_resolved, true)
|
||||
end
|
||||
|
||||
def flag_count_for_creator
|
||||
PostAppeal.for_user(creator_id).recent.count
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
<% if post.is_flagged? %>
|
||||
<% if (post.is_flagged? || post.is_deleted?) && post.flags.any? %>
|
||||
<div class="ui-corner-all ui-state-error notice">
|
||||
<span class="ui-icon ui-icon-alert"></span>
|
||||
This post has been flagged for deletion: <%= post_flag_reasons(post) %>
|
||||
|
||||
@@ -1013,6 +1013,40 @@ CREATE SEQUENCE pools_id_seq
|
||||
ALTER SEQUENCE pools_id_seq OWNED BY pools.id;
|
||||
|
||||
|
||||
--
|
||||
-- Name: post_appeals; Type: TABLE; Schema: public; Owner: -; Tablespace:
|
||||
--
|
||||
|
||||
CREATE TABLE post_appeals (
|
||||
id integer NOT NULL,
|
||||
post_id integer NOT NULL,
|
||||
creator_id integer NOT NULL,
|
||||
creator_ip_addr integer NOT NULL,
|
||||
reason text,
|
||||
created_at timestamp without time zone,
|
||||
updated_at timestamp without time zone
|
||||
);
|
||||
|
||||
|
||||
--
|
||||
-- Name: post_appeals_id_seq; Type: SEQUENCE; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
CREATE SEQUENCE post_appeals_id_seq
|
||||
START WITH 1
|
||||
INCREMENT BY 1
|
||||
NO MAXVALUE
|
||||
NO MINVALUE
|
||||
CACHE 1;
|
||||
|
||||
|
||||
--
|
||||
-- Name: post_appeals_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
ALTER SEQUENCE post_appeals_id_seq OWNED BY post_appeals.id;
|
||||
|
||||
|
||||
--
|
||||
-- Name: post_disapprovals; Type: TABLE; Schema: public; Owner: -; Tablespace:
|
||||
--
|
||||
@@ -1045,6 +1079,40 @@ CREATE SEQUENCE post_disapprovals_id_seq
|
||||
ALTER SEQUENCE post_disapprovals_id_seq OWNED BY post_disapprovals.id;
|
||||
|
||||
|
||||
--
|
||||
-- Name: post_flags; Type: TABLE; Schema: public; Owner: -; Tablespace:
|
||||
--
|
||||
|
||||
CREATE TABLE post_flags (
|
||||
id integer NOT NULL,
|
||||
post_id integer NOT NULL,
|
||||
creator_id integer NOT NULL,
|
||||
creator_ip_addr inet NOT NULL,
|
||||
reason text,
|
||||
created_at timestamp without time zone,
|
||||
updated_at timestamp without time zone
|
||||
);
|
||||
|
||||
|
||||
--
|
||||
-- Name: post_flags_id_seq; Type: SEQUENCE; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
CREATE SEQUENCE post_flags_id_seq
|
||||
START WITH 1
|
||||
INCREMENT BY 1
|
||||
NO MAXVALUE
|
||||
NO MINVALUE
|
||||
CACHE 1;
|
||||
|
||||
|
||||
--
|
||||
-- Name: post_flags_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
ALTER SEQUENCE post_flags_id_seq OWNED BY post_flags.id;
|
||||
|
||||
|
||||
--
|
||||
-- Name: post_versions; Type: TABLE; Schema: public; Owner: -; Tablespace:
|
||||
--
|
||||
@@ -1750,6 +1818,13 @@ ALTER TABLE pool_versions ALTER COLUMN id SET DEFAULT nextval('pool_versions_id_
|
||||
ALTER TABLE pools ALTER COLUMN id SET DEFAULT nextval('pools_id_seq'::regclass);
|
||||
|
||||
|
||||
--
|
||||
-- Name: id; Type: DEFAULT; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
ALTER TABLE post_appeals ALTER COLUMN id SET DEFAULT nextval('post_appeals_id_seq'::regclass);
|
||||
|
||||
|
||||
--
|
||||
-- Name: id; Type: DEFAULT; Schema: public; Owner: -
|
||||
--
|
||||
@@ -1757,6 +1832,13 @@ ALTER TABLE pools ALTER COLUMN id SET DEFAULT nextval('pools_id_seq'::regclass);
|
||||
ALTER TABLE post_disapprovals ALTER COLUMN id SET DEFAULT nextval('post_disapprovals_id_seq'::regclass);
|
||||
|
||||
|
||||
--
|
||||
-- Name: id; Type: DEFAULT; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
ALTER TABLE post_flags ALTER COLUMN id SET DEFAULT nextval('post_flags_id_seq'::regclass);
|
||||
|
||||
|
||||
--
|
||||
-- Name: id; Type: DEFAULT; Schema: public; Owner: -
|
||||
--
|
||||
@@ -2072,6 +2154,14 @@ ALTER TABLE ONLY pools
|
||||
ADD CONSTRAINT pools_pkey PRIMARY KEY (id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: post_appeals_pkey; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace:
|
||||
--
|
||||
|
||||
ALTER TABLE ONLY post_appeals
|
||||
ADD CONSTRAINT post_appeals_pkey PRIMARY KEY (id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: post_disapprovals_pkey; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace:
|
||||
--
|
||||
@@ -2080,6 +2170,14 @@ ALTER TABLE ONLY post_disapprovals
|
||||
ADD CONSTRAINT post_disapprovals_pkey PRIMARY KEY (id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: post_flags_pkey; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace:
|
||||
--
|
||||
|
||||
ALTER TABLE ONLY post_flags
|
||||
ADD CONSTRAINT post_flags_pkey PRIMARY KEY (id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: post_versions_pkey; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace:
|
||||
--
|
||||
@@ -2639,6 +2737,27 @@ CREATE INDEX index_pools_on_creator_id ON pools USING btree (creator_id);
|
||||
CREATE INDEX index_pools_on_name ON pools USING btree (name);
|
||||
|
||||
|
||||
--
|
||||
-- Name: index_post_appeals_on_creator_id; Type: INDEX; Schema: public; Owner: -; Tablespace:
|
||||
--
|
||||
|
||||
CREATE INDEX index_post_appeals_on_creator_id ON post_appeals USING btree (creator_id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: index_post_appeals_on_creator_ip_addr; Type: INDEX; Schema: public; Owner: -; Tablespace:
|
||||
--
|
||||
|
||||
CREATE INDEX index_post_appeals_on_creator_ip_addr ON post_appeals USING btree (creator_ip_addr);
|
||||
|
||||
|
||||
--
|
||||
-- Name: index_post_appeals_on_post_id; Type: INDEX; Schema: public; Owner: -; Tablespace:
|
||||
--
|
||||
|
||||
CREATE INDEX index_post_appeals_on_post_id ON post_appeals USING btree (post_id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: index_post_disapprovals_on_post_id; Type: INDEX; Schema: public; Owner: -; Tablespace:
|
||||
--
|
||||
@@ -2653,6 +2772,27 @@ CREATE INDEX index_post_disapprovals_on_post_id ON post_disapprovals USING btree
|
||||
CREATE INDEX index_post_disapprovals_on_user_id ON post_disapprovals USING btree (user_id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: index_post_flags_on_creator_id; Type: INDEX; Schema: public; Owner: -; Tablespace:
|
||||
--
|
||||
|
||||
CREATE INDEX index_post_flags_on_creator_id ON post_flags USING btree (creator_id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: index_post_flags_on_creator_ip_addr; Type: INDEX; Schema: public; Owner: -; Tablespace:
|
||||
--
|
||||
|
||||
CREATE INDEX index_post_flags_on_creator_ip_addr ON post_flags USING btree (creator_ip_addr);
|
||||
|
||||
|
||||
--
|
||||
-- Name: index_post_flags_on_post_id; Type: INDEX; Schema: public; Owner: -; Tablespace:
|
||||
--
|
||||
|
||||
CREATE INDEX index_post_flags_on_post_id ON post_flags USING btree (post_id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: index_post_versions_on_post_id; Type: INDEX; Schema: public; Owner: -; Tablespace:
|
||||
--
|
||||
@@ -3009,4 +3149,8 @@ INSERT INTO schema_migrations (version) VALUES ('20100309211553');
|
||||
|
||||
INSERT INTO schema_migrations (version) VALUES ('20100318213503');
|
||||
|
||||
INSERT INTO schema_migrations (version) VALUES ('20100826232512');
|
||||
INSERT INTO schema_migrations (version) VALUES ('20100826232512');
|
||||
|
||||
INSERT INTO schema_migrations (version) VALUES ('20110328215652');
|
||||
|
||||
INSERT INTO schema_migrations (version) VALUES ('20110328215701');
|
||||
@@ -5,6 +5,7 @@ class CreatePostFlags < ActiveRecord::Migration
|
||||
t.column :creator_id, :integer, :null => false
|
||||
t.column :creator_ip_addr, :inet, :null => false
|
||||
t.column :reason, :text
|
||||
t.column :is_resolved, :boolean, :null => false, :default => false
|
||||
t.timestamps
|
||||
end
|
||||
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
require 'test_helper'
|
||||
|
||||
class Admin::UsersHelperTest < ActionView::TestCase
|
||||
end
|
||||
@@ -1,4 +0,0 @@
|
||||
require 'test_helper'
|
||||
|
||||
class AdvertisementsHelperTest < ActionView::TestCase
|
||||
end
|
||||
@@ -1,4 +0,0 @@
|
||||
require 'test_helper'
|
||||
|
||||
class ArtistVersionsHelperTest < ActionView::TestCase
|
||||
end
|
||||
@@ -1,4 +0,0 @@
|
||||
require 'test_helper'
|
||||
|
||||
class ArtistsHelperTest < ActionView::TestCase
|
||||
end
|
||||
@@ -1,4 +0,0 @@
|
||||
require 'test_helper'
|
||||
|
||||
class BansHelperTest < ActionView::TestCase
|
||||
end
|
||||
@@ -1,4 +0,0 @@
|
||||
require 'test_helper'
|
||||
|
||||
class CommentVotesHelperTest < ActionView::TestCase
|
||||
end
|
||||
@@ -1,4 +0,0 @@
|
||||
require 'test_helper'
|
||||
|
||||
class CommentsHelperTest < ActionView::TestCase
|
||||
end
|
||||
@@ -1,4 +0,0 @@
|
||||
require 'test_helper'
|
||||
|
||||
class DmailsHelperTest < ActionView::TestCase
|
||||
end
|
||||
@@ -1,4 +0,0 @@
|
||||
require 'test_helper'
|
||||
|
||||
class FavoritesHelperTest < ActionView::TestCase
|
||||
end
|
||||
@@ -1,4 +0,0 @@
|
||||
require 'test_helper'
|
||||
|
||||
class ForumPostsHelperTest < ActionView::TestCase
|
||||
end
|
||||
@@ -1,4 +0,0 @@
|
||||
require 'test_helper'
|
||||
|
||||
class ForumTopicsHelperTest < ActionView::TestCase
|
||||
end
|
||||
@@ -1,4 +0,0 @@
|
||||
require 'test_helper'
|
||||
|
||||
class JanitorTrialsHelperTest < ActionView::TestCase
|
||||
end
|
||||
@@ -1,4 +0,0 @@
|
||||
require 'test_helper'
|
||||
|
||||
class NotesHelperTest < ActionView::TestCase
|
||||
end
|
||||
@@ -1,4 +0,0 @@
|
||||
require 'test_helper'
|
||||
|
||||
class PoolVersionsHelperTest < ActionView::TestCase
|
||||
end
|
||||
@@ -1,4 +0,0 @@
|
||||
require 'test_helper'
|
||||
|
||||
class PoolsHelperTest < ActionView::TestCase
|
||||
end
|
||||
@@ -1,4 +0,0 @@
|
||||
require 'test_helper'
|
||||
|
||||
class PostHelperTest < ActionView::TestCase
|
||||
end
|
||||
@@ -1,4 +0,0 @@
|
||||
require 'test_helper'
|
||||
|
||||
class PostModerationDetailsHelperTest < ActionView::TestCase
|
||||
end
|
||||
@@ -1,4 +0,0 @@
|
||||
require 'test_helper'
|
||||
|
||||
class PostVersionsHelperTest < ActionView::TestCase
|
||||
end
|
||||
@@ -1,4 +0,0 @@
|
||||
require 'test_helper'
|
||||
|
||||
class PostVotesHelperTest < ActionView::TestCase
|
||||
end
|
||||
@@ -1,4 +0,0 @@
|
||||
require 'test_helper'
|
||||
|
||||
class SessionsHelperTest < ActionView::TestCase
|
||||
end
|
||||
@@ -1,4 +0,0 @@
|
||||
require 'test_helper'
|
||||
|
||||
class TagAliasesHelperTest < ActionView::TestCase
|
||||
end
|
||||
@@ -1,4 +0,0 @@
|
||||
require 'test_helper'
|
||||
|
||||
class TagImplicationsHelperTest < ActionView::TestCase
|
||||
end
|
||||
@@ -1,4 +0,0 @@
|
||||
require 'test_helper'
|
||||
|
||||
class TagSubscriptionsHelperTest < ActionView::TestCase
|
||||
end
|
||||
@@ -1,4 +0,0 @@
|
||||
require 'test_helper'
|
||||
|
||||
class TagsHelperTest < ActionView::TestCase
|
||||
end
|
||||
@@ -1,4 +0,0 @@
|
||||
require 'test_helper'
|
||||
|
||||
class UnapprovalsHelperTest < ActionView::TestCase
|
||||
end
|
||||
@@ -1,4 +0,0 @@
|
||||
require 'test_helper'
|
||||
|
||||
class UploadsHelperTest < ActionView::TestCase
|
||||
end
|
||||
@@ -1,4 +0,0 @@
|
||||
require 'test_helper'
|
||||
|
||||
class UserFeedbackHelperTest < ActionView::TestCase
|
||||
end
|
||||
@@ -1,4 +0,0 @@
|
||||
require 'test_helper'
|
||||
|
||||
class UsersHelperTest < ActionView::TestCase
|
||||
end
|
||||
@@ -1,4 +0,0 @@
|
||||
require 'test_helper'
|
||||
|
||||
class WikiPageVersionsHelperTest < ActionView::TestCase
|
||||
end
|
||||
@@ -1,4 +0,0 @@
|
||||
require 'test_helper'
|
||||
|
||||
class WikiPagesHelperTest < ActionView::TestCase
|
||||
end
|
||||
@@ -1,8 +1,57 @@
|
||||
require 'test_helper'
|
||||
|
||||
class PostFlagTest < ActiveSupport::TestCase
|
||||
# Replace this with your real tests.
|
||||
test "the truth" do
|
||||
assert true
|
||||
context "In all cases" do
|
||||
setup do
|
||||
@alice = Factory.create(:user)
|
||||
CurrentUser.user = @alice
|
||||
CurrentUser.ip_addr = "127.0.0.1"
|
||||
MEMCACHE.flush_all
|
||||
end
|
||||
|
||||
teardown do
|
||||
CurrentUser.user = nil
|
||||
CurrentUser.ip_addr = nil
|
||||
end
|
||||
|
||||
context "a user" do
|
||||
setup do
|
||||
@post = Factory.create(:post, :tag_string => "aaa")
|
||||
end
|
||||
|
||||
should "not be able to flag a post more than twice" do
|
||||
assert_difference("PostFlag.count", 1) do
|
||||
@post_flag = PostFlag.create(:post => @post, :reason => "aaa")
|
||||
end
|
||||
|
||||
assert_difference("PostFlag.count", 0) do
|
||||
@post_flag = PostFlag.create(:post => @post, :reason => "aaa")
|
||||
end
|
||||
|
||||
assert_equal(["Creator has already flagged this post"], @post_flag.errors.full_messages)
|
||||
end
|
||||
|
||||
should "not be able to flag more than 10 posts in 24 hours" do
|
||||
@post_flag = PostFlag.new(:post => @post, :reason => "aaa")
|
||||
@post_flag.expects(:flag_count_for_creator).returns(10)
|
||||
assert_difference("PostFlag.count", 0) do
|
||||
@post_flag.save
|
||||
end
|
||||
assert_equal(["Creator can flag 10 posts a day"], @post_flag.errors.full_messages)
|
||||
end
|
||||
|
||||
should "not be able to flag a deleted post" do
|
||||
@post.update_attribute(:is_deleted, true)
|
||||
assert_difference("PostFlag.count", 0) do
|
||||
@post_flag = PostFlag.create(:post => @post, :reason => "aaa")
|
||||
end
|
||||
assert_equal(["Post is deleted"], @post_flag.errors.full_messages)
|
||||
end
|
||||
|
||||
should "initialize its creator" do
|
||||
@post_flag = PostFlag.create(:post => @post, :reason => "aaa")
|
||||
assert_equal(@alice.id, @post_flag.creator_id)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user