added cached columns for post update count, post upload count, note update count, favorite count to users; fixed tests

This commit is contained in:
albert
2011-11-01 13:45:26 -04:00
parent ec362d79bf
commit cdc958d4f3
12 changed files with 126 additions and 26 deletions

View File

@@ -119,6 +119,12 @@ class AnonymousUser
def update_column(*params)
end
def increment!(field)
end
def decrement!(field)
end
def role
:anonymous
end

View File

@@ -58,6 +58,8 @@ class Note < ActiveRecord::Base
end
def create_version
CurrentUser.increment!(:note_update_count)
versions.create(
:updater_id => updater_id,
:updater_ip_addr => updater_ip_addr,

View File

@@ -995,6 +995,8 @@ class Post < ActiveRecord::Base
module VersionMethods
def create_version
CurrentUser.increment!(:post_update_count)
if created_at == updated_at
versions.create(
:rating => rating,

View File

@@ -69,6 +69,7 @@ class Upload < ActiveRecord::Base
post = convert_to_post
post.distribute_files
if post.save
CurrentUser.increment!(:post_upload_count)
update_attributes(:status => "completed", :post_id => post.id)
else
update_attribute(:status, "error: " + post.errors.full_messages.join(", "))

View File

@@ -182,12 +182,14 @@ class User < ActiveRecord::Base
def add_favorite!(post)
return if Favorite.exists?(:user_id => id, :post_id => post.id)
Favorite.create(:user_id => id, :post_id => post.id)
increment!(:favorite_count)
post.add_favorite!(self)
end
def remove_favorite!(post)
return unless Favorite.exists?(:user_id => id, :post_id => post.id)
Favorite.destroy_all(:user_id => id, :post_id => post.id)
decrement!(:favorite_count)
post.remove_favorite!(self)
end
end

View File

@@ -58,7 +58,7 @@ class UserPresenter
end
def uploads(template)
template.link_to(Post.for_user(user.id).count, template.posts_path(:tags => "uploader:#{user.name}"))
template.link_to(user.post_upload_count, template.posts_path(:tags => "uploader:#{user.name}"))
end
def deleted_uploads(template)
@@ -66,7 +66,7 @@ class UserPresenter
end
def favorites(template)
template.link_to(Favorite.for_user(user.id).count, template.favorites_path(:user_id => user.id))
template.link_to(user.favorite_count, template.favorites_path(:user_id => user.id))
end
def comments(template)
@@ -74,11 +74,11 @@ class UserPresenter
end
def post_versions(template)
template.link_to(PostVersion.for_user(user.id).count, template.post_versions_path(:search => {:updater_id_eq => user.id}))
template.link_to(user.post_update_count, template.post_versions_path(:search => {:updater_id_eq => user.id}))
end
def note_versions(template)
template.link_to(NoteVersion.for_user(user.id).count, template.note_versions_path(:search => {:updater_id_eq => user.id}))
template.link_to(user.note_update_count, template.note_versions_path(:search => {:updater_id_eq => user.id}))
end
def wiki_page_versions(template)

View File

@@ -2646,6 +2646,11 @@ CREATE TABLE users (
last_logged_in_at timestamp without time zone,
last_forum_read_at timestamp without time zone,
has_mail boolean DEFAULT false NOT NULL,
recent_tags text,
post_upload_count integer DEFAULT 0 NOT NULL,
post_update_count integer DEFAULT 0 NOT NULL,
note_update_count integer DEFAULT 0 NOT NULL,
favorite_count integer DEFAULT 0 NOT NULL,
receive_email_notifications boolean DEFAULT false NOT NULL,
comment_threshold integer DEFAULT (-1) NOT NULL,
always_resize_images boolean DEFAULT false NOT NULL,

View File

@@ -17,6 +17,10 @@ class CreateUsers < ActiveRecord::Migration
t.column :last_forum_read_at, :datetime
t.column :has_mail, :boolean, :null => false, :default => false
t.column :recent_tags, :text
t.column :post_upload_count, :integer, :null => false, :default => 0
t.column :post_update_count, :integer, :null => false, :default => 0
t.column :note_update_count, :integer, :null => false, :default => 0
t.column :favorite_count, :integer, :null => false, :default => 0
# Profile settings
t.column :receive_email_notifications, :boolean, :null => false, :default => false

View File

@@ -3121,13 +3121,21 @@ alter table users add column default_image_size varchar(255) not null default 'm
alter table users add column favorite_tags text;
alter table users add column blacklisted_tags text;
alter table users add column time_zone varchar(255) not null default 'Eastern Time (US & Canada)';
alter table users add column post_update_count integer not null default 0;
alter table users add column note_update_count integer not null default 0;
alter table users add column favorite_count integer not null default 0;
alter table users add column post_upload_count integer not null default 0;
alter table users drop column invite_count;
alter table users rename column upload_limit to base_upload_limit;
alter table users drop column uploaded_tags;
alter index idx_users__name rename to index_users_on_name;
create index index_users_on_email on users (email) where email is not null;
create index index_users_on_inviter_id on users (inviter_id) where inviter_id is not null;
update users set post_upload_count = (select count(*) from posts where uploader_id = users.id);
update users set blacklisted_tags = (select string_agg(_.tags, E'\n') from user_blacklisted_tags _ where _.user_id = users.id);
update users set post_update_count = (select count(*) from post_versions where updater_id = users.id);
update users set note_update_count = (select count(*) from note_versions where updater_id = users.id);
update users set favorite_count = (select count(*) from favorites where user_id = users.id);
drop table user_blacklisted_tags;
CREATE TABLE user_password_reset_nonces (

View File

@@ -56,6 +56,12 @@ class NoteTest < ActiveSupport::TestCase
@note = Factory.create(:note, :post => @post)
end
should "increment the updater's note_update_count" do
assert_difference("CurrentUser.note_update_count", 1) do
@note.update_attributes(:body => "zzz")
end
end
should "update the post's last_noted_at field" do
assert_nil(@post.last_noted_at)
@note.update_attributes(:x => 1000)

View File

@@ -415,6 +415,15 @@ class PostTest < ActiveSupport::TestCase
end
context "that has been updated" do
should "increment the updater's post_update_count" do
post = Factory.create(:post, :tag_string => "aaa bbb ccc")
assert_difference("CurrentUser.post_update_count", 1) do
post.update_attributes(:tag_string => "zzz")
CurrentUser.reload
end
end
should "reset its tag array cache" do
post = Factory.create(:post, :tag_string => "aaa bbb ccc")
user = Factory.create(:user)
@@ -525,29 +534,76 @@ class PostTest < ActiveSupport::TestCase
end
context "Favorites:" do
context "Adding a post to a user's favorites" do
should "update the fav strings ont he post" do
user = Factory.create(:user)
post = Factory.create(:post)
post.add_favorite!(user)
post.reload
assert_equal("fav:#{user.id}", post.fav_string)
assert(Favorite.exists?(:user_id => user.id, :post_id => post.id))
post.add_favorite!(user)
post.reload
assert_equal("fav:#{user.id}", post.fav_string)
assert(Favorite.exists?(:user_id => user.id, :post_id => post.id))
post.remove_favorite!(user)
post.reload
assert_equal("", post.fav_string)
assert(!Favorite.exists?(:user_id => user.id, :post_id => post.id))
context "Removing a post from a user's favorites" do
setup do
@user = Factory.create(:user)
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
@post = Factory.create(:post)
@post.add_favorite!(@user)
end
post.remove_favorite!(user)
post.reload
assert_equal("", post.fav_string)
assert(!Favorite.exists?(:user_id => user.id, :post_id => post.id))
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
should "decrement the user's favorite_count" do
assert_difference("CurrentUser.favorite_count", -1) do
@post.remove_favorite!(@user)
CurrentUser.reload
end
end
should "not decrement the user's favorite_count if the user did not favorite the post" do
@post2 = Factory.create(:post)
assert_difference("CurrentUser.favorite_count", 0) do
@post2.remove_favorite!(@user)
CurrentUser.reload
end
end
end
context "Adding a post to a user's favorites" do
setup do
@user = Factory.create(:user)
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
@post = Factory.create(:post)
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
should "increment the user's favorite_count" do
assert_difference("CurrentUser.favorite_count", 1) do
@post.add_favorite!(@user)
CurrentUser.reload
end
end
should "update the fav strings ont he post" do
@post.add_favorite!(@user)
@post.reload
assert_equal("fav:#{@user.id}", @post.fav_string)
assert(Favorite.exists?(:user_id => @user.id, :post_id => @post.id))
@post.add_favorite!(@user)
@post.reload
assert_equal("fav:#{@user.id}", @post.fav_string)
assert(Favorite.exists?(:user_id => @user.id, :post_id => @post.id))
@post.remove_favorite!(@user)
@post.reload
assert_equal("", @post.fav_string)
assert(!Favorite.exists?(:user_id => @user.id, :post_id => @post.id))
@post.remove_favorite!(@user)
@post.reload
assert_equal("", @post.fav_string)
assert(!Favorite.exists?(:user_id => @user.id, :post_id => @post.id))
end
end
end

View File

@@ -128,6 +128,14 @@ class UploadTest < ActiveSupport::TestCase
assert_equal(198695, File.size(@upload.resized_file_path_for(Danbooru.config.large_image_width)))
end
end
should "increment the uploaders post_upload_count" do
@upload = Factory.create(:source_upload)
assert_difference("CurrentUser.post_upload_count", 1) do
@upload.process!
CurrentUser.reload
end
end
should "process completely for a downloaded image" do
@upload = Factory.create(:source_upload,