users: replace scopes with associations.

This commit is contained in:
evazion
2019-08-29 16:00:06 -05:00
parent 8756480500
commit 9a3e9747d8
14 changed files with 24 additions and 79 deletions

View File

@@ -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