users: replace scopes with associations.
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
class ArtistCommentaryVersion < ApplicationRecord
|
||||
belongs_to :post
|
||||
belongs_to_updater
|
||||
scope :for_user, ->(user_id) {where("updater_id = ?", user_id)}
|
||||
|
||||
def self.search(params)
|
||||
q = super
|
||||
|
||||
@@ -7,14 +7,6 @@ class ArtistVersion < ApplicationRecord
|
||||
delegate :visible?, :to => :artist
|
||||
|
||||
module SearchMethods
|
||||
def for_user(user_id)
|
||||
where("updater_id = ?", user_id)
|
||||
end
|
||||
|
||||
def updater_name(name)
|
||||
where("updater_id = (select _.id from users _ where lower(_.name) = ?)", name.mb_chars.downcase)
|
||||
end
|
||||
|
||||
def search(params)
|
||||
q = super
|
||||
|
||||
|
||||
@@ -35,14 +35,6 @@ class Comment < ApplicationRecord
|
||||
where(post_id: PostQueryBuilder.new(query).build.reorder(""))
|
||||
end
|
||||
|
||||
def for_creator(user_id)
|
||||
user_id.present? ? where("creator_id = ?", user_id) : none
|
||||
end
|
||||
|
||||
def for_creator_name(user_name)
|
||||
for_creator(User.name_to_id(user_name))
|
||||
end
|
||||
|
||||
def search(params)
|
||||
q = super
|
||||
|
||||
|
||||
@@ -138,14 +138,6 @@ class Dmail < ApplicationRecord
|
||||
where("owner_id = ?", CurrentUser.id)
|
||||
end
|
||||
|
||||
def to_name_matches(name)
|
||||
where("to_id = (select _.id from users _ where lower(_.name) = ?)", name.mb_chars.downcase)
|
||||
end
|
||||
|
||||
def from_name_matches(name)
|
||||
where("from_id = (select _.id from users _ where lower(_.name) = ?)", name.mb_chars.downcase)
|
||||
end
|
||||
|
||||
def search(params)
|
||||
q = super
|
||||
|
||||
|
||||
@@ -36,14 +36,6 @@ class ForumPost < ApplicationRecord
|
||||
joins(:topic).merge(ForumTopic.search(title_matches: title))
|
||||
end
|
||||
|
||||
def for_user(user_id)
|
||||
where("forum_posts.creator_id = ?", user_id)
|
||||
end
|
||||
|
||||
def creator_name(name)
|
||||
where("forum_posts.creator_id = (select _.id from users _ where lower(_.name) = ?)", name.mb_chars.downcase)
|
||||
end
|
||||
|
||||
def active
|
||||
where("forum_posts.is_deleted = false")
|
||||
end
|
||||
|
||||
@@ -20,14 +20,6 @@ class Note < ApplicationRecord
|
||||
where(post_id: PostQueryBuilder.new(query).build.reorder(""))
|
||||
end
|
||||
|
||||
def for_creator(user_id)
|
||||
where("creator_id = ?", user_id)
|
||||
end
|
||||
|
||||
def creator_name(name)
|
||||
where("creator_id = (select _.id from users _ where lower(_.name) = ?)", name.mb_chars.downcase)
|
||||
end
|
||||
|
||||
def search(params)
|
||||
q = super
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
class NoteVersion < ApplicationRecord
|
||||
belongs_to_updater :counter_cache => "note_update_count"
|
||||
scope :for_user, ->(user_id) {where("updater_id = ?", user_id)}
|
||||
|
||||
def self.search(params)
|
||||
q = super
|
||||
|
||||
@@ -22,18 +22,10 @@ class PostAppeal < ApplicationRecord
|
||||
joins(:post).where("posts.is_deleted = true or posts.is_flagged = true")
|
||||
end
|
||||
|
||||
def for_user(user_id)
|
||||
where("creator_id = ?", user_id)
|
||||
end
|
||||
|
||||
def recent
|
||||
where("created_at >= ?", 1.day.ago)
|
||||
end
|
||||
|
||||
def for_creator(user_id)
|
||||
where("creator_id = ?", user_id)
|
||||
end
|
||||
|
||||
def search(params)
|
||||
q = super
|
||||
|
||||
@@ -90,7 +82,7 @@ class PostAppeal < ApplicationRecord
|
||||
end
|
||||
|
||||
def appeal_count_for_creator
|
||||
PostAppeal.for_user(creator_id).recent.count
|
||||
creator.post_appeals.recent.count
|
||||
end
|
||||
|
||||
def method_attributes
|
||||
|
||||
@@ -52,15 +52,12 @@ class PostFlag < ApplicationRecord
|
||||
where("created_at <= ?", 3.days.ago)
|
||||
end
|
||||
|
||||
def for_creator(user_id)
|
||||
where("creator_id = ?", user_id)
|
||||
end
|
||||
|
||||
def search(params)
|
||||
q = super
|
||||
|
||||
q = q.attribute_matches(:reason, params[:reason_matches])
|
||||
|
||||
# XXX
|
||||
if params[:creator_id].present?
|
||||
if CurrentUser.can_view_flagger?(params[:creator_id].to_i)
|
||||
q = q.where.not(post_id: CurrentUser.user.posts)
|
||||
@@ -70,6 +67,7 @@ class PostFlag < ApplicationRecord
|
||||
end
|
||||
end
|
||||
|
||||
# XXX
|
||||
if params[:creator_name].present?
|
||||
flagger_id = User.name_to_id(params[:creator_name].strip)
|
||||
if flagger_id && CurrentUser.can_view_flagger?(flagger_id)
|
||||
@@ -150,7 +148,7 @@ class PostFlag < ApplicationRecord
|
||||
errors[:creator] << "cannot flag posts"
|
||||
end
|
||||
|
||||
if creator_id != User.system.id && PostFlag.for_creator(creator_id).where("created_at > ?", 30.days.ago).count >= CREATION_THRESHOLD
|
||||
if creator_id != User.system.id && creator.post_flags.where("created_at > ?", 30.days.ago).count >= CREATION_THRESHOLD
|
||||
report = Reports::PostFlags.new(user_id: post.uploader_id, date_range: 90.days.ago)
|
||||
|
||||
if report.attackers.include?(creator_id)
|
||||
@@ -185,7 +183,7 @@ class PostFlag < ApplicationRecord
|
||||
end
|
||||
|
||||
def flag_count_for_creator
|
||||
PostFlag.where(:creator_id => creator_id).recent.count
|
||||
creator.post_flags.recent.count
|
||||
end
|
||||
|
||||
def uploader_id
|
||||
|
||||
@@ -85,13 +85,18 @@ class User < ApplicationRecord
|
||||
before_update :encrypt_password_on_update
|
||||
before_create :promote_to_admin_if_first_user
|
||||
before_create :customize_new_user
|
||||
has_many :artist_versions, foreign_key: :updater_id
|
||||
has_many :artist_commentary_versions, foreign_key: :updater_id
|
||||
has_many :comments, foreign_key: :creator_id
|
||||
has_many :wiki_page_versions, foreign_key: :updater_id
|
||||
has_many :feedback, :class_name => "UserFeedback", :dependent => :destroy
|
||||
has_many :posts, :foreign_key => "uploader_id"
|
||||
has_many :post_appeals, foreign_key: :creator_id
|
||||
has_many :post_approvals, :dependent => :destroy
|
||||
has_many :post_disapprovals, :dependent => :destroy
|
||||
has_many :post_flags, foreign_key: :creator_id
|
||||
has_many :post_votes
|
||||
has_many :post_archives
|
||||
has_many :note_versions
|
||||
has_many :bans, -> {order("bans.id desc")}
|
||||
has_one :recent_ban, -> {order("bans.id desc")}, :class_name => "Ban"
|
||||
|
||||
@@ -456,8 +461,8 @@ class User < ApplicationRecord
|
||||
end
|
||||
|
||||
def used_upload_slots
|
||||
uploaded_count = Post.for_user(id).where("created_at >= ?", 23.hours.ago).count
|
||||
uploaded_comic_count = Post.for_user(id).tag_match("comic").where("created_at >= ?", 23.hours.ago).count / 3
|
||||
uploaded_count = posts.where("created_at >= ?", 23.hours.ago).count
|
||||
uploaded_comic_count = posts.tag_match("comic").where("created_at >= ?", 23.hours.ago).count / 3
|
||||
uploaded_count - uploaded_comic_count
|
||||
end
|
||||
memoize :used_upload_slots
|
||||
@@ -615,15 +620,15 @@ class User < ApplicationRecord
|
||||
|
||||
module CountMethods
|
||||
def wiki_page_version_count
|
||||
WikiPageVersion.for_user(id).count
|
||||
wiki_page_versions.count
|
||||
end
|
||||
|
||||
def artist_version_count
|
||||
ArtistVersion.for_user(id).count
|
||||
artist_versions.count
|
||||
end
|
||||
|
||||
def artist_commentary_version_count
|
||||
ArtistCommentaryVersion.for_user(id).count
|
||||
artist_commentary_versions.count
|
||||
end
|
||||
|
||||
def pool_version_count
|
||||
@@ -632,11 +637,11 @@ class User < ApplicationRecord
|
||||
end
|
||||
|
||||
def forum_post_count
|
||||
ForumPost.for_user(id).count
|
||||
forum_posts.count
|
||||
end
|
||||
|
||||
def comment_count
|
||||
Comment.for_creator(id).count
|
||||
comments.count
|
||||
end
|
||||
|
||||
def favorite_group_count
|
||||
@@ -644,11 +649,11 @@ class User < ApplicationRecord
|
||||
end
|
||||
|
||||
def appeal_count
|
||||
PostAppeal.for_creator(id).count
|
||||
post_appeals.count
|
||||
end
|
||||
|
||||
def flag_count
|
||||
PostFlag.for_creator(id).count
|
||||
post_flags.count
|
||||
end
|
||||
|
||||
def positive_feedback_count
|
||||
@@ -666,9 +671,9 @@ class User < ApplicationRecord
|
||||
def refresh_counts!
|
||||
self.class.without_timeout do
|
||||
User.where(id: id).update_all(
|
||||
post_upload_count: Post.for_user(id).count,
|
||||
post_upload_count: posts.count,
|
||||
post_update_count: PostArchive.for_user(id).count,
|
||||
note_update_count: NoteVersion.where(updater_id: id).count
|
||||
note_update_count: note_versions.count
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -28,10 +28,6 @@ class UserFeedback < ApplicationRecord
|
||||
where("category = ?", "negative")
|
||||
end
|
||||
|
||||
def for_user(user_id)
|
||||
where("user_id = ?", user_id)
|
||||
end
|
||||
|
||||
def visible(viewer = CurrentUser.user)
|
||||
if viewer.is_admin?
|
||||
all
|
||||
|
||||
@@ -43,7 +43,7 @@ class UserNameChangeRequest < ApplicationRecord
|
||||
end
|
||||
|
||||
def feedback
|
||||
UserFeedback.for_user(user_id).order("id desc")
|
||||
user.feedback.order("id desc")
|
||||
end
|
||||
|
||||
def approve!
|
||||
|
||||
@@ -6,10 +6,6 @@ class WikiPageVersion < ApplicationRecord
|
||||
delegate :visible?, :to => :wiki_page
|
||||
|
||||
module SearchMethods
|
||||
def for_user(user_id)
|
||||
where("updater_id = ?", user_id)
|
||||
end
|
||||
|
||||
def search(params)
|
||||
q = super
|
||||
|
||||
|
||||
@@ -80,7 +80,7 @@ class UserPresenter
|
||||
end
|
||||
|
||||
def deleted_upload_count(template)
|
||||
template.link_to(Post.for_user(user.id).deleted.count, template.posts_path(:tags => "status:deleted user:#{user.name}"))
|
||||
template.link_to(user.posts.deleted.count, template.posts_path(:tags => "status:deleted user:#{user.name}"))
|
||||
end
|
||||
|
||||
def favorite_count(template)
|
||||
|
||||
Reference in New Issue
Block a user