stopgap measure for #1210
This commit is contained in:
@@ -526,12 +526,25 @@ class Post < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
|
|
||||||
module FavoriteMethods
|
module FavoriteMethods
|
||||||
|
def clean_fav_string?
|
||||||
|
rand(100) < [Math.log(fav_string.size, 2), 5].min
|
||||||
|
end
|
||||||
|
|
||||||
|
def clean_fav_string!
|
||||||
|
array = fav_string.scan(/\S+/).uniq
|
||||||
|
self.fav_string = array.join(" ")
|
||||||
|
self.fav_count = array.size
|
||||||
|
update_column(:fav_string, fav_string)
|
||||||
|
update_column(:fav_count, fav_count)
|
||||||
|
end
|
||||||
|
|
||||||
def favorited_by?(user_id)
|
def favorited_by?(user_id)
|
||||||
fav_string =~ /(?:\A| )fav:#{user_id}(?:\Z| )/
|
fav_string =~ /(?:\A| )fav:#{user_id}(?:\Z| )/
|
||||||
end
|
end
|
||||||
|
|
||||||
def append_user_to_fav_string(user_id)
|
def append_user_to_fav_string(user_id)
|
||||||
update_column(:fav_string, (fav_string + " fav:#{user_id}").strip)
|
update_column(:fav_string, (fav_string + " fav:#{user_id}").strip)
|
||||||
|
clean_fav_string! if clean_fav_string?
|
||||||
end
|
end
|
||||||
|
|
||||||
def add_favorite!(user)
|
def add_favorite!(user)
|
||||||
|
|||||||
@@ -204,8 +204,17 @@ class User < ActiveRecord::Base
|
|||||||
Favorite.where("user_id % 100 = #{id % 100} and user_id = #{id}").order("id desc")
|
Favorite.where("user_id % 100 = #{id % 100} and user_id = #{id}").order("id desc")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def clean_favorite_count?
|
||||||
|
favorite_count < 0 || rand(100) < [Math.log(favorite_count, 2), 5].min
|
||||||
|
end
|
||||||
|
|
||||||
|
def clean_favorite_count!
|
||||||
|
update_column(:favorite_count, Favorite.for_user(id).count)
|
||||||
|
end
|
||||||
|
|
||||||
def add_favorite!(post)
|
def add_favorite!(post)
|
||||||
Favorite.add(post, self)
|
Favorite.add(post, self)
|
||||||
|
clean_favorite_count! if clean_favorite_count?
|
||||||
end
|
end
|
||||||
|
|
||||||
def remove_favorite!(post)
|
def remove_favorite!(post)
|
||||||
|
|||||||
@@ -2058,7 +2058,8 @@ CREATE TABLE pool_versions (
|
|||||||
updater_id integer NOT NULL,
|
updater_id integer NOT NULL,
|
||||||
updater_ip_addr inet NOT NULL,
|
updater_ip_addr inet NOT NULL,
|
||||||
created_at timestamp without time zone NOT NULL,
|
created_at timestamp without time zone NOT NULL,
|
||||||
updated_at timestamp without time zone NOT NULL
|
updated_at timestamp without time zone NOT NULL,
|
||||||
|
name character varying(255)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
@@ -6410,3 +6411,5 @@ INSERT INTO schema_migrations (version) VALUES ('20130401013601');
|
|||||||
INSERT INTO schema_migrations (version) VALUES ('20130409191950');
|
INSERT INTO schema_migrations (version) VALUES ('20130409191950');
|
||||||
|
|
||||||
INSERT INTO schema_migrations (version) VALUES ('20130417221643');
|
INSERT INTO schema_migrations (version) VALUES ('20130417221643');
|
||||||
|
|
||||||
|
INSERT INTO schema_migrations (version) VALUES ('20130424121410');
|
||||||
@@ -741,6 +741,15 @@ class PostTest < ActiveSupport::TestCase
|
|||||||
CurrentUser.ip_addr = nil
|
CurrentUser.ip_addr = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
should "periodically clean the fav_string" do
|
||||||
|
@post.update_column(:fav_string, "fav:1 fav:1 fav:1")
|
||||||
|
@post.update_column(:fav_count, 3)
|
||||||
|
@post.stubs(:clean_fav_string?).returns(true)
|
||||||
|
@post.append_user_to_fav_string(2)
|
||||||
|
assert_equal("fav:1 fav:2", @post.fav_string)
|
||||||
|
assert_equal(2, @post.fav_count)
|
||||||
|
end
|
||||||
|
|
||||||
should "increment the user's favorite_count" do
|
should "increment the user's favorite_count" do
|
||||||
assert_difference("CurrentUser.favorite_count", 1) do
|
assert_difference("CurrentUser.favorite_count", 1) do
|
||||||
@post.add_favorite!(@user)
|
@post.add_favorite!(@user)
|
||||||
|
|||||||
@@ -14,6 +14,19 @@ class UserTest < ActiveSupport::TestCase
|
|||||||
CurrentUser.ip_addr = nil
|
CurrentUser.ip_addr = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "favoriting a post" do
|
||||||
|
setup do
|
||||||
|
@user.update_column(:favorite_count, 999)
|
||||||
|
@user.stubs(:clean_favorite_count?).returns(true)
|
||||||
|
@post = FactoryGirl.create(:post)
|
||||||
|
end
|
||||||
|
|
||||||
|
should "periodically clean the favorite_count" do
|
||||||
|
@user.add_favorite!(@post)
|
||||||
|
assert_equal(1, @user.favorite_count)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context "that has been invited by a mod" do
|
context "that has been invited by a mod" do
|
||||||
setup do
|
setup do
|
||||||
@mod = FactoryGirl.create(:moderator_user)
|
@mod = FactoryGirl.create(:moderator_user)
|
||||||
|
|||||||
Reference in New Issue
Block a user