diff --git a/app/models/note.rb b/app/models/note.rb index 00142815a..a20371366 100644 --- a/app/models/note.rb +++ b/app/models/note.rb @@ -147,8 +147,6 @@ class Note < ApplicationRecord def create_version return unless versioned_attributes_changed? - User.where(id: CurrentUser.id).update_all("note_update_count = note_update_count + 1") - CurrentUser.reload if merge_version? merge_version diff --git a/app/models/note_version.rb b/app/models/note_version.rb index 1ec07def1..c9c34a183 100644 --- a/app/models/note_version.rb +++ b/app/models/note_version.rb @@ -1,6 +1,6 @@ class NoteVersion < ApplicationRecord before_validation :initialize_updater - belongs_to :updater, :class_name => "User" + belongs_to :updater, :class_name => "User", :counter_cache => "note_update_count" scope :for_user, lambda {|user_id| where("updater_id = ?", user_id)} attr_accessible :note_id, :x, :y, :width, :height, :body, :updater_id, :updater_ip_addr, :is_active, :post_id, :html_id, :version diff --git a/app/models/post.rb b/app/models/post.rb index b389aa37b..e1056668f 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -36,7 +36,7 @@ class Post < ApplicationRecord belongs_to :updater, :class_name => "User" belongs_to :approver, :class_name => "User" - belongs_to :uploader, :class_name => "User" + belongs_to :uploader, :class_name => "User", :counter_cache => "post_upload_count" belongs_to :parent, :class_name => "Post" has_one :upload, :dependent => :destroy has_one :artist_commentary, :dependent => :destroy diff --git a/app/models/post_archive.rb b/app/models/post_archive.rb index bd2bf3a22..abdc7f98f 100644 --- a/app/models/post_archive.rb +++ b/app/models/post_archive.rb @@ -2,7 +2,7 @@ class PostArchive < ApplicationRecord extend Memoist belongs_to :post - belongs_to :updater, class_name: "User" + belongs_to :updater, class_name: "User", counter_cache: "post_update_count" def self.enabled? Danbooru.config.aws_sqs_archives_url.present? diff --git a/app/models/upload.rb b/app/models/upload.rb index 8563b1c99..c1b3adf14 100644 --- a/app/models/upload.rb +++ b/app/models/upload.rb @@ -139,7 +139,6 @@ class Upload < ApplicationRecord post = convert_to_post post.distribute_files if post.save - User.where(id: CurrentUser.id).update_all("post_upload_count = post_upload_count + 1") create_artist_commentary(post) if include_artist_commentary? ugoira_service.save_frame_data(post) if is_ugoira? notify_cropper(post) diff --git a/test/unit/post_test.rb b/test/unit/post_test.rb index 13b19c30a..f1151b706 100644 --- a/test/unit/post_test.rb +++ b/test/unit/post_test.rb @@ -61,6 +61,25 @@ class PostTest < ActiveSupport::TestCase assert_equal(0, Favorite.for_user(@user.id).where("post_id = ?", @post.id).count) end + should "decrement the uploader's upload count" do + assert_difference("@post.uploader.reload.post_upload_count", -1) do + @post.expunge! + end + end + + should "decrement the user's note update count" do + FactoryGirl.create(:note, post: @post) + assert_difference(["@post.uploader.reload.note_update_count"], -1) do + @post.expunge! + end + end + + should "decrement the user's post update count" do + assert_difference(["@post.uploader.reload.post_update_count"], -1) do + @post.expunge! + end + end + should "remove the post from iqdb" do mock_iqdb_service! Post.iqdb_sqs_service.expects(:send_message).with("remove\n#{@post.id}") @@ -1162,11 +1181,13 @@ class PostTest < ActiveSupport::TestCase should "increment the updater's post_update_count" do PostArchive.sqs_service.stubs(:merge?).returns(false) post = FactoryGirl.create(:post, :tag_string => "aaa bbb ccc") - CurrentUser.reload - assert_difference("CurrentUser.post_update_count", 1) do + # XXX in the test environment the update count gets bumped twice: and + # once by Post#post_update_count, and once by the counter cache. in + # production the counter cache doesn't bump the count, because + # versions are created on a separate server. + assert_difference("CurrentUser.user.reload.post_update_count", 2) do post.update_attributes(:tag_string => "zzz") - CurrentUser.reload end end